• 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 #include "tensorflow/lite/micro/simple_memory_allocator.h"
17 
18 #include <cstddef>
19 
20 #include "tensorflow/lite/core/api/flatbuffer_conversions.h"
21 #include "tensorflow/lite/micro/memory_helpers.h"
22 
23 namespace tflite {
24 
CreateInPlaceSimpleMemoryAllocator(uint8_t * buffer,size_t buffer_size)25 SimpleMemoryAllocator* CreateInPlaceSimpleMemoryAllocator(uint8_t* buffer,
26                                                           size_t buffer_size) {
27   SimpleMemoryAllocator tmp = SimpleMemoryAllocator(buffer, buffer_size);
28   SimpleMemoryAllocator* in_place_allocator =
29       reinterpret_cast<SimpleMemoryAllocator*>(tmp.AllocateFromTail(
30           sizeof(SimpleMemoryAllocator), alignof(SimpleMemoryAllocator)));
31   *in_place_allocator = tmp;
32   return in_place_allocator;
33 }
34 
AllocateFromTail(size_t size,size_t alignment)35 uint8_t* SimpleMemoryAllocator::AllocateFromTail(size_t size,
36                                                  size_t alignment) {
37   if (has_child_allocator_) {
38     // TODO(wangtz): Add error reporting when the parent allocator is locked!
39     return nullptr;
40   }
41   uint8_t* previous_free = (data_ + data_size_max_) - data_size_;
42   uint8_t* current_data = previous_free - size;
43   uint8_t* aligned_result = AlignPointerDown(current_data, alignment);
44   std::ptrdiff_t aligned_size = (previous_free - aligned_result);
45   if ((data_size_ + aligned_size) > data_size_max_) {
46     // TODO(petewarden): Add error reporting beyond returning null!
47     return nullptr;
48   }
49   data_size_ += aligned_size;
50   return aligned_result;
51 }
52 
CreateChildAllocator()53 SimpleMemoryAllocator SimpleMemoryAllocator::CreateChildAllocator() {
54   // Note that the parameterized constructor initializes data_size_ to 0 which
55   // is not what we expected.
56   SimpleMemoryAllocator child = *this;
57   child.parent_allocator_ = this;
58   has_child_allocator_ = true;
59   return child;
60 }
61 
~SimpleMemoryAllocator()62 SimpleMemoryAllocator::~SimpleMemoryAllocator() {
63   // Root allocator doesn't have a parent.
64   if (nullptr != parent_allocator_) {
65     parent_allocator_->has_child_allocator_ = false;
66   }
67 }
68 
69 }  // namespace tflite
70