1 /* Copyright 2020 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 #ifndef TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_EXTERNAL_FILE_HANDLER_H_
17 #define TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_EXTERNAL_FILE_HANDLER_H_
18 
19 #include <memory>
20 
21 #include "absl/status/status.h"
22 #include "absl/strings/string_view.h"
23 #include "tensorflow_lite_support/cc/port/integral_types.h"
24 #include "tensorflow_lite_support/cc/port/statusor.h"
25 #include "tensorflow_lite_support/cc/task/core/proto/external_file_proto_inc.h"
26 
27 namespace tflite {
28 namespace task {
29 namespace core {
30 
31 // Handler providing easy access to the contents of a file specified by an
32 // ExternalFile proto [1]. Takes care (if needed, depending on the provided
33 // proto fields) of opening and/or mapping the file in memory at creation time,
34 // as well as closing and/or unmapping at destruction time.
35 //
36 // [1]: support/c/task/core/proto/external_file.proto
37 class ExternalFileHandler {
38  public:
39   // Creates an ExternalFileHandler from the input ExternalFile proto and
40   // returns a pointer to the new object. Ownership is transferred to the
41   // caller. Returns an error if the creation failed, which may happen if the
42   // provided ExternalFile can't be opened or mapped into memory.
43   //
44   // Warning: Does not take ownership of `external_file`, which must refer to a
45   // valid proto that outlives this object.
46   static tflite::support::StatusOr<std::unique_ptr<ExternalFileHandler>>
47   CreateFromExternalFile(const ExternalFile* external_file);
48 
49   ~ExternalFileHandler();
50 
51   // Returns the content of the ExternalFile as a string_view guaranteed to be
52   // valid as long as the ExternalFileHandler is alive.
53   absl::string_view GetFileContent();
54 
55  private:
56   // Private constructor, called from CreateFromExternalFile().
ExternalFileHandler(const ExternalFile * external_file)57   explicit ExternalFileHandler(const ExternalFile* external_file)
58       : external_file_(*external_file) {}
59 
60   // Opens (if provided by path) and maps (if provided by path or file
61   // descriptor) the external file in memory. Does nothing otherwise, as file
62   // contents are already loaded in memory.
63   absl::Status MapExternalFile();
64 
65   // Reference to the input ExternalFile.
66   const ExternalFile& external_file_;
67 
68   // The file descriptor of the ExternalFile if provided by path, as it is
69   // opened and owned by this class. Set to -1 otherwise.
70   int owned_fd_{-1};
71 
72   // Points to the memory buffer mapped from the file descriptor of the
73   // ExternalFile, if provided by path or file descriptor.
74   void* buffer_{};
75 
76   // The mapped memory buffer offset, if any.
77   int64 buffer_offset_{};
78   // The size in bytes of the mapped memory buffer, if any.
79   int64 buffer_size_{};
80 
81   // As mmap(2) requires the offset to be a multiple of sysconf(_SC_PAGE_SIZE):
82 
83   // The aligned mapped memory buffer offset, if any.
84   int64 buffer_aligned_offset_{};
85   // The aligned mapped memory buffer size in bytes taking into account the
86   // offset shift introduced by buffer_aligned_memory_offset_, if any.
87   int64 buffer_aligned_size_{};
88 };
89 
90 }  // namespace core
91 }  // namespace task
92 }  // namespace tflite
93 
94 #endif  // TENSORFLOW_LITE_SUPPORT_CC_TASK_CORE_EXTERNAL_FILE_HANDLER_H_
95