FST  openfst-1.8.2
OpenFst Library
mapped-file.h
Go to the documentation of this file.
1 // Copyright 2005-2020 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 #ifdef _WIN32
22 #include <windows.h>
23 #include <fst/compat.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, size_t size);
66 
67  // Returns a MappedFile object that contains the contents of the file referred
68  // to by the file descriptor starting from pos with size bytes. If the
69  // memory mapping fails, nullptr is returned. In contrast to Map(), this
70  // factory function does not backoff to allocating and reading.
71  static MappedFile *MapFromFileDescriptor(int fd, size_t pos, size_t size);
72 
73  // Creates a MappedFile object with a new'ed block of memory of size. The
74  // align argument can be used to specify a desired block alignment.
75  // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
76  // releases.
77  static MappedFile *Allocate(size_t size, size_t align = kArchAlignment);
78 
79  // Creates a MappedFile object with a new'ed block of memory with enough
80  // space for count elements of type T, correctly aligned for the type.
81  // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
82  // releases.
83  template <typename T>
84  static MappedFile *AllocateType(size_t count) {
85  return Allocate(sizeof(T) * count, alignof(T));
86  }
87 
88  // Creates a MappedFile object pointing to a borrowed reference to data. This
89  // block of memory is not owned by the MappedFile object and will not be
90  // freed. This is RECOMMENDED FOR INTERNAL USE ONLY, may change in future
91  // releases.
92  static MappedFile *Borrow(void *data);
93 
94  // Alignment required for mapping structures in bytes. Regions of memory that
95  // are not aligned upon a 128-bit boundary are read from the file instead.
96  // This is consistent with the alignment boundary set in ConstFst and
97  // CompactFst.
98  static constexpr size_t kArchAlignment = 16;
99 
100  static constexpr size_t kMaxReadChunk = 256 * 1024 * 1024; // 256 MB.
101 
102  private:
103  explicit MappedFile(const MemoryRegion &region);
104 
105  MemoryRegion region_;
106  MappedFile(const MappedFile &) = delete;
107  MappedFile &operator=(const MappedFile &) = delete;
108 };
109 } // namespace fst
110 
111 #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:46
static MappedFile * AllocateType(size_t count)
Definition: mapped-file.h:84
const void * data() const
Definition: mapped-file.h:57