proxygen
MemoryMapping.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <boost/noncopyable.hpp>
20 #include <glog/logging.h>
21 
22 #include <folly/File.h>
23 #include <folly/Range.h>
24 
25 namespace folly {
26 
32 class MemoryMapping : boost::noncopyable {
33  public:
39  enum class LockMode {
40  TRY_LOCK,
41  MUST_LOCK,
42  };
54  struct Options {
55  Options() {}
56 
57  // Convenience methods; return *this for chaining.
58  Options& setPageSize(off_t v) {
59  pageSize = v;
60  return *this;
61  }
62  Options& setShared(bool v) {
63  shared = v;
64  return *this;
65  }
67  prefault = v;
68  return *this;
69  }
71  readable = v;
72  return *this;
73  }
75  writable = v;
76  return *this;
77  }
78  Options& setGrow(bool v) {
79  grow = v;
80  return *this;
81  }
82 
83  // Page size. 0 = use appropriate page size.
84  // (On Linux, we use a huge page size if the file is on a hugetlbfs
85  // file system, and the default page size otherwise)
86  off_t pageSize = 0;
87 
88  // If shared (default), the memory mapping is shared with other processes
89  // mapping the same file (or children); if not shared (private), each
90  // process has its own mapping. Changes in writable, private mappings are
91  // not reflected to the underlying file. See the discussion of
92  // MAP_PRIVATE vs MAP_SHARED in the mmap(2) manual page.
93  bool shared = true;
94 
95  // Populate page tables; subsequent accesses should not be blocked
96  // by page faults. This is a hint, as it may not be supported.
97  bool prefault = false;
98 
99  // Map the pages readable. Note that mapping pages without read permissions
100  // is not universally supported (not supported on hugetlbfs on Linux, for
101  // example)
102  bool readable = true;
103 
104  // Map the pages writable.
105  bool writable = false;
106 
107  // When mapping a file in writable mode, grow the file to the requested
108  // length (using ftruncate()) before mapping; if false, truncate the
109  // mapping to the actual file size instead.
110  bool grow = false;
111 
112  // Fix map at this address, if not nullptr. Must be aligned to a multiple
113  // of the appropriate page size.
114  void* address = nullptr;
115  };
116 
117  // Options to emulate the old WritableMemoryMapping: readable and writable,
118  // allow growing the file if mapping past EOF.
119  static Options writable() {
120  return Options().setWritable(true).setGrow(true);
121  }
122 
125  };
126 
130  MemoryMapping(AnonymousType, off_t length, Options options = Options());
131 
132  explicit MemoryMapping(
133  File file,
134  off_t offset = 0,
135  off_t length = -1,
136  Options options = Options());
137 
138  explicit MemoryMapping(
139  const char* name,
140  off_t offset = 0,
141  off_t length = -1,
142  Options options = Options());
143 
144  explicit MemoryMapping(
145  int fd,
146  off_t offset = 0,
147  off_t length = -1,
148  Options options = Options());
149 
151 
152  ~MemoryMapping();
153 
154  MemoryMapping& operator=(MemoryMapping);
155 
156  void swap(MemoryMapping& other) noexcept;
157 
161  bool mlock(LockMode lock);
162 
168  void munlock(bool dontneed = false);
169 
174  void hintLinearScan();
175 
179  void advise(int advice) const;
180  void advise(int advice, size_t offset, size_t length) const;
181 
186  template <class T>
187  Range<const T*> asRange() const {
188  size_t count = data_.size() / sizeof(T);
189  return Range<const T*>(
190  static_cast<const T*>(static_cast<const void*>(data_.data())), count);
191  }
192 
196  ByteRange range() const {
197  return data_;
198  }
199 
204  template <class T>
206  DCHECK(options_.writable); // you'll segfault anyway...
207  size_t count = data_.size() / sizeof(T);
208  return Range<T*>(static_cast<T*>(static_cast<void*>(data_.data())), count);
209  }
210 
215  DCHECK(options_.writable); // you'll segfault anyway...
216  return data_;
217  }
218 
223  StringPiece data() const {
224  return asRange<const char>();
225  }
226 
227  bool mlocked() const {
228  return locked_;
229  }
230 
231  int fd() const {
232  return file_.fd();
233  }
234 
235  private:
236  MemoryMapping();
237 
238  enum InitFlags {
239  kGrow = 1 << 0,
240  kAnon = 1 << 1,
241  };
242  void init(off_t offset, off_t length);
243 
245  void* mapStart_ = nullptr;
246  off_t mapLength_ = 0;
248  bool locked_ = false;
250 };
251 
253 
264 void alignedForwardMemcpy(void* dest, const void* src, size_t size);
265 
269 void mmapFileCopy(const char* src, const char* dest, mode_t mode = 0666);
270 
271 } // namespace folly
void swap(MemoryMapping &other) noexcept
Options & setPrefault(bool v)
Definition: MemoryMapping.h:66
auto v
MutableByteRange writableRange() const
Options & setReadable(bool v)
Definition: MemoryMapping.h:70
dest
Definition: upload.py:394
constexpr size_type size() const
Definition: Range.h:431
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
void alignedForwardMemcpy(void *dst, const void *src, size_t size)
bool mlock(LockMode lock)
folly::Optional< PskKeyExchangeMode > mode
const char * name
Definition: http_parser.c:437
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
int fd() const
Definition: File.h:85
Range< T * > asWritableRange() const
MutableByteRange data_
constexpr Iter data() const
Definition: Range.h:446
void advise(int advice) const
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
static Options writable()
Options & setPageSize(off_t v)
Definition: MemoryMapping.h:58
Range< const T * > asRange() const
int * count
bool mlocked() const
void munlock(bool dontneed=false)
const
Definition: upload.py:398
Options & setWritable(bool v)
Definition: MemoryMapping.h:74
ByteRange range() const
StringPiece data() const
void init(off_t offset, off_t length)
void mmapFileCopy(const char *src, const char *dest, mode_t mode)
Options & setShared(bool v)
Definition: MemoryMapping.h:62