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 | |
16 | #include "tensorflow/lite/allocation.h" |
17 | |
18 | #include <stddef.h> |
19 | #include <sys/stat.h> |
20 | #include <sys/types.h> |
21 | |
22 | #include <cstdint> |
23 | #include <cstdio> |
24 | #include <memory> |
25 | |
26 | #include "tensorflow/lite/core/api/error_reporter.h" |
27 | |
28 | namespace tflite { |
29 | |
30 | #ifndef TFLITE_MCU |
31 | FileCopyAllocation::FileCopyAllocation(const char* filename, |
32 | ErrorReporter* error_reporter) |
33 | : Allocation(error_reporter, Allocation::Type::kFileCopy) { |
34 | // Obtain the file size using fstat, or report an error if that fails. |
35 | std::unique_ptr<FILE, decltype(&fclose)> file(fopen(filename, "rb" ), fclose); |
36 | if (!file) { |
37 | error_reporter_->Report("Could not open '%s'." , filename); |
38 | return; |
39 | } |
40 | struct stat sb; |
41 | |
42 | // support usage of msvc's posix-like fileno symbol |
43 | #ifdef _WIN32 |
44 | #define FILENO(_x) _fileno(_x) |
45 | #else |
46 | #define FILENO(_x) fileno(_x) |
47 | #endif |
48 | if (fstat(FILENO(file.get()), &sb) != 0) { |
49 | error_reporter_->Report("Failed to get file size of '%s'." , filename); |
50 | return; |
51 | } |
52 | #undef FILENO |
53 | buffer_size_bytes_ = sb.st_size; |
54 | std::unique_ptr<char[]> buffer(new char[buffer_size_bytes_]); |
55 | if (!buffer) { |
56 | error_reporter_->Report("Malloc of buffer to hold copy of '%s' failed." , |
57 | filename); |
58 | return; |
59 | } |
60 | size_t bytes_read = |
61 | fread(buffer.get(), sizeof(char), buffer_size_bytes_, file.get()); |
62 | if (bytes_read != buffer_size_bytes_) { |
63 | error_reporter_->Report("Read of '%s' failed (too few bytes read)." , |
64 | filename); |
65 | return; |
66 | } |
67 | // Versions of GCC before 6.2.0 don't support std::move from non-const |
68 | // char[] to const char[] unique_ptrs. |
69 | copied_buffer_.reset(const_cast<char const*>(buffer.release())); |
70 | } |
71 | |
72 | FileCopyAllocation::~FileCopyAllocation() {} |
73 | |
74 | const void* FileCopyAllocation::base() const { return copied_buffer_.get(); } |
75 | |
76 | size_t FileCopyAllocation::bytes() const { return buffer_size_bytes_; } |
77 | |
78 | bool FileCopyAllocation::valid() const { return copied_buffer_ != nullptr; } |
79 | #endif |
80 | |
81 | MemoryAllocation::MemoryAllocation(const void* ptr, size_t num_bytes, |
82 | ErrorReporter* error_reporter) |
83 | : Allocation(error_reporter, Allocation::Type::kMemory) { |
84 | #ifdef __arm__ |
85 | if ((reinterpret_cast<uintptr_t>(ptr) & 0x3) != 0) { |
86 | // The flatbuffer schema has alignment requirements of up to 16 bytes to |
87 | // guarantee that data can be correctly accesses by various backends. |
88 | // Therefore, model pointer should also be 16-bytes aligned to preserve this |
89 | // requirement. But this condition only checks 4-bytes alignment which is |
90 | // the mininum requirement to prevent SIGBUS fault on 32bit ARM. Some models |
91 | // could require 8 or 16 bytes alignment which is not checked yet. |
92 | // |
93 | // Note that 64-bit ARM may also suffer a performance impact, but no crash - |
94 | // that case is not checked. |
95 | TF_LITE_REPORT_ERROR(error_reporter, |
96 | "The supplied buffer is not 4-bytes aligned" ); |
97 | buffer_ = nullptr; |
98 | buffer_size_bytes_ = 0; |
99 | return; |
100 | } |
101 | #endif // __arm__ |
102 | |
103 | buffer_ = ptr; |
104 | buffer_size_bytes_ = num_bytes; |
105 | } |
106 | |
107 | MemoryAllocation::~MemoryAllocation() {} |
108 | |
109 | const void* MemoryAllocation::base() const { return buffer_; } |
110 | |
111 | size_t MemoryAllocation::bytes() const { return buffer_size_bytes_; } |
112 | |
113 | bool MemoryAllocation::valid() const { return buffer_ != nullptr; } |
114 | |
115 | } // namespace tflite |
116 | |