You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.1 KiB

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