Move all header method implementations of SimpleMemoryAllocator to the cc file.

This is a cleanup to prepare for some recording and logging subclasses of this class to help TF Micro keep tracking of tail/head buffer allocations.

PiperOrigin-RevId: 313247671
Change-Id: Id6766c02467e1829961addfcc449fdf6990ce684
This commit is contained in:
Nick Kreeger 2020-05-26 12:37:41 -07:00 committed by TensorFlower Gardener
parent 741ef7999c
commit c64cdf3098
2 changed files with 48 additions and 15 deletions

View File

@ -23,6 +23,20 @@ limitations under the License.
namespace tflite { 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( SimpleMemoryAllocator* CreateInPlaceSimpleMemoryAllocator(
ErrorReporter* error_reporter, uint8_t* buffer, size_t buffer_size) { ErrorReporter* error_reporter, uint8_t* buffer, size_t buffer_size) {
SimpleMemoryAllocator tmp = SimpleMemoryAllocator tmp =
@ -64,4 +78,28 @@ uint8_t* SimpleMemoryAllocator::AllocateFromTail(size_t size,
return aligned_result; 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 } // namespace tflite

View File

@ -29,15 +29,9 @@ namespace tflite {
class SimpleMemoryAllocator { class SimpleMemoryAllocator {
public: public:
SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer_head, SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer_head,
uint8_t* buffer_tail) uint8_t* buffer_tail);
: error_reporter_(error_reporter),
buffer_head_(buffer_head),
buffer_tail_(buffer_tail),
head_(buffer_head),
tail_(buffer_tail) {}
SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer, SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer,
size_t buffer_size) size_t buffer_size);
: SimpleMemoryAllocator(error_reporter, buffer, buffer + buffer_size) {}
// Allocates memory starting at the head of the arena (lowest address and // Allocates memory starting at the head of the arena (lowest address and
// moving upwards). // moving upwards).
@ -46,16 +40,17 @@ class SimpleMemoryAllocator {
// moving downwards). // moving downwards).
uint8_t* AllocateFromTail(size_t size, size_t alignment); uint8_t* AllocateFromTail(size_t size, size_t alignment);
uint8_t* GetHead() const { return head_; } uint8_t* GetHead() const;
uint8_t* GetTail() const { return tail_; } uint8_t* GetTail() const;
size_t GetAvailableMemory() const { return tail_ - head_; }
size_t GetUsedBytes() const { return GetBufferSize() - GetAvailableMemory(); }
size_t GetHeadUsedBytes() const { return head_ - buffer_head_; } size_t GetHeadUsedBytes() const;
size_t GetTailUsedBytes() const { return buffer_tail_ - tail_; } size_t GetTailUsedBytes() const;
size_t GetAvailableMemory() const;
size_t GetUsedBytes() const;
private: private:
size_t GetBufferSize() const { return buffer_tail_ - buffer_head_; } size_t GetBufferSize() const;
ErrorReporter* error_reporter_; ErrorReporter* error_reporter_;
uint8_t* buffer_head_; uint8_t* buffer_head_;