2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_MEMORYMAPPING_H_
18 #define FOLLY_MEMORYMAPPING_H_
20 #include "folly/FBString.h"
21 #include "folly/File.h"
22 #include "folly/Range.h"
23 #include <glog/logging.h>
24 #include <boost/noncopyable.hpp>
29 * Maps files in memory (read-only).
31 * @author Tudor Bosman (tudorb@fb.com)
33 class MemoryMapping : boost::noncopyable {
36 * Lock the pages in memory?
37 * TRY_LOCK = try to lock, log warning if permission denied
38 * MUST_LOCK = lock, fail assertion if permission denied.
45 * Map a portion of the file indicated by filename in memory, causing a CHECK
48 * By default, map the whole file. length=-1: map from offset to EOF.
49 * Unlike the mmap() system call, offset and length don't need to be
50 * page-aligned. length is clipped to the end of the file if it's too large.
52 * The mapping will be destroyed (and the memory pointed-to by data() will
53 * likely become inaccessible) when the MemoryMapping object is destroyed.
55 explicit MemoryMapping(File file,
60 explicit MemoryMapping(const char* name,
65 explicit MemoryMapping(int fd,
70 MemoryMapping(MemoryMapping&&);
72 virtual ~MemoryMapping();
74 MemoryMapping& operator=(MemoryMapping);
76 void swap(MemoryMapping& other);
79 * Lock the pages in memory
81 bool mlock(LockMode lock);
85 * If dontneed is true, the kernel is instructed to release these pages
86 * (per madvise(MADV_DONTNEED)).
88 void munlock(bool dontneed = false);
91 * Hint that these pages will be scanned linearly.
92 * madvise(MADV_SEQUENTIAL)
94 void hintLinearScan();
97 * Advise the kernel about memory access.
99 void advise(int advice) const;
102 * A bitwise cast of the mapped bytes as range of values. Only intended for
103 * use with POD or in-place usable types.
106 Range<const T*> asRange() const {
107 size_t count = data_.size() / sizeof(T);
108 return Range<const T*>(static_cast<const T*>(
109 static_cast<const void*>(data_.data())),
114 * A range of bytes mapped by this mapping.
116 Range<const uint8_t*> range() const {
117 return {data_.begin(), data_.end()};
121 * Return the memory area where the file was mapped.
123 StringPiece data() const {
124 return asRange<const char>();
127 bool mlocked() const {
131 int fd() const { return file_.fd(); }
137 off_t offset, off_t length,
147 Range<uint8_t*> data_;
151 * Maps files in memory for writing.
153 * @author Tom Jackson (tjackson@fb.com)
155 class WritableMemoryMapping : public MemoryMapping {
157 explicit WritableMemoryMapping(File file,
162 * A bitwise cast of the mapped bytes as range of mutable values. Only
163 * intended for use with POD or in-place usable types.
166 Range<T*> asWritableRange() const {
167 size_t count = data_.size() / sizeof(T);
168 return Range<T*>(static_cast<T*>(
169 static_cast<void*>(data_.data())),
174 * A range of mutable bytes mapped by this mapping.
176 Range<uint8_t*> writableRange() const {
181 void swap(MemoryMapping&, MemoryMapping&);
185 #endif /* FOLLY_MEMORYMAPPING_H_ */