php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
shared_alloc_posix.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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 | https://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@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Stanislav Malyshev <stas@zend.com> |
18 | Dmitry Stogov <dmitry@php.net> |
19 +----------------------------------------------------------------------+
20*/
21
22#include "zend_shared_alloc.h"
23
24#ifdef USE_SHM_OPEN
25
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <stdio.h>
29#include <fcntl.h>
30#include <sys/mman.h>
31#include <unistd.h>
32#include <stdlib.h>
33
34typedef struct {
36 int shm_fd;
37} zend_shared_segment_posix;
38
39static int create_segments(size_t requested_size, zend_shared_segment_posix ***shared_segments_p, int *shared_segments_count, const char **error_in)
40{
41 zend_shared_segment_posix *shared_segment;
42 char shared_segment_name[sizeof("/ZendAccelerator.") + 20];
43 int shared_segment_flags = O_RDWR|O_CREAT|O_TRUNC;
44 mode_t shared_segment_mode = 0600;
45
46#if defined(HAVE_SHM_CREATE_LARGEPAGE)
54 size_t i, shared_segment_sizes = 0, shared_segment_lg_index = 0;
55 size_t shared_segment_sindexes[3] = {0};
56 const size_t entries = sizeof(shared_segment_sindexes) / sizeof(shared_segment_sindexes[0]);
57
58 shared_segment_sizes = getpagesizes(shared_segment_sindexes, entries);
59
60 if (shared_segment_sizes > 0) {
61 for (i = shared_segment_sizes - 1; i >= 0; i --) {
62 if (shared_segment_sindexes[i] != 0 &&
63 !(requested_size % shared_segment_sindexes[i])) {
64 shared_segment_lg_index = i;
65 break;
66 }
67 }
68 }
69#endif
70
71 *shared_segments_count = 1;
72 *shared_segments_p = (zend_shared_segment_posix **) calloc(1, sizeof(zend_shared_segment_posix) + sizeof(void *));
73 if (!*shared_segments_p) {
74 *error_in = "calloc";
75 return ALLOC_FAILURE;
76 }
77 shared_segment = (zend_shared_segment_posix *)((char *)(*shared_segments_p) + sizeof(void *));
78 (*shared_segments_p)[0] = shared_segment;
79
80 snprintf(shared_segment_name, sizeof(shared_segment_name), "/ZendAccelerator.%d", getpid());
81#if defined(HAVE_SHM_CREATE_LARGEPAGE)
82 if (shared_segment_lg_index > 0) {
83 shared_segment->shm_fd = shm_create_largepage(shared_segment_name, shared_segment_flags, shared_segment_lg_index, SHM_LARGEPAGE_ALLOC_DEFAULT, shared_segment_mode);
84 if (shared_segment->shm_fd != -1) {
85 goto truncate_segment;
86 }
87 }
88#endif
89
90 shared_segment->shm_fd = shm_open(shared_segment_name, shared_segment_flags, shared_segment_mode);
91 if (shared_segment->shm_fd == -1) {
92 *error_in = "shm_open";
93 return ALLOC_FAILURE;
94 }
95
96#if defined(HAVE_SHM_CREATE_LARGEPAGE)
97truncate_segment:
98#endif
99 if (ftruncate(shared_segment->shm_fd, requested_size) != 0) {
100 *error_in = "ftruncate";
101 shm_unlink(shared_segment_name);
102 return ALLOC_FAILURE;
103 }
104
105 shared_segment->common.p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_segment->shm_fd, 0);
106 if (shared_segment->common.p == MAP_FAILED) {
107 *error_in = "mmap";
108 shm_unlink(shared_segment_name);
109 return ALLOC_FAILURE;
110 }
111 shm_unlink(shared_segment_name);
112
113 shared_segment->common.pos = 0;
114 shared_segment->common.size = requested_size;
115
116 return ALLOC_SUCCESS;
117}
118
119static int detach_segment(zend_shared_segment_posix *shared_segment)
120{
121 munmap(shared_segment->common.p, shared_segment->common.size);
122 close(shared_segment->shm_fd);
123 return 0;
124}
125
126static size_t segment_type_size(void)
127{
128 return sizeof(zend_shared_segment_posix);
129}
130
131const zend_shared_memory_handlers zend_alloc_posix_handlers = {
132 (create_segments_t)create_segments,
133 (detach_segment_t)detach_segment,
134 segment_type_size
135};
136
137#endif /* USE_SHM_OPEN */
ftruncate($stream, int $size)
unsigned short mode_t
Definition ioutil.h:72
#define PROT_READ
Definition phpdbg_win.h:26
#define PROT_WRITE
Definition phpdbg_win.h:27
#define close(a)
#define MAP_FAILED
Definition zend_alloc.c:98
#define snprintf
struct _zend_shared_segment zend_shared_segment
int(* detach_segment_t)(zend_shared_segment *shared_segment)
#define ALLOC_SUCCESS
#define ALLOC_FAILURE
int(* create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, const char **error_in)