1 /* Copyright 2020 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/recording_simple_memory_allocator.h"
17
18 #include <new>
19
20 #include "tensorflow/lite/kernels/internal/compatibility.h"
21
22 namespace tflite {
23
RecordingSimpleMemoryAllocator(ErrorReporter * error_reporter,uint8_t * buffer_head,size_t buffer_size)24 RecordingSimpleMemoryAllocator::RecordingSimpleMemoryAllocator(
25 ErrorReporter* error_reporter, uint8_t* buffer_head, size_t buffer_size)
26 : SimpleMemoryAllocator(error_reporter, buffer_head, buffer_size),
27 requested_head_bytes_(0),
28 requested_tail_bytes_(0),
29 used_bytes_(0),
30 alloc_count_(0) {}
31
~RecordingSimpleMemoryAllocator()32 RecordingSimpleMemoryAllocator::~RecordingSimpleMemoryAllocator() {}
33
Create(ErrorReporter * error_reporter,uint8_t * buffer_head,size_t buffer_size)34 RecordingSimpleMemoryAllocator* RecordingSimpleMemoryAllocator::Create(
35 ErrorReporter* error_reporter, uint8_t* buffer_head, size_t buffer_size) {
36 TFLITE_DCHECK(error_reporter != nullptr);
37 TFLITE_DCHECK(buffer_head != nullptr);
38 RecordingSimpleMemoryAllocator tmp =
39 RecordingSimpleMemoryAllocator(error_reporter, buffer_head, buffer_size);
40
41 uint8_t* allocator_buffer =
42 tmp.AllocateFromTail(sizeof(RecordingSimpleMemoryAllocator),
43 alignof(RecordingSimpleMemoryAllocator));
44 // Use the default copy constructor to populate internal states.
45 return new (allocator_buffer) RecordingSimpleMemoryAllocator(tmp);
46 }
47
GetRequestedBytes() const48 size_t RecordingSimpleMemoryAllocator::GetRequestedBytes() const {
49 return requested_head_bytes_ + requested_tail_bytes_;
50 }
51
GetUsedBytes() const52 size_t RecordingSimpleMemoryAllocator::GetUsedBytes() const {
53 return used_bytes_;
54 }
55
GetAllocatedCount() const56 size_t RecordingSimpleMemoryAllocator::GetAllocatedCount() const {
57 return alloc_count_;
58 }
59
SetHeadBufferSize(size_t size,size_t alignment)60 TfLiteStatus RecordingSimpleMemoryAllocator::SetHeadBufferSize(
61 size_t size, size_t alignment) {
62 const uint8_t* previous_head = head();
63 TfLiteStatus status =
64 SimpleMemoryAllocator::SetHeadBufferSize(size, alignment);
65 if (status == kTfLiteOk) {
66 used_bytes_ += head() - previous_head;
67 requested_head_bytes_ = size;
68 }
69 return status;
70 }
71
AllocateFromTail(size_t size,size_t alignment)72 uint8_t* RecordingSimpleMemoryAllocator::AllocateFromTail(size_t size,
73 size_t alignment) {
74 const uint8_t* previous_tail = tail();
75 uint8_t* result = SimpleMemoryAllocator::AllocateFromTail(size, alignment);
76 if (result != nullptr) {
77 used_bytes_ += previous_tail - tail();
78 requested_tail_bytes_ += size;
79 alloc_count_++;
80 }
81 return result;
82 }
83
84 } // namespace tflite
85