1 /* Copyright 2017 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 #ifndef TENSORFLOW_LITE_SIMPLE_MEMORY_ARENA_H_ 16 #define TENSORFLOW_LITE_SIMPLE_MEMORY_ARENA_H_ 17 18 #include <list> 19 #include <memory> 20 #include "tensorflow/lite/c/c_api_internal.h" 21 22 namespace tflite { 23 24 // This little structure holds the offset and the size for a dynamic memory 25 // allocation in the memory arena. When the arena is committed and the 26 // underlying buffer is set, the alloc can be resolved into an actual memory 27 // pointer. 28 struct ArenaAlloc { ArenaAllocArenaAlloc29 ArenaAlloc() : offset(0), size(0) {} 30 31 size_t offset; 32 size_t size; 33 34 inline bool operator<(const ArenaAlloc& other) const { 35 return offset < other.offset; 36 } 37 }; 38 39 // This small class is responsible for allocating, deallocating and reusing 40 // dynamic memory from a common underlying buffer. The arena can be used in 41 // scenarios when the pattern of memory allocations and deallocations is 42 // repetitive, e.g. running NN inference in multiple iterations. Note that 43 // zero-sized allocations are explicitly allowed, and will resolve to null. 44 class SimpleMemoryArena { 45 public: SimpleMemoryArena(size_t arena_alignment)46 explicit SimpleMemoryArena(size_t arena_alignment) 47 : committed_(false), 48 arena_alignment_(arena_alignment), 49 high_water_mark_(0), 50 underlying_buffer_size_(0), 51 allocs_() {} 52 53 TfLiteStatus Allocate(TfLiteContext* context, size_t alignment, size_t size, 54 ArenaAlloc* new_alloc); 55 56 TfLiteStatus Deallocate(TfLiteContext* context, const ArenaAlloc& alloc); 57 RequiredBufferSize()58 inline size_t RequiredBufferSize() { 59 // Add in a small amount of padding to reduce the chance of resize events 60 // for small allocations. 61 size_t padding = arena_alignment_; 62 return arena_alignment_ + high_water_mark_ + padding; 63 } 64 65 TfLiteStatus Commit(TfLiteContext* context); 66 67 TfLiteStatus ResolveAlloc(TfLiteContext* context, const ArenaAlloc& alloc, 68 char** output_ptr); 69 70 TfLiteStatus Clear(); 71 BasePointer()72 int64_t BasePointer() const { 73 return reinterpret_cast<int64_t>(underlying_buffer_aligned_ptr_); 74 } 75 76 private: 77 bool committed_; 78 size_t arena_alignment_; 79 size_t high_water_mark_; 80 std::unique_ptr<char[]> underlying_buffer_; 81 size_t underlying_buffer_size_; 82 char* underlying_buffer_aligned_ptr_; 83 // TODO(maciekc): add list iterator to the ArenaAlloc to lookup quickly. 84 std::list<ArenaAlloc> allocs_; 85 }; 86 87 } // namespace tflite 88 89 #endif // TENSORFLOW_LITE_SIMPLE_MEMORY_ARENA_H_ 90