FST  openfst-1.7.1
OpenFst Library
mapped-file.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #ifndef FST_MAPPED_FILE_H_
5 #define FST_MAPPED_FILE_H_
6 
7 #include <cstddef>
8 #include <istream>
9 #include <string>
10 
11 #include <fst/compat.h>
12 #include <fst/flags.h>
13 
14 namespace fst {
15 
16 // A memory region is a simple abstraction for allocated memory or data from
17 // memory-mapped files. If mmap is null, then data represents an owned region
18 // of size bytes. Otherwise, mmap and size refer to the mapping and data is a
19 // casted pointer to a region contained within [mmap, mmap + size). If size is
20 // 0, then mmap and data refer to a block of memory managed externally by some
21 // other allocator. The offset is used when allocating memory to providing
22 // padding for alignment.
23 struct MemoryRegion {
24  void *data;
25  void *mmap;
26  size_t size;
27  int offset;
28 };
29 
30 class MappedFile {
31  public:
32  ~MappedFile();
33 
34  void *mutable_data() const { return region_.data; }
35 
36  const void *data() const { return region_.data; }
37 
38  // Returns a MappedFile object that contains the contents of the input stream
39  // strm starting from the current file position with size bytes. The memorymap
40  // bool is advisory, and Map will default to allocating and reading. The
41  // source argument needs to contain the filename that was used to open the
42  // input stream.
43  static MappedFile *Map(std::istream *istrm, bool memorymap,
44  const string &source, size_t size);
45 
46  // Returns a MappedFile object that contains the contents of the file referred
47  // to by the file descriptor starting from pos with size bytes. If the
48  // memory mapping fails, nullptr is returned. In contrast to Map(), this
49  // factory function does not backoff to allocating and reading.
50  static MappedFile *MapFromFileDescriptor(int fd, int pos, size_t size);
51 
52  // Creates a MappedFile object with a new[]'ed block of memory of size. The
53  // align argument can be used to specify a desired block alignment.
54  // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
55  // releases.
56  static MappedFile *Allocate(size_t size, int align = kArchAlignment);
57 
58  // Creates a MappedFile object pointing to a borrowed reference to data. This
59  // block of memory is not owned by the MappedFile object and will not be
60  // freed. This is RECOMMENDED FOR INTERNAL USE ONLY, may change in future
61  // releases.
62  static MappedFile *Borrow(void *data);
63 
64  // Alignment required for mapping structures in bytes. Regions of memory that
65  // are not aligned upon a 128-bit boundary are read from the file instead.
66  // This is consistent with the alignment boundary set in ConstFst and
67  // CompactFst.
68  static constexpr int kArchAlignment = 16;
69 
70  static constexpr size_t kMaxReadChunk = 256 * 1024 * 1024; // 256 MB.
71 
72  private:
73  explicit MappedFile(const MemoryRegion &region);
74 
75  MemoryRegion region_;
76  MappedFile(const MappedFile &) = delete;
77  MappedFile &operator=(const MappedFile &) = delete;
78 };
79 } // namespace fst
80 
81 #endif // FST_MAPPED_FILE_H_
void * mutable_data() const
Definition: mapped-file.h:34
void Map(MutableFst< A > *fst, C *mapper)
Definition: map.h:17
const void * data() const
Definition: mapped-file.h:36