php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
fpm_shm.c
Go to the documentation of this file.
1 /* (c) 2007,2008 Andrei Nigmatulin, Jerome Loyet */
2
3#include <sys/mman.h>
4#include <errno.h>
5#include <string.h>
6
7#include "fpm_shm.h"
8#include "zlog.h"
9
10
11/* MAP_ANON is deprecated, but not in macOS */
12#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
13#define MAP_ANONYMOUS MAP_ANON
14#endif
15
16static size_t fpm_shm_size = 0;
17
18void *fpm_shm_alloc(size_t size) /* {{{ */
19{
20 void *mem;
21
22 mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
23
24#ifdef MAP_FAILED
25 if (mem == MAP_FAILED) {
26 zlog(ZLOG_SYSERROR, "unable to allocate %zu bytes in shared memory: %s", size, strerror(errno));
27 return NULL;
28 }
29#endif
30
31 if (!mem) {
32 zlog(ZLOG_SYSERROR, "unable to allocate %zu bytes in shared memory", size);
33 return NULL;
34 }
35
36 fpm_shm_size += size;
37 return mem;
38}
39/* }}} */
40
41int fpm_shm_free(void *mem, size_t size) /* {{{ */
42{
43 if (!mem) {
44 zlog(ZLOG_ERROR, "mem is NULL");
45 return 0;
46 }
47
48 if (munmap(mem, size) == -1) {
49 zlog(ZLOG_SYSERROR, "Unable to free shm");
50 return 0;
51 }
52
53 if (fpm_shm_size > size) {
54 fpm_shm_size -= size;
55 } else {
56 fpm_shm_size = 0;
57 }
58
59 return 1;
60}
61/* }}} */
62
64{
65 return fpm_shm_size;
66}
new_type size
Definition ffi.c:4365
size_t fpm_shm_get_size_allocated(void)
Definition fpm_shm.c:63
int fpm_shm_free(void *mem, size_t size)
Definition fpm_shm.c:41
void * fpm_shm_alloc(size_t size)
Definition fpm_shm.c:18
#define NULL
Definition gdcache.h:45
#define PROT_READ
Definition phpdbg_win.h:26
#define PROT_WRITE
Definition phpdbg_win.h:27
#define errno
#define MAP_FAILED
Definition zend_alloc.c:98
#define ZLOG_SYSERROR
Definition zlog.h:53
@ ZLOG_ERROR
Definition zlog.h:45
#define zlog(flags,...)
Definition zlog.h:9