forked from netstao/connect-pool
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpMemory.c
More file actions
executable file
·80 lines (72 loc) · 2.37 KB
/
Copy pathcpMemory.c
File metadata and controls
executable file
·80 lines (72 loc) · 2.37 KB
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
/*
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| original: Tianfeng Han modify: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 cpShareMemory_sysv_create(cpShareMemory *object, int size, int key) {
int shmid;
void *mem = NULL;
bzero(object, sizeof (cpShareMemory));
if (key == 0) {
key = IPC_PRIVATE;
}
if ((shmid = shmget(key, size, SHM_R | SHM_W | IPC_CREAT | 0666)) < 0) {
cpLog("shmget Error: %s[%d]", strerror(errno), errno);
return 0;
}
object->key = key;
object->shmid = shmid;
object->size = size;
object->mem = mem;
return shmid;
}
int cpShareMemory_sysv_free(cpShareMemory *object, int rm) {
int ret = shmdt(object->mem);
if (rm == 1) {
shmctl(object->shmid, IPC_RMID, NULL);
}
object->mem = NULL;
return ret;
}