• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_LITE_MICRO_RECORDING_SIMPLE_MEMORY_ALLOCATOR_H_
17 #define TENSORFLOW_LITE_MICRO_RECORDING_SIMPLE_MEMORY_ALLOCATOR_H_
18 
19 #include "tensorflow/lite/micro/compatibility.h"
20 #include "tensorflow/lite/micro/simple_memory_allocator.h"
21 
22 namespace tflite {
23 
24 // Utility class used to log allocations of a SimpleMemoryAllocator. Should only
25 // be used in debug/evaluation settings or unit tests to evaluate allocation
26 // usage.
27 class RecordingSimpleMemoryAllocator : public SimpleMemoryAllocator {
28  public:
29   RecordingSimpleMemoryAllocator(ErrorReporter* error_reporter,
30                                  uint8_t* buffer_head, size_t buffer_size);
31   // TODO(b/157615197): Cleanup constructors/destructor and use factory
32   // functions.
33   ~RecordingSimpleMemoryAllocator() override;
34 
35   static RecordingSimpleMemoryAllocator* Create(ErrorReporter* error_reporter,
36                                                 uint8_t* buffer_head,
37                                                 size_t buffer_size);
38 
39   // Returns the number of bytes requested from the head or tail.
40   size_t GetRequestedBytes() const;
41 
42   // Returns the number of bytes actually allocated from the head or tail. This
43   // value will be >= to the number of requested bytes due to padding and
44   // alignment.
45   size_t GetUsedBytes() const;
46 
47   // Returns the number of alloc calls from the head or tail.
48   size_t GetAllocatedCount() const;
49 
50   TfLiteStatus SetHeadBufferSize(size_t size, size_t alignment) override;
51   uint8_t* AllocateFromTail(size_t size, size_t alignment) override;
52 
53  private:
54   size_t requested_head_bytes_;
55   size_t requested_tail_bytes_;
56   size_t used_bytes_;
57   size_t alloc_count_;
58 
59   TF_LITE_REMOVE_VIRTUAL_DELETE
60 };
61 
62 }  // namespace tflite
63 
64 #endif  // TENSORFLOW_LITE_MICRO_RECORDING_SIMPLE_MEMORY_ALLOCATOR_H_
65