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 // Main abstraction controlling the tflite interpreter. 16 // See context.h for the API for defining operations (TfLiteRegistration). 17 #ifndef TENSORFLOW_CONTRIB_LITE_ALLOCATION_H_ 18 #define TENSORFLOW_CONTRIB_LITE_ALLOCATION_H_ 19 20 #include <cstdio> 21 #include <cstdlib> 22 #include <vector> 23 #include "tensorflow/contrib/lite/context.h" 24 #include "tensorflow/contrib/lite/error_reporter.h" 25 #include "tensorflow/contrib/lite/simple_memory_arena.h" 26 27 namespace tflite { 28 29 // A memory allocation handle. This could be a mmap or shared memory. 30 class Allocation { 31 public: Allocation(ErrorReporter * error_reporter)32 Allocation(ErrorReporter* error_reporter) : error_reporter_(error_reporter) {} ~Allocation()33 virtual ~Allocation() {} 34 35 // Base pointer of this allocation 36 virtual const void* base() const = 0; 37 // Size in bytes of the allocation 38 virtual size_t bytes() const = 0; 39 // Whether the allocation is valid 40 virtual bool valid() const = 0; 41 42 protected: 43 ErrorReporter* error_reporter_; 44 }; 45 46 class MMAPAllocation : public Allocation { 47 public: 48 MMAPAllocation(const char* filename, ErrorReporter* error_reporter); 49 virtual ~MMAPAllocation(); 50 const void* base() const override; 51 size_t bytes() const override; 52 bool valid() const override; 53 54 protected: 55 // Data required for mmap. 56 int mmap_fd_ = -1; // mmap file descriptor 57 const void* mmapped_buffer_; 58 size_t buffer_size_bytes_ = 0; 59 }; 60 61 class FileCopyAllocation : public Allocation { 62 public: 63 FileCopyAllocation(const char* filename, ErrorReporter* error_reporter); 64 virtual ~FileCopyAllocation(); 65 const void* base() const override; 66 size_t bytes() const override; 67 bool valid() const override; 68 69 private: 70 // Data required for mmap. 71 std::unique_ptr<const char[]> copied_buffer_; 72 size_t buffer_size_bytes_ = 0; 73 }; 74 75 class MemoryAllocation : public Allocation { 76 public: 77 // Allocates memory with the pointer and the number of bytes of the memory. 78 // The pointer has to remain alive and unchanged until the destructor is 79 // called. 80 MemoryAllocation(const void* ptr, size_t num_bytes, 81 ErrorReporter* error_reporter); 82 virtual ~MemoryAllocation(); 83 const void* base() const override; 84 size_t bytes() const override; 85 bool valid() const override; 86 87 private: 88 const void* buffer_; 89 size_t buffer_size_bytes_ = 0; 90 }; 91 92 } // namespace tflite 93 94 #endif // TENSORFLOW_CONTRIB_LITE_ALLOCATION_H_ 95