• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/stat.h>
19 #include <sys/types.h>
20 #include <cassert>
21 #include <cstdarg>
22 #include <cstdint>
23 #include <cstring>
24 #include <utility>
25 
26 #include "tensorflow/lite/c/c_api_internal.h"
27 #include "tensorflow/lite/core/api/error_reporter.h"
28 
29 namespace tflite {
30 
31 #ifndef TFLITE_MCU
FileCopyAllocation(const char * filename,ErrorReporter * error_reporter)32 FileCopyAllocation::FileCopyAllocation(const char* filename,
33                                        ErrorReporter* error_reporter)
34     : Allocation(error_reporter) {
35   // Obtain the file size, using an alternative method that is does not
36   // require fstat for more compatibility.
37   std::unique_ptr<FILE, decltype(&fclose)> file(fopen(filename, "rb"), fclose);
38   if (!file) {
39     error_reporter_->Report("Could not open '%s'.", filename);
40     return;
41   }
42   // TODO(ahentz): Why did you think using fseek here was better for finding
43   // the size?
44   struct stat sb;
45   if (fstat(fileno(file.get()), &sb) != 0) {
46     error_reporter_->Report("Failed to get file size of '%s'.", filename);
47     return;
48   }
49   buffer_size_bytes_ = sb.st_size;
50   std::unique_ptr<char[]> buffer(new char[buffer_size_bytes_]);
51   if (!buffer) {
52     error_reporter_->Report("Malloc of buffer to hold copy of '%s' failed.",
53                             filename);
54     return;
55   }
56   size_t bytes_read =
57       fread(buffer.get(), sizeof(char), buffer_size_bytes_, file.get());
58   if (bytes_read != buffer_size_bytes_) {
59     error_reporter_->Report("Read of '%s' failed (too few bytes read).",
60                             filename);
61     return;
62   }
63   // Versions of GCC before 6.2.0 don't support std::move from non-const
64   // char[] to const char[] unique_ptrs.
65   copied_buffer_.reset(const_cast<char const*>(buffer.release()));
66 }
67 
~FileCopyAllocation()68 FileCopyAllocation::~FileCopyAllocation() {}
69 
base() const70 const void* FileCopyAllocation::base() const { return copied_buffer_.get(); }
71 
bytes() const72 size_t FileCopyAllocation::bytes() const { return buffer_size_bytes_; }
73 
valid() const74 bool FileCopyAllocation::valid() const { return copied_buffer_ != nullptr; }
75 #endif
76 
MemoryAllocation(const void * ptr,size_t num_bytes,ErrorReporter * error_reporter)77 MemoryAllocation::MemoryAllocation(const void* ptr, size_t num_bytes,
78                                    ErrorReporter* error_reporter)
79     : Allocation(error_reporter) {
80   buffer_ = ptr;
81   buffer_size_bytes_ = num_bytes;
82 }
83 
~MemoryAllocation()84 MemoryAllocation::~MemoryAllocation() {}
85 
base() const86 const void* MemoryAllocation::base() const { return buffer_; }
87 
bytes() const88 size_t MemoryAllocation::bytes() const { return buffer_size_bytes_; }
89 
valid() const90 bool MemoryAllocation::valid() const { return buffer_ != nullptr; }
91 
92 }  // namespace tflite
93