1 /*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2016 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 | Stanislav Malyshev <stas@zend.com> |
18 | Dmitry Stogov <dmitry@zend.com> |
19 +----------------------------------------------------------------------+
20 */
21
22 #include "zend_shared_alloc.h"
23
24 #ifdef USE_MMAP
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/mman.h>
31
32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
33 # define MAP_ANONYMOUS MAP_ANON
34 #endif
35
create_segments(size_t requested_size,zend_shared_segment *** shared_segments_p,int * shared_segments_count,char ** error_in)36 static int create_segments(size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
37 {
38 zend_shared_segment *shared_segment;
39
40 *shared_segments_count = 1;
41 *shared_segments_p = (zend_shared_segment **) calloc(1, sizeof(zend_shared_segment) + sizeof(void *));
42 if (!*shared_segments_p) {
43 *error_in = "calloc";
44 return ALLOC_FAILURE;
45 }
46 shared_segment = (zend_shared_segment *)((char *)(*shared_segments_p) + sizeof(void *));
47 (*shared_segments_p)[0] = shared_segment;
48
49 shared_segment->p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
50 if (shared_segment->p == MAP_FAILED) {
51 *error_in = "mmap";
52 return ALLOC_FAILURE;
53 }
54
55 shared_segment->pos = 0;
56 shared_segment->size = requested_size;
57
58 return ALLOC_SUCCESS;
59 }
60
detach_segment(zend_shared_segment * shared_segment)61 static int detach_segment(zend_shared_segment *shared_segment)
62 {
63 munmap(shared_segment->p, shared_segment->size);
64 return 0;
65 }
66
segment_type_size(void)67 static size_t segment_type_size(void)
68 {
69 return sizeof(zend_shared_segment);
70 }
71
72 zend_shared_memory_handlers zend_alloc_mmap_handlers = {
73 create_segments,
74 detach_segment,
75 segment_type_size
76 };
77
78 #endif /* USE_MMAP */
79