-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpages.cpp
More file actions
43 lines (32 loc) · 872 Bytes
/
pages.cpp
File metadata and controls
43 lines (32 loc) · 872 Bytes
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
#include <sys/mman.h>
#include <errno.h>
#include "pages.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;
return ptr;
}
void PageFree(void* ptr, size_t size)
{
ASSERT((size & PAGE_MASK) == 0);
int ret = munmap(ptr, size);
(void)ret; // suppress unused variable warning
int err = errno;
(void)err;
ASSERT(ret == 0);
}