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/fst.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 struct MemoryRegion {
39   void *data;
40   void *mmap;
41   size_t size;
42 };
43 
44 class MappedFile {
45  public:
46   virtual ~MappedFile();
47 
mutable_data()48   void* mutable_data() const {
49     return reinterpret_cast<void*>(region_.data);
50   }
51 
data()52   const void* data() const {
53     return reinterpret_cast<void*>(region_.data);
54   }
55 
56   // Returns a MappedFile object that contains the contents of the input
57   // stream s starting from the current file position with size bytes.
58   // The file name must also be provided in the FstReadOptions as opts.source
59   // or else mapping will fail.  If mapping is not possible, then a MappedFile
60   // object with a new[]'ed  block of memory will be created.
61   static MappedFile* Map(istream* s, const FstReadOptions& opts, size_t size);
62 
63   // Creates a MappedFile object with a new[]'ed block of memory of size.
64   // RECOMMENDED FOR INTERNAL USE ONLY, may change in future releases.
65   static MappedFile* Allocate(size_t size);
66 
67   // Creates a MappedFile object pointing to a borrowed reference to data.
68   // This block of memory is not owned by the MappedFile object and will not
69   // be freed.
70   // RECOMMENDED FOR INTERNAL USE ONLY, may change in future releases.
71   static MappedFile* Borrow(void *data);
72 
73   static const int kArchAlignment;
74 
75  private:
76   explicit MappedFile(const MemoryRegion &region);
77 
78   MemoryRegion region_;
79   DISALLOW_COPY_AND_ASSIGN(MappedFile);
80 };
81 }  // namespace fst
82 
83 #endif  // FST_LIB_MAPPED_FILE_H_
84