-
Notifications
You must be signed in to change notification settings - Fork 507
/
shm.c
53 lines (43 loc) · 867 Bytes
/
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
#include "shm.h"
#ifdef PHP_WIN32
#include <Windows.h>
#else
#include <sys/mman.h>
#ifndef MAP_NOSYNC
#define MAP_NOSYNC 0
#endif
#endif
void *beast_shm_alloc(size_t size)
{
void *p;
#ifdef PHP_WIN32
HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL, PAGE_READWRITE, 0, size, NULL);
if (hMapFile == INVALID_HANDLE_VALUE) {
return NULL;
}
p = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
size);
CloseHandle(hMapFile);
#else
p = mmap(NULL,
size,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_ANON,
-1,
0);
#endif
return p;
}
int beast_shm_free(void *p, size_t size)
{
#ifdef PHP_WIN32
return UnmapViewOfFile(p) ? 0 : -1;
#else
return munmap(p, size);
#endif
}