2 * Copyright 2016 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 #include <folly/portability/SysMman.h>
21 #include <folly/Portability.h>
22 #include <folly/portability/Windows.h>
24 static bool mmap_to_page_protection(int prot, DWORD& ret, DWORD& acc) {
25 if (prot == PROT_NONE) {
28 } else if (prot == PROT_READ) {
31 } else if (prot == PROT_EXEC) {
33 acc = FILE_MAP_EXECUTE;
34 } else if (prot == (PROT_READ | PROT_EXEC)) {
35 ret = PAGE_EXECUTE_READ;
36 acc = FILE_MAP_READ | FILE_MAP_EXECUTE;
37 } else if (prot == (PROT_READ | PROT_WRITE)) {
39 acc = FILE_MAP_READ | FILE_MAP_WRITE;
40 } else if (prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) {
41 ret = PAGE_EXECUTE_READWRITE;
42 acc = FILE_MAP_READ | FILE_MAP_WRITE | FILE_MAP_EXECUTE;
49 static size_t alignToAllocationGranularity(size_t s) {
50 static size_t granularity = [] {
51 static SYSTEM_INFO inf;
53 return inf.dwAllocationGranularity;
55 return (s + granularity - 1) / granularity * granularity;
59 int madvise(const void* addr, size_t len, int advise) {
60 // We do nothing at all.
61 // Could probably implement dontneed via VirtualAlloc
62 // with the MEM_RESET and MEM_RESET_UNDO flags.
66 int mlock(const void* addr, size_t len) {
67 // For some strange reason, it's allowed to
68 // lock a nullptr as long as length is zero.
69 // VirtualLock doesn't allow it, so handle
71 if (addr == nullptr && len == 0) {
74 if (!VirtualLock((void*)addr, len)) {
80 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
81 // Make sure it's something we support first.
84 if ((flags & (MAP_ANONYMOUS | MAP_SHARED)) == (MAP_ANONYMOUS | MAP_SHARED)) {
87 // No private copy on write.
88 if ((flags & MAP_PRIVATE) == MAP_PRIVATE && fd != -1) {
91 // Map isn't anon, must be file backed.
92 if (!(flags & MAP_ANONYMOUS) && fd == -1) {
98 if (!mmap_to_page_protection(prot, newProt, accessFlags)) {
103 if (!(flags & MAP_ANONYMOUS) || (flags & MAP_SHARED)) {
104 HANDLE h = INVALID_HANDLE_VALUE;
105 if (!(flags & MAP_ANONYMOUS)) {
106 h = (HANDLE)_get_osfhandle(fd);
109 HANDLE fmh = CreateFileMapping(
113 (DWORD)((length >> 32) & 0xFFFFFFFF),
114 (DWORD)(length & 0xFFFFFFFF),
116 if (fmh == nullptr) {
119 ret = MapViewOfFileEx(
122 (DWORD)((off >> 32) & 0xFFFFFFFF),
123 (DWORD)(off & 0xFFFFFFFF),
126 if (ret == nullptr) {
131 // VirtualAlloc rounds size down to a multiple
132 // of the system allocation granularity :(
133 length = alignToAllocationGranularity(length);
134 ret = VirtualAlloc(addr, length, MEM_COMMIT | MEM_RESERVE, newProt);
135 if (ret == nullptr) {
140 // TODO: Could technically implement MAP_POPULATE via PrefetchVirtualMemory
141 // Should also see about implementing MAP_NORESERVE
145 int mprotect(void* addr, size_t size, int prot) {
148 if (!mmap_to_page_protection(prot, newProt, access)) {
153 BOOL res = VirtualProtect(addr, size, newProt, &oldProt);
160 int munlock(const void* addr, size_t length) {
161 // See comment in mlock
162 if (addr == nullptr && length == 0) {
165 if (!VirtualUnlock((void*)addr, length)) {
171 int munmap(void* addr, size_t length) {
172 // Try to unmap it as a file, otherwise VirtualFree.
173 if (!UnmapViewOfFile(addr)) {
174 if (folly::kIsDebug) {
175 // We can't do partial unmapping with Windows, so
176 // assert that we aren't trying to do that if we're
178 MEMORY_BASIC_INFORMATION inf;
179 VirtualQuery(addr, &inf, sizeof(inf));
180 assert(inf.BaseAddress == addr);
181 assert(inf.RegionSize == alignToAllocationGranularity(length));
183 if (!VirtualFree(addr, 0, MEM_RELEASE)) {