1 /* Copyright 2017 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 /// \file 16 /// Memory management for TF Lite. 17 #ifndef TENSORFLOW_LITE_ALLOCATION_H_ 18 #define TENSORFLOW_LITE_ALLOCATION_H_ 19 20 #include <stddef.h> 21 22 #include <cstdio> 23 #include <cstdlib> 24 #include <memory> 25 #include <vector> 26 27 #include "tensorflow/lite/c/common.h" 28 #include "tensorflow/lite/core/api/error_reporter.h" 29 #include "tensorflow/lite/string_type.h" 30 31 namespace tflite { 32 33 // A memory allocation handle. This could be a mmap or shared memory. 34 class Allocation { 35 public: ~Allocation()36 virtual ~Allocation() {} 37 38 enum class Type { 39 kMMap, 40 kFileCopy, 41 kMemory, 42 }; 43 44 // Base pointer of this allocation 45 virtual const void* base() const = 0; 46 // Size in bytes of the allocation 47 virtual size_t bytes() const = 0; 48 // Whether the allocation is valid 49 virtual bool valid() const = 0; 50 // Return the type of the Allocation. type()51 Type type() const { return type_; } 52 53 protected: Allocation(ErrorReporter * error_reporter,Type type)54 Allocation(ErrorReporter* error_reporter, Type type) 55 : error_reporter_(error_reporter), type_(type) {} 56 ErrorReporter* error_reporter_; 57 58 private: 59 const Type type_; 60 }; 61 62 class MMAPAllocation : public Allocation { 63 public: 64 MMAPAllocation(const char* filename, ErrorReporter* error_reporter); 65 virtual ~MMAPAllocation(); 66 const void* base() const override; 67 size_t bytes() const override; 68 bool valid() const override; 69 fd()70 int fd() const { return mmap_fd_; } 71 72 static bool IsSupported(); 73 74 protected: 75 // Data required for mmap. 76 int mmap_fd_ = -1; // mmap file descriptor 77 const void* mmapped_buffer_; 78 size_t buffer_size_bytes_ = 0; 79 }; 80 81 class FileCopyAllocation : public Allocation { 82 public: 83 FileCopyAllocation(const char* filename, ErrorReporter* error_reporter); 84 virtual ~FileCopyAllocation(); 85 const void* base() const override; 86 size_t bytes() const override; 87 bool valid() const override; 88 89 private: 90 // Data required for mmap. 91 std::unique_ptr<const char[]> copied_buffer_; 92 size_t buffer_size_bytes_ = 0; 93 }; 94 95 class MemoryAllocation : public Allocation { 96 public: 97 // Allocates memory with the pointer and the number of bytes of the memory. 98 // The pointer has to remain alive and unchanged until the destructor is 99 // called. 100 MemoryAllocation(const void* ptr, size_t num_bytes, 101 ErrorReporter* error_reporter); 102 virtual ~MemoryAllocation(); 103 const void* base() const override; 104 size_t bytes() const override; 105 bool valid() const override; 106 107 private: 108 const void* buffer_; 109 size_t buffer_size_bytes_ = 0; 110 }; 111 112 } // namespace tflite 113 114 #endif // TENSORFLOW_LITE_ALLOCATION_H_ 115