-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpages.cpp
48 lines (37 loc) · 1.06 KB
/
pages.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (C) 2019 Ricardo Leite. All rights reserved.
* Licenced under the MIT licence. See COPYING file in the project root for
* details.
*/
#include "pages.h"
#include <sys/mman.h>
#include "log.h"
void* PageAlloc(size_t size)
{
ASSERT((size & PAGE_MASK) == 0);
void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (ptr == MAP_FAILED) {
ptr = nullptr;
}
return ptr;
}
void* PageAllocOvercommit(size_t size)
{
ASSERT((size & PAGE_MASK) == 0);
// use no MAP_NORESERVE to skip OS overcommit limits
void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, -1, 0);
if (ptr == MAP_FAILED) {
ptr = nullptr;
} else {
// exclude such large maps from core dumps as they become unusable otherwise
madvise(ptr, size, MADV_DONTDUMP);
}
return ptr;
}
void PageFree(void* ptr, size_t size)
{
ASSERT((size & PAGE_MASK) == 0);
int ret = munmap(ptr, size);
(void)ret; // suppress warning
ASSERT(ret == 0);
}