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_EXPERIMENTAL_MICRO_SIMPLE_TENSOR_ALLOCATOR_H_
17 #define TENSORFLOW_LITE_EXPERIMENTAL_MICRO_SIMPLE_TENSOR_ALLOCATOR_H_
18 
19 #include "tensorflow/lite/c/c_api_internal.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 SimpleTensorAllocator {
29  public:
SimpleTensorAllocator(uint8_t * buffer,int buffer_size)30   SimpleTensorAllocator(uint8_t* buffer, int buffer_size)
31       : data_size_(0), data_size_max_(buffer_size), data_(buffer) {}
32 
33   TfLiteStatus AllocateTensor(
34       const tflite::Tensor& flatbuffer_tensor, int create_before,
35       int destroy_after,
36       const flatbuffers::Vector<flatbuffers::Offset<Buffer>>* buffers,
37       ErrorReporter* error_reporter, TfLiteTensor* result);
38 
39   uint8_t* AllocateMemory(size_t size, size_t alignment);
40 
GetDataSize()41   int GetDataSize() const { return data_size_; }
42 
43  private:
44   int data_size_;
45   int data_size_max_;
46   uint8_t* data_;
47 };
48 
49 }  // namespace tflite
50 
51 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_MICRO_SIMPLE_TENSOR_ALLOCATOR_H_
52