Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core/common/spaceallocator/itf/spaceallocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ class SpaceAllocatorItf {
*/
virtual void FreeSpace(size_t size) = 0;

/**
* Resizes space.
*
* @param oldSize old size.
* @param newSize new size.
* @return Error.
*/
virtual Error ResizeSpace(size_t oldSize, size_t newSize) = 0;

/**
* Adds outdated item.
*
Expand Down
35 changes: 35 additions & 0 deletions src/core/common/spaceallocator/spaceallocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Space : public SpaceItf {
*/
Error Resize(size_t size) override
{
if (auto err = mAllocator->ResizeSpace(mSize, size); !err.IsNone()) {
return err;
}

mSize = size;

return ErrorEnum::eNone;
Expand Down Expand Up @@ -432,6 +436,8 @@ class SpaceAllocator : public SpaceAllocatorItf, public SpaceAllocatorStorage {
}

if (err = mPartition->Allocate(size); !err.IsNone()) {
Free(size);

return {nullptr, err};
}

Expand All @@ -451,6 +457,35 @@ class SpaceAllocator : public SpaceAllocatorItf, public SpaceAllocatorStorage {
mPartition->Free(size);
}

/**
* Resizes space.
*
* @param oldSize old size.
* @param newSize new size.
* @return Error.
*/
Error ResizeSpace(size_t oldSize, size_t newSize) override
{
if (oldSize == newSize) {
return ErrorEnum::eNone;
}

Free(oldSize);
mPartition->Free(oldSize);

if (auto err = Allocate(newSize); !err.IsNone()) {
return err;
}

if (auto err = mPartition->Allocate(newSize); !err.IsNone()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if an error occurs, shouldn't SpaceAllocator::Free be called?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Good catch!

Free(newSize);

return err;
}

return ErrorEnum::eNone;
}

/**
* Allocates done.
*
Expand Down
45 changes: 44 additions & 1 deletion src/core/common/spaceallocator/tests/spaceallocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ namespace aos::spaceallocator {

class SpaceallocatorTest : public Test {
protected:
void TearDown() { fs::RemoveAll(mPath); }
void TearDown()
{
// Clear global partition state between tests
SpaceAllocator<1>::mPartitions.Clear();
fs::RemoveAll(mPath);
}

StrictMock<FSPlatformMock> mPlatformFS;
StrictMock<ItemRemoverMock> mRemover;
Expand Down Expand Up @@ -292,4 +297,42 @@ TEST_F(SpaceallocatorTest, PartLimit)
ASSERT_TRUE(mSpaceAllocator.Close().IsNone());
}

TEST_F(SpaceallocatorTest, ResizeSpace)
{
SpaceAllocator<5> mSpaceAllocator;

EXPECT_CALL(mPlatformFS, GetMountPoint(mPath))
.WillOnce(Return(RetWithError<StaticString<cFilePathLen>>(mMountPoint, ErrorEnum::eNone)));

EXPECT_CALL(mPlatformFS, GetTotalSize(mMountPoint))
.WillOnce(Return(RetWithError<size_t>(mTotalSize, ErrorEnum::eNone)));

ASSERT_TRUE(mSpaceAllocator.Init(mPath, mPlatformFS, mLimit).IsNone());

EXPECT_CALL(mPlatformFS, GetAvailableSize(mMountPoint))
.WillOnce(Return(RetWithError<size_t>(mTotalSize, ErrorEnum::eNone)));

const size_t initialSize = 256 * cKilobyte;

auto [space, err] = mSpaceAllocator.AllocateSpace(initialSize);
ASSERT_TRUE(err.IsNone());
ASSERT_NE(space.Get(), nullptr);
ASSERT_EQ(space->Size(), initialSize);

const size_t newSize = 512 * cKilobyte;
ASSERT_TRUE(space->Resize(newSize).IsNone());
ASSERT_EQ(space->Size(), newSize);

const size_t smallerSize = 128 * cKilobyte;
ASSERT_TRUE(space->Resize(smallerSize).IsNone());
ASSERT_EQ(space->Size(), smallerSize);

ASSERT_TRUE(space->Resize(smallerSize).IsNone());
ASSERT_EQ(space->Size(), smallerSize);

EXPECT_TRUE(space->Accept().IsNone());

ASSERT_TRUE(mSpaceAllocator.Close().IsNone());
}

} // namespace aos::spaceallocator
1 change: 1 addition & 0 deletions src/core/common/tests/mocks/spaceallocatormock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SpaceAllocatorMock : public SpaceAllocatorItf {
public:
MOCK_METHOD(RetWithError<UniquePtr<SpaceItf>>, AllocateSpace, (size_t size), (override));
MOCK_METHOD(void, FreeSpace, (size_t size), (override));
MOCK_METHOD(Error, ResizeSpace, (size_t oldSize, size_t newSize), (override));
MOCK_METHOD(Error, AddOutdatedItem, (const String& id, const Time& timestamp), (override));
MOCK_METHOD(Error, RestoreOutdatedItem, (const String& id), (override));
MOCK_METHOD(Error, AllocateDone, (), (override));
Expand Down
Loading