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 
26 #include "tensorflow/lite/core/api/error_reporter.h"
27 
28 namespace tflite {
29 
30 // A memory allocation handle. This could be a mmap or shared memory.
31 class Allocation {
32  public:
~Allocation()33   virtual ~Allocation() {}
34 
35   enum class Type {
36     kMMap,
37     kFileCopy,
38     kMemory,
39   };
40 
41   // Base pointer of this allocation
42   virtual const void* base() const = 0;
43   // Size in bytes of the allocation
44   virtual size_t bytes() const = 0;
45   // Whether the allocation is valid
46   virtual bool valid() const = 0;
47   // Return the type of the Allocation.
type()48   Type type() const { return type_; }
49 
50  protected:
Allocation(ErrorReporter * error_reporter,Type type)51   Allocation(ErrorReporter* error_reporter, Type type)
52       : error_reporter_(error_reporter), type_(type) {}
53   ErrorReporter* error_reporter_;
54 
55  private:
56   const Type type_;
57 };
58 
59 // Note that not all platforms support MMAP-based allocation.
60 // Use `IsSupported()` to check.
61 class MMAPAllocation : public Allocation {
62  public:
63   // Loads and maps the provided file to a memory region.
64   MMAPAllocation(const char* filename, ErrorReporter* error_reporter);
65 
66   // Maps the provided file descriptor to a memory region.
67   // Note: The provided file descriptor will be dup'ed for usage; the caller
68   // retains ownership of the provided descriptor and should close accordingly.
69   MMAPAllocation(int fd, ErrorReporter* error_reporter);
70 
71   virtual ~MMAPAllocation();
72   const void* base() const override;
73   size_t bytes() const override;
74   bool valid() const override;
75 
fd()76   int fd() const { return mmap_fd_; }
77 
78   static bool IsSupported();
79 
80  protected:
81   // Data required for mmap.
82   int mmap_fd_ = -1;  // mmap file descriptor
83   const void* mmapped_buffer_;
84   size_t buffer_size_bytes_ = 0;
85 
86  private:
87   // Assumes ownership of the provided `owned_fd` instance.
88   MMAPAllocation(ErrorReporter* error_reporter, int owned_fd);
89 };
90 
91 class FileCopyAllocation : public Allocation {
92  public:
93   // Loads the provided file into a heap memory region.
94   FileCopyAllocation(const char* filename, ErrorReporter* error_reporter);
95   virtual ~FileCopyAllocation();
96   const void* base() const override;
97   size_t bytes() const override;
98   bool valid() const override;
99 
100  private:
101   std::unique_ptr<const char[]> copied_buffer_;
102   size_t buffer_size_bytes_ = 0;
103 };
104 
105 class MemoryAllocation : public Allocation {
106  public:
107   // Provides a (read-only) view of the provided buffer region as an allocation.
108   // Note: The caller retains ownership of `ptr`, and must ensure it remains
109   // valid for the lifetime of the class instance.
110   MemoryAllocation(const void* ptr, size_t num_bytes,
111                    ErrorReporter* error_reporter);
112   virtual ~MemoryAllocation();
113   const void* base() const override;
114   size_t bytes() const override;
115   bool valid() const override;
116 
117  private:
118   const void* buffer_;
119   size_t buffer_size_bytes_ = 0;
120 };
121 
122 }  // namespace tflite
123 
124 #endif  // TENSORFLOW_LITE_ALLOCATION_H_
125