forked from swoole/php-cp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpMemory.c
executable file
·101 lines (88 loc) · 2.56 KB
/
cpMemory.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
+----------------------------------------------------------------------+
| common con pool |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Xinhua Guo <woshiguo35@sina.com>|
+----------------------------------------------------------------------+
*/
#include "php_connect_pool.h"
void *cp_mmap_calloc(int size)
{
void *mem;
int tmpfd = -1;
int flag = MAP_SHARED;
#ifdef MAP_ANONYMOUS
flag |= MAP_ANONYMOUS;
#else
char *mapfile = NULL;
if (mapfile == NULL)
{
mapfile = "/dev/zero";
}
if ((tmpfd = open(mapfile, O_RDWR)) < 0)
{
return NULL;
}
strncpy(object->mapfile, mapfile, SW_SHM_MMAP_FILE_LEN);
object->tmpfd = tmpfd;
#endif
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flag, tmpfd, 0);
#ifdef MAP_FAILED
if (mem == MAP_FAILED)
#else
if (!mem)
#endif
{
cpLog("mmap fail. Error: %s[%d]", strerror(errno), errno);
return NULL;
}
else
{
bzero(mem, size);
return mem;
}
}
int cp_create_mmap_file(cpShareMemory *object){
umask(0);
int fd = open(object->mmap_name, O_RDWR | O_CREAT,S_IROTH|S_IWOTH);
if (fd == -1)
{
cpLog("open fail. Error: %s[%d]", strerror(errno), errno);
return - 1;
}
ftruncate(fd, object->size);//extend 黑洞
return 0;
};
void* cp_mmap_calloc_with_file(cpShareMemory *object)
{
int fd = open(object->mmap_name, O_RDWR );
if (fd == -1)
{
cpLog("open fail. Error: %s[%d]", strerror(errno), errno);
return NULL;
}
void *mem = mmap(NULL, object->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
#ifdef MAP_FAILED
if (mem == MAP_FAILED)
#else
if (!mem)
#endif
{
cpLog("mmap fail. Error: %s[%d]", strerror(errno), errno);
return NULL;
}
else
{
// bzero(mem, object->size);
object->mem = mem;
return mem;
}
}