• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 "tensorflow/lite/c/common.h"
20 #include "tensorflow/lite/core/api/error_reporter.h"
21 #include "tensorflow/lite/schema/schema_generated.h"
22 
23 namespace tflite {
24 
25 // TODO(petewarden): This allocator never frees up or reuses  any memory, even
26 // though we have enough information about lifetimes of the tensors to do so.
27 // This makes it pretty wasteful, so we should use a more intelligent method.
28 class SimpleMemoryAllocator {
29  public:
SimpleMemoryAllocator(uint8_t * buffer,size_t buffer_size)30   SimpleMemoryAllocator(uint8_t* buffer, size_t buffer_size)
31       : data_size_max_(buffer_size), data_(buffer) {}
32 
33   // Allocates memory starting at the end of the arena (highest address and
34   // moving downwards, so that tensor buffers can be allocated from the start
35   // in ascending order.
36   uint8_t* AllocateFromTail(size_t size, size_t alignment);
37 
GetDataSize()38   size_t GetDataSize() const { return data_size_; }
GetBuffer()39   uint8_t* GetBuffer() const { return data_; }
GetMaxBufferSize()40   size_t GetMaxBufferSize() const { return data_size_max_; }
41 
42   // Child allocator is something like a temporary allocator. Memory allocated
43   // by the child allocator will be freed once the child allocator is
44   // deallocated. Child allocator could be cascaded to have for example
45   // grandchild allocator. But at any given time, only the latest child
46   // allocator can be used. All its ancestors will be locked to avoid memory
47   // corruption. Locked means that the allocator can't allocate memory.
48   // WARNING: Parent allocator needs to live longer than the child allocator.
49   SimpleMemoryAllocator CreateChildAllocator();
50 
51   // Unlocks parent allocator when the child allocator is deconstructed.
52   ~SimpleMemoryAllocator();
53 
54  private:
55   size_t data_size_ = 0;
56   size_t data_size_max_;
57   uint8_t* data_;
58   SimpleMemoryAllocator* parent_allocator_ = nullptr;
59   // The allocator is locaked if it has a child.
60   bool has_child_allocator_ = false;
61 };
62 
63 // Allocate a SimpleMemoryAllocator from the buffer and then return the pointer
64 // to this allocator.
65 SimpleMemoryAllocator* CreateInPlaceSimpleMemoryAllocator(uint8_t* buffer,
66                                                           size_t buffer_size);
67 
68 }  // namespace tflite
69 
70 #endif  // TENSORFLOW_LITE_MICRO_SIMPLE_MEMORY_ALLOCATOR_H_
71