Return correct size for GetAvailableMemory() and GetUsedBytes() in SimpleMemoryAllocator when temp allocations are used.

The current implementation always returns the byte length from `head_` and `tail_`. The real actual usage is based off of `temp_` which increments from `head_`.

PiperOrigin-RevId: 335550052
Change-Id: I9acce31b1e70df802bd6de8082a96e430e7ffb29
This commit is contained in:
Nick Kreeger 2020-10-05 19:15:22 -07:00 committed by TensorFlower Gardener
parent 4f0d3bd4d2
commit 083a88f29d
3 changed files with 79 additions and 4 deletions

View File

@ -131,13 +131,13 @@ size_t SimpleMemoryAllocator::GetTailUsedBytes() const {
}
size_t SimpleMemoryAllocator::GetAvailableMemory(size_t alignment) const {
uint8_t* const aligned_head = AlignPointerUp(head_, alignment);
uint8_t* const aligned_temp = AlignPointerUp(temp_, alignment);
uint8_t* const aligned_tail = AlignPointerDown(tail_, alignment);
return aligned_tail - aligned_head;
return aligned_tail - aligned_temp;
}
size_t SimpleMemoryAllocator::GetUsedBytes() const {
return GetBufferSize() - (tail_ - head_);
return GetBufferSize() - (tail_ - temp_);
}
size_t SimpleMemoryAllocator::GetBufferSize() const {

View File

@ -81,9 +81,12 @@ class SimpleMemoryAllocator {
size_t GetHeadUsedBytes() const;
size_t GetTailUsedBytes() const;
// Returns the number of bytes available with a given alignment.
// Returns the number of bytes available with a given alignment. This number
// takes in account any temporary allocations.
size_t GetAvailableMemory(size_t alignment) const;
// Returns the number of used bytes in the allocator. This number takes in
// account any temporary allocations.
size_t GetUsedBytes() const;
private:

View File

@ -98,6 +98,78 @@ TF_LITE_MICRO_TEST(TestAdjustHeadSizeMisalignedHandlesCorrectBytesAvailable) {
TF_LITE_MICRO_EXPECT_GE(aligned_available_bytes, arena_size - 1000 - 24);
}
TF_LITE_MICRO_TEST(TestGetAvailableMemory) {
constexpr size_t arena_size = 1024;
uint8_t arena[arena_size];
tflite::SimpleMemoryAllocator allocator(micro_test::reporter, arena,
arena_size);
constexpr size_t allocation_size = 100;
allocator.SetHeadSize(/*size=*/allocation_size,
/*alignment=*/1);
allocator.AllocateFromTail(/*size=*/allocation_size,
/*alignment=*/1);
TF_LITE_MICRO_EXPECT_EQ(allocator.GetAvailableMemory(/*alignment=*/1),
arena_size - allocation_size * 2);
}
TF_LITE_MICRO_TEST(TestGetAvailableMemoryWithTempAllocations) {
constexpr size_t arena_size = 1024;
uint8_t arena[arena_size];
tflite::SimpleMemoryAllocator allocator(micro_test::reporter, arena,
arena_size);
constexpr size_t allocation_size = 100;
allocator.AllocateTemp(/*size=*/allocation_size,
/*alignment=*/1);
TF_LITE_MICRO_EXPECT_EQ(allocator.GetAvailableMemory(/*alignment=*/1),
arena_size - allocation_size);
// Reset temp allocations and ensure GetAvailableMemory() is back to the
// starting size:
allocator.ResetTempAllocations();
TF_LITE_MICRO_EXPECT_EQ(allocator.GetAvailableMemory(/*alignment=*/1),
arena_size);
}
TF_LITE_MICRO_TEST(TestGetUsedBytes) {
constexpr size_t arena_size = 1024;
uint8_t arena[arena_size];
tflite::SimpleMemoryAllocator allocator(micro_test::reporter, arena,
arena_size);
TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(0));
constexpr size_t allocation_size = 100;
allocator.SetHeadSize(/*size=*/allocation_size,
/*alignment=*/1);
allocator.AllocateFromTail(/*size=*/allocation_size,
/*alignment=*/1);
TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), allocation_size * 2);
}
TF_LITE_MICRO_TEST(TestGetUsedBytesTempAllocations) {
constexpr size_t arena_size = 1024;
uint8_t arena[arena_size];
tflite::SimpleMemoryAllocator allocator(micro_test::reporter, arena,
arena_size);
constexpr size_t allocation_size = 100;
allocator.AllocateTemp(/*size=*/allocation_size,
/*alignment=*/1);
TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), allocation_size);
// Reset temp allocations and ensure GetUsedBytes() is back to the starting
// size:
allocator.ResetTempAllocations();
TF_LITE_MICRO_EXPECT_EQ(allocator.GetUsedBytes(), static_cast<size_t>(0));
}
TF_LITE_MICRO_TEST(TestJustFits) {
constexpr size_t arena_size = 1024;
uint8_t arena[arena_size];