forked from shdown/lua-shm-state-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping_kind.c
56 lines (50 loc) · 1.2 KB
/
mapping_kind.c
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
49
50
51
52
53
54
55
56
#define _DEFAULT_SOURCE
#include "mapping_kind.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
static
void *
create_mapping_portable(size_t len)
{
int fd = open("/dev/zero", O_RDWR);
if (fd < 0) {
perror("open: /dev/zero");
abort();
}
void *r = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
return r == MAP_FAILED ? NULL : r;
}
#ifdef __linux__
static
void *
create_mapping_ondemand(size_t len)
{
void *r = mmap(
NULL, len, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_NORESERVE, -1, 0);
return r == MAP_FAILED ? NULL : r;
}
static
int
punch_hole(void *addr, size_t len)
{
return madvise(addr, len, MADV_REMOVE);
}
#endif
const MappingKind MAPPING_KIND_PORTABLE = {
.default_len = 1024ull * 1024 * 16,
.create = create_mapping_portable,
};
const MappingKind MAPPING_KIND_ONDEMAND = {
.default_len = 1024ull * 1024 * 1024 * 16,
#ifdef __linux__
.create = create_mapping_ondemand,
.reclaim_pages = punch_hole,
#endif
};
#ifdef __linux__
const MappingKind *mapping_kind_pdefault = &MAPPING_KIND_ONDEMAND;
#else
const MappingKind *mapping_kind_pdefault = &MAPPING_KIND_PORTABLE;
#endif