forked from andikleen/simple-pt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
36 lines (31 loc) · 717 Bytes
/
map.c
File metadata and controls
36 lines (31 loc) · 717 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
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/stat.h>
#define round_up(x, y) (((x) + (y) - 1) & ~((y) - 1))
static int pagesize;
static void __attribute__((constructor)) init_ps(void)
{
pagesize = sysconf(_SC_PAGESIZE);
}
void *mapfile(char *fn, size_t *size)
{
int fd = open(fn, O_RDWR);
if (fd < 0)
return NULL;
struct stat st;
void *map = (void *)-1L;
if (fstat(fd, &st) >= 0) {
*size = st.st_size;
map = mmap(NULL, round_up(st.st_size, pagesize),
PROT_READ|PROT_WRITE,
MAP_PRIVATE, fd, 0);
}
close(fd);
return map != (void *)-1L ? map : NULL;
}
void unmapfile(void *map, size_t size)
{
munmap(map, round_up(size, pagesize));
}