Daniel Konegen / MNIST_example

Dependencies:   mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers simple_memory_allocator.cc Source File

simple_memory_allocator.cc

00001 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
00002 
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 ==============================================================================*/
00015 
00016 #include "tensorflow/lite/experimental/micro/simple_memory_allocator.h"
00017 
00018 #include "tensorflow/lite/core/api/flatbuffer_conversions.h"
00019 #include "tensorflow/lite/experimental/micro/memory_helpers.h"
00020 
00021 namespace tflite {
00022 
00023 uint8_t* SimpleMemoryAllocator::AllocateFromTail(size_t size,
00024                                                  size_t alignment) {
00025   if (has_child_allocator_) {
00026     // TODO(wangtz): Add error reporting when the parent allocator is locked!
00027     return nullptr;
00028   }
00029   uint8_t* previous_free = (data_ + data_size_max_) - data_size_;
00030   uint8_t* current_data = previous_free - size;
00031   uint8_t* aligned_result = AlignPointerDown(current_data, alignment);
00032   size_t aligned_size = (previous_free - aligned_result);
00033   if ((data_size_ + aligned_size) > data_size_max_) {
00034     // TODO(petewarden): Add error reporting beyond returning null!
00035     return nullptr;
00036   }
00037   data_size_ += aligned_size;
00038   return aligned_result;
00039 }
00040 
00041 SimpleMemoryAllocator SimpleMemoryAllocator::CreateChildAllocator() {
00042   // Note that the parameterized constructor initializes data_size_ to 0 which
00043   // is not what we expected.
00044   SimpleMemoryAllocator child = *this;
00045   child.parent_allocator_ = this;
00046   // With C++ copy elision, &child should be available after return.
00047   has_child_allocator_ = true;
00048   return child;
00049 }
00050 
00051 SimpleMemoryAllocator::~SimpleMemoryAllocator() {
00052   // Root allocator doesn't have a parent.
00053   if (nullptr != parent_allocator_) {
00054     parent_allocator_->has_child_allocator_ = false;
00055   }
00056 }
00057 
00058 }  // namespace tflite