6 #include <folly/portability/SysMman.h>
7 #include <folly/portability/Unistd.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
39 if (mem == reinterpret_cast<void*>(-1)) {
40 throw std::system_error(errno, std::system_category());
42 #if !defined(MAP_POPULATE) && defined(MADV_WILLNEED)
43 madvise(mem, size, MADV_WILLNEED);
49 void deallocate(void* p, size_t size) {
50 auto len = computeSize(size);
55 template<typename Allocator>
56 struct GivesZeroFilledMemory : public std::false_type {};
59 struct GivesZeroFilledMemory<MMapAlloc> : public std::true_type{};