1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_ 17 #define TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_ 18 19 #include <cstddef> 20 #include <cstdint> 21 22 #include "tensorflow/lite/c/common.h" 23 #include "tensorflow/lite/core/api/error_reporter.h" 24 #include "tensorflow/lite/micro/compatibility.h" 25 26 namespace tflite { 27 28 // TODO(petewarden): This allocator never frees up or reuses any memory, even 29 // though we have enough information about lifetimes of the tensors to do so. 30 // This makes it pretty wasteful, so we should use a more intelligent method. 31 class SimpleMemoryAllocator { 32 public: 33 // TODO(b/157615197): Cleanup constructors/destructor and use factory 34 // functions. 35 SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer_head, 36 uint8_t* buffer_tail); 37 SimpleMemoryAllocator(ErrorReporter* error_reporter, uint8_t* buffer, 38 size_t buffer_size); 39 virtual ~SimpleMemoryAllocator(); 40 41 // Creates a new SimpleMemoryAllocator from a given buffer head and size. 42 static SimpleMemoryAllocator* Create(ErrorReporter* error_reporter, 43 uint8_t* buffer_head, 44 size_t buffer_size); 45 46 // Adjusts the head (lowest address and moving upwards) memory allocation to a 47 // given size. Calls to this method will also invalidate all temporary 48 // allocation values (it sets the location of temp space at the end of the 49 // head section). This call will fail if a chain of allocations through 50 // AllocateTemp() have not been cleaned up with a call to 51 // ResetTempAllocations(). 52 virtual TfLiteStatus SetHeadBufferSize(size_t size, size_t alignment); 53 54 // Allocates memory starting at the tail of the arena (highest address and 55 // moving downwards). 56 virtual uint8_t* AllocateFromTail(size_t size, size_t alignment); 57 58 // Allocates a temporary buffer from the head of the arena (lowest address and 59 // moving upwards) but does not update the actual head allocation size or 60 // position. The returned buffer is guaranteed until either 61 // ResetTempAllocations() is called or another call to AllocateFromHead(). 62 // Repeat calls to this function will create a chain of temp allocations. All 63 // calls to AllocateTemp() must end with a call to ResetTempAllocations(). If 64 // AllocateFromHead() is called before a call to ResetTempAllocations(), it 65 // will fail with an error message. 66 virtual uint8_t* AllocateTemp(size_t size, size_t alignment); 67 68 // Resets a chain of temporary allocations back to the current head of the 69 // arena (lowest address). 70 virtual void ResetTempAllocations(); 71 72 // Returns a pointer to the buffer currently assigned to the head section. 73 // This buffer is set by calling SetHeadSize(). 74 uint8_t* GetHeadBuffer() const; 75 76 // Returns the size of the head section in bytes. 77 size_t GetHeadUsedBytes() const; 78 79 // Returns the size of all allocations in the tail section in bytes. 80 size_t GetTailUsedBytes() const; 81 82 // Returns the number of bytes available with a given alignment. This number 83 // takes in account any temporary allocations. 84 size_t GetAvailableMemory(size_t alignment) const; 85 86 // Returns the number of used bytes in the allocator. This number takes in 87 // account any temporary allocations. 88 size_t GetUsedBytes() const; 89 90 protected: 91 // Returns a pointer to the current end of the head buffer. 92 uint8_t* head() const; 93 94 // Returns a pointer to the current end of the tail buffer. 95 uint8_t* tail() const; 96 97 private: 98 size_t GetBufferSize() const; 99 100 ErrorReporter* error_reporter_; 101 uint8_t* buffer_head_; 102 uint8_t* buffer_tail_; 103 uint8_t* head_; 104 uint8_t* tail_; 105 uint8_t* temp_; 106 107 TF_LITE_REMOVE_VIRTUAL_DELETE 108 }; 109 110 } // namespace tflite 111 112 #endif // TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_ 113