7 #include <folly/portability/SysMman.h>
9 namespace folly { namespace detail {
13 size_t computeSize(size_t size) {
14 long pagesize = sysconf(_SC_PAGESIZE);
15 size_t mmapLength = ((size - 1) & ~(pagesize - 1)) + pagesize;
16 assert(size <= mmapLength && mmapLength < size + pagesize);
17 assert((mmapLength % pagesize) == 0);
22 void* allocate(size_t size) {
23 auto len = computeSize(size);
25 // MAP_HUGETLB is a perf win, but requires cooperation from the
26 // deployment environment (and a change to computeSize()).
27 void* mem = static_cast<void*>(mmap(
30 PROT_READ | PROT_WRITE,
31 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
34 if (mem == reinterpret_cast<void*>(-1)) {
35 throw std::system_error(errno, std::system_category());
41 void deallocate(void* p, size_t size) {
42 auto len = computeSize(size);
47 template<typename Allocator>
48 struct GivesZeroFilledMemory : public std::false_type {};
51 struct GivesZeroFilledMemory<MMapAlloc> : public std::true_type{};