1 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 // 14 // Copyright 2005-2010 Google, Inc. 15 // Author: sorenj@google.com (Jeffrey Sorensen) 16 17 #ifndef FST_LIB_MAPPED_FILE_H_ 18 #define FST_LIB_MAPPED_FILE_H_ 19 20 #include <unistd.h> 21 #include <sys/mman.h> 22 23 #include <fst/compat.h> 24 #include <iostream> 25 #include <fstream> 26 #include <sstream> 27 28 DECLARE_int32(fst_arch_alignment); // defined in mapped-file.h 29 30 namespace fst { 31 32 // A memory region is a simple abstraction for allocated memory or data from 33 // mmap'ed files. If mmap equals NULL, then data represents an owned region of 34 // size bytes. Otherwise, mmap and size refer to the mapping and data is a 35 // casted pointer to a region contained within [mmap, mmap + size). 36 // If size is 0, then mmap refers and data refer to a block of memory managed 37 // externally by some other allocator. 38 // offset is used when allocating memory to providing padding for alignment. 39 struct MemoryRegion { 40 void *data; 41 void *mmap; 42 size_t size; 43 int offset; 44 }; 45 46 class MappedFile { 47 public: 48 virtual ~MappedFile(); 49 mutable_data()50 void* mutable_data() const { 51 return reinterpret_cast<void*>(region_.data); 52 } 53 data()54 const void* data() const { 55 return reinterpret_cast<void*>(region_.data); 56 } 57 58 // Returns a MappedFile object that contains the contents of the input 59 // stream s starting from the current file position with size bytes. 60 // the memorymap bool is advisory, and Map will default to allocating and 61 // reading. source needs to contain the filename that was used to open 62 // the istream. 63 static MappedFile* Map(istream* s, bool memorymap, const string& source, 64 size_t size); 65 66 // Creates a MappedFile object with a new[]'ed block of memory of size. 67 // Align can be used to specify a desired block alignment. 68 // RECOMMENDED FOR INTERNAL USE ONLY, may change in future releases. 69 static MappedFile* Allocate(size_t size, int align = kArchAlignment); 70 71 // Creates a MappedFile object pointing to a borrowed reference to data. 72 // This block of memory is not owned by the MappedFile object and will not 73 // be freed. 74 // RECOMMENDED FOR INTERNAL USE ONLY, may change in future releases. 75 static MappedFile* Borrow(void *data); 76 77 static const int kArchAlignment; 78 79 private: 80 explicit MappedFile(const MemoryRegion ®ion); 81 82 MemoryRegion region_; 83 DISALLOW_COPY_AND_ASSIGN(MappedFile); 84 }; 85 } // namespace fst 86 87 #endif // FST_LIB_MAPPED_FILE_H_ 88