projects
/
folly.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
logging: add a FileHandlerFactory class
[folly.git]
/
folly
/
portability
/
SysMman.cpp
diff --git
a/folly/portability/SysMman.cpp
b/folly/portability/SysMman.cpp
old mode 100755
(executable)
new mode 100644
(file)
index
38d56ae
..
af76d8b
--- a/
folly/portability/SysMman.cpp
+++ b/
folly/portability/SysMman.cpp
@@
-1,5
+1,5
@@
/*
/*
- * Copyright 201
6
Facebook, Inc.
+ * Copyright 201
7
Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@
-17,7
+17,9
@@
#include <folly/portability/SysMman.h>
#ifdef _WIN32
#include <folly/portability/SysMman.h>
#ifdef _WIN32
+
#include <cassert>
#include <cassert>
+
#include <folly/Portability.h>
#include <folly/portability/Windows.h>
#include <folly/Portability.h>
#include <folly/portability/Windows.h>
@@
-56,7
+58,7
@@
static size_t alignToAllocationGranularity(size_t s) {
}
extern "C" {
}
extern "C" {
-int madvise(const void*
addr, size_t len, int advise
) {
+int madvise(const void*
/* addr */, size_t /* len */, int /* advise */
) {
// We do nothing at all.
// Could probably implement dontneed via VirtualAlloc
// with the MEM_RESET and MEM_RESET_UNDO flags.
// We do nothing at all.
// Could probably implement dontneed via VirtualAlloc
// with the MEM_RESET and MEM_RESET_UNDO flags.
@@
-77,6
+79,14
@@
int mlock(const void* addr, size_t len) {
return 0;
}
return 0;
}
+namespace {
+constexpr uint32_t kMMapLengthMagic = 0xFACEB00C;
+struct MemMapDebugTrailer {
+ size_t length;
+ uint32_t magic;
+};
+}
+
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
// Make sure it's something we support first.
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
// Make sure it's something we support first.
@@
-119,7
+129,7
@@
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
ret = MapViewOfFileEx(
fmh,
accessFlags,
ret = MapViewOfFileEx(
fmh,
accessFlags,
- (DWORD)(
(off >> 32) & 0xFFFFFFFF),
+ (DWORD)(
0), // off_t is only 32-bit :(
(DWORD)(off & 0xFFFFFFFF),
0,
addr);
(DWORD)(off & 0xFFFFFFFF),
0,
addr);
@@
-128,6
+138,14
@@
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
}
CloseHandle(fmh);
} else {
}
CloseHandle(fmh);
} else {
+ auto baseLength = length;
+ if (folly::kIsDebug) {
+ // In debug mode we keep track of the length to make
+ // sure you're only munmapping the entire thing if
+ // we're using VirtualAlloc.
+ length += sizeof(MemMapDebugTrailer);
+ }
+
// VirtualAlloc rounds size down to a multiple
// of the system allocation granularity :(
length = alignToAllocationGranularity(length);
// VirtualAlloc rounds size down to a multiple
// of the system allocation granularity :(
length = alignToAllocationGranularity(length);
@@
-135,6
+153,12
@@
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
if (ret == nullptr) {
return MAP_FAILED;
}
if (ret == nullptr) {
return MAP_FAILED;
}
+
+ if (folly::kIsDebug) {
+ auto deb = (MemMapDebugTrailer*)((char*)ret + baseLength);
+ deb->length = baseLength;
+ deb->magic = kMMapLengthMagic;
+ }
}
// TODO: Could technically implement MAP_POPULATE via PrefetchVirtualMemory
}
// TODO: Could technically implement MAP_POPULATE via PrefetchVirtualMemory
@@
-177,8
+201,11
@@
int munmap(void* addr, size_t length) {
// in debug mode.
MEMORY_BASIC_INFORMATION inf;
VirtualQuery(addr, &inf, sizeof(inf));
// in debug mode.
MEMORY_BASIC_INFORMATION inf;
VirtualQuery(addr, &inf, sizeof(inf));
- assert(inf.BaseAddress == addr);
- assert(inf.RegionSize == alignToAllocationGranularity(length));
+ assert(inf.AllocationBase == addr);
+
+ auto deb = (MemMapDebugTrailer*)((char*)addr + length);
+ assert(deb->length == length);
+ assert(deb->magic == kMMapLengthMagic);
}
if (!VirtualFree(addr, 0, MEM_RELEASE)) {
return -1;
}
if (!VirtualFree(addr, 0, MEM_RELEASE)) {
return -1;
@@
-188,4
+215,5
@@
int munmap(void* addr, size_t length) {
return 0;
}
}
return 0;
}
}
+
#endif
#endif