-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathncx_shm.c
68 lines (51 loc) · 1.02 KB
/
ncx_shm.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
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "ncx_shm.h"
#ifdef MAP_ANON
int
ncx_shm_alloc(ncx_shm_t *shm)
{
shm->addr = (void *) mmap(NULL, shm->size,
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED,
-1, 0);
if (shm->addr == NULL) {
return -1;
}
return 0;
}
void
ncx_shm_free(ncx_shm_t *shm)
{
if (shm->addr) {
munmap((void *) shm->addr, shm->size);
}
}
#else
int
ncx_shm_alloc(ncx_shm_t *shm)
{
ngx_fd_t fd;
fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
return -1;
}
shm->addr = (void *) mmap(NULL, shm->size,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (shm->addr == NULL) {
return -1;
}
return 0;
}
void
ncx_shm_free(ncx_shm_t *shm)
{
if (shm->addr) {
munmap((void *) shm->addr, shm->size);
}
}
#endif