FST  openfst-1.8.3
OpenFst Library
mapped-file.h
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
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 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 
18 #ifndef FST_MAPPED_FILE_H_
19 #define FST_MAPPED_FILE_H_
20 
21 #include <fst/compat.h>
22 #ifdef _WIN32
23 #include <windows.h>
24 #endif
25 
26 #include <cstddef>
27 #include <istream>
28 #include <string>
29 
30 #include <fst/flags.h>
31 
32 namespace fst {
33 
34 // A memory region is a simple abstraction for allocated memory or data from
35 // memory-mapped files. If mmap is null, then data represents an owned region
36 // of size bytes. Otherwise, mmap and size refer to the mapping and data is a
37 // casted pointer to a region contained within [mmap, mmap + size). If size is
38 // 0, then mmap and data refer to a block of memory managed externally by some
39 // other allocator. The offset is used when allocating memory to providing
40 // padding for alignment.
41 struct MemoryRegion {
42  void *data;
43  void *mmap;
44  size_t size;
45  size_t offset;
46 #ifdef _WIN32
47  HANDLE file_mapping;
48 #endif
49 };
50 
51 class MappedFile {
52  public:
53  ~MappedFile();
54 
55  void *mutable_data() const { return region_.data; }
56 
57  const void *data() const { return region_.data; }
58 
59  // Returns a MappedFile object that contains the contents of the input stream
60  // strm starting from the current file position with size bytes. The memorymap
61  // bool is advisory, and Map will default to allocating and reading. The
62  // source argument needs to contain the filename that was used to open the
63  // input stream.
64  static MappedFile * Map(std::istream &istrm, bool memorymap,
65  const std::string &source,
66  size_t size);
67 
68  // Returns a MappedFile object that contains the contents of the file referred
69  // to by the file descriptor starting from pos with size bytes. If the
70  // memory mapping fails, nullptr is returned. In contrast to Map(), this
71  // factory function does not backoff to allocating and reading.
72  static MappedFile * MapFromFileDescriptor(int fd, size_t pos,
73  size_t size);
74 
75  // Creates a MappedFile object with a new'ed block of memory of size. The
76  // align argument can be used to specify a desired block alignment.
77  // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
78  // releases.
79  static MappedFile *Allocate(size_t size, size_t align = kArchAlignment);
80 
81  // Creates a MappedFile object with a new'ed block of memory with enough
82  // space for count elements of type T, correctly aligned for the type.
83  // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
84  // releases.
85  template <typename T>
86  static MappedFile *AllocateType(size_t count) {
87  return Allocate(sizeof(T) * count, alignof(T));
88  }
89 
90  // Creates a MappedFile object pointing to a borrowed reference to data. This
91  // block of memory is not owned by the MappedFile object and will not be
92  // freed. This is RECOMMENDED FOR INTERNAL USE ONLY, may change in future
93  // releases.
94  static MappedFile *Borrow(void *data);
95 
96  // Alignment required for mapping structures in bytes. Regions of memory that
97  // are not aligned upon a 128-bit boundary are read from the file instead.
98  // This is consistent with the alignment boundary set in ConstFst and
99  // CompactFst.
100  static constexpr size_t kArchAlignment = 16;
101 
102  static constexpr size_t kMaxReadChunk = 256 * 1024 * 1024; // 256 MB.
103 
104  private:
105  explicit MappedFile(const MemoryRegion &region);
106 
107  MemoryRegion region_;
108  MappedFile(const MappedFile &) = delete;
109  MappedFile &operator=(const MappedFile &) = delete;
110 };
111 } // namespace fst
112 
113 #endif // FST_MAPPED_FILE_H_
void * mutable_data() const
Definition: mapped-file.h:55
void Map(FarReader< Arc > &reader, FarWriter< Arc > &writer, Functor functor)
Definition: map-reduce.h:49
static MappedFile * AllocateType(size_t count)
Definition: mapped-file.h:86
const void * data() const
Definition: mapped-file.h:57