-
Notifications
You must be signed in to change notification settings - Fork 67
/
map.c
45 lines (40 loc) · 969 Bytes
/
map.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
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "map.h"
#define roundup(x,y) (((x) + (y) - 1) & ~((y) - 1))
char *mapfile_flag(char *file, int oflags, size_t *size, int flag)
{
int fd = open(file, oflags, 0644);
if (fd < 0)
return NULL;
struct stat st;
if (fstat(fd, &st) >= 0 && (*size = st.st_size) > 0) {
size_t ps = sysconf(_SC_PAGE_SIZE);
*size = roundup(st.st_size, ps);
int prot = PROT_READ;
if ((oflags & O_WRONLY) || (flag & MAP_PRIVATE))
prot |= PROT_WRITE;
char *map = mmap(NULL, *size, prot,
flag,
fd, 0);
close(fd);
if (map == (char *)-1)
return NULL;
*size = st.st_size;
return map;
}
close(fd);
return NULL;
}
char *mapfile(char *file, int oflags, size_t *size)
{
return mapfile_flag(file, oflags, size, MAP_SHARED);
}
void unmap_file(char *map, size_t size)
{
size_t ps = sysconf(_SC_PAGE_SIZE);
munmap(map, roundup(size, ps));
}