diff --git a/tensorflow/lite/micro/simple_memory_allocator.cc b/tensorflow/lite/micro/simple_memory_allocator.cc index 911e1e404f7..d55e7e87640 100644 --- a/tensorflow/lite/micro/simple_memory_allocator.cc +++ b/tensorflow/lite/micro/simple_memory_allocator.cc @@ -23,6 +23,20 @@ limitations under the License. namespace tflite { +SimpleMemoryAllocator::SimpleMemoryAllocator(ErrorReporter* error_reporter, + uint8_t* buffer_head, + uint8_t* buffer_tail) + : error_reporter_(error_reporter), + buffer_head_(buffer_head), + buffer_tail_(buffer_tail), + head_(buffer_head), + tail_(buffer_tail) {} + +SimpleMemoryAllocator::SimpleMemoryAllocator(ErrorReporter* error_reporter, + uint8_t* buffer, + size_t buffer_size) + : SimpleMemoryAllocator(error_reporter, buffer, buffer + buffer_size) {} + SimpleMemoryAllocator* CreateInPlaceSimpleMemoryAllocator( ErrorReporter* error_reporter, uint8_t* buffer, size_t buffer_size) { SimpleMemoryAllocator tmp = @@ -64,4 +78,28 @@ uint8_t* SimpleMemoryAllocator::AllocateFromTail(size_t size, return aligned_result; } +uint8_t* SimpleMemoryAllocator::GetHead() const { return head_; } + +uint8_t* SimpleMemoryAllocator::GetTail() const { return tail_; } + +size_t SimpleMemoryAllocator::GetHeadUsedBytes() const { + return head_ - buffer_head_; +} + +size_t SimpleMemoryAllocator::GetTailUsedBytes() const { + return buffer_tail_ - tail_; +} + +size_t SimpleMemoryAllocator::GetAvailableMemory() const { + return tail_ - head_; +} + +size_t SimpleMemoryAllocator::GetUsedBytes() const { + return GetBufferSize() - GetAvailableMemory(); +} + +size_t SimpleMemoryAllocator::GetBufferSize() const { + return buffer_tail_ - buffer_head_; +} + } // namespace tflite diff --git a/tensorflow/lite/micro/simple_memory_allocator.h b/tensorflow/lite/micro/simple_memory_allocator.h index 223ef8398a4..5be260f9ed2 100644 --- a/tensorflow/lite/micro/simple_memory_allocator.h +++ b/tensorflow/lite/micro/simple_memory_allocator.h @@ -29,15 +29,9 @@ namespace tflite { class SimpleMemoryAllocator { public: SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer_head, - uint8_t* buffer_tail) - : error_reporter_(error_reporter), - buffer_head_(buffer_head), - buffer_tail_(buffer_tail), - head_(buffer_head), - tail_(buffer_tail) {} + uint8_t* buffer_tail); SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer, - size_t buffer_size) - : SimpleMemoryAllocator(error_reporter, buffer, buffer + buffer_size) {} + size_t buffer_size); // Allocates memory starting at the head of the arena (lowest address and // moving upwards). @@ -46,16 +40,17 @@ class SimpleMemoryAllocator { // moving downwards). uint8_t* AllocateFromTail(size_t size, size_t alignment); - uint8_t* GetHead() const { return head_; } - uint8_t* GetTail() const { return tail_; } - size_t GetAvailableMemory() const { return tail_ - head_; } - size_t GetUsedBytes() const { return GetBufferSize() - GetAvailableMemory(); } + uint8_t* GetHead() const; + uint8_t* GetTail() const; - size_t GetHeadUsedBytes() const { return head_ - buffer_head_; } - size_t GetTailUsedBytes() const { return buffer_tail_ - tail_; } + size_t GetHeadUsedBytes() const; + size_t GetTailUsedBytes() const; + + size_t GetAvailableMemory() const; + size_t GetUsedBytes() const; private: - size_t GetBufferSize() const { return buffer_tail_ - buffer_head_; } + size_t GetBufferSize() const; ErrorReporter* error_reporter_; uint8_t* buffer_head_;