/src/php-src/ext/opcache/zend_shared_alloc.h
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend OPcache | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © The PHP Group and Contributors. | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to the Modified BSD License that is | |
8 | | | bundled with this package in the file LICENSE, and is available | |
9 | | | through the World Wide Web at <https://www.php.net/license/>. | |
10 | | | | |
11 | | | SPDX-License-Identifier: BSD-3-Clause | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Andi Gutmans <andi@php.net> | |
14 | | | Zeev Suraski <zeev@php.net> | |
15 | | | Stanislav Malyshev <stas@zend.com> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #ifndef ZEND_SHARED_ALLOC_H |
21 | | #define ZEND_SHARED_ALLOC_H |
22 | | |
23 | | #include "zend.h" |
24 | | #include "ZendAccelerator.h" |
25 | | |
26 | | #if defined(__APPLE__) && defined(__MACH__) /* darwin */ |
27 | | # ifdef HAVE_SHM_MMAP_POSIX |
28 | | # define USE_SHM_OPEN 1 |
29 | | # endif |
30 | | # ifdef HAVE_SHM_MMAP_ANON |
31 | | # define USE_MMAP 1 |
32 | | # endif |
33 | | #elif defined(__linux__) || defined(_AIX) |
34 | | # ifdef HAVE_SHM_MMAP_POSIX |
35 | | # define USE_SHM_OPEN 1 |
36 | | # endif |
37 | | # ifdef HAVE_SHM_IPC |
38 | | # define USE_SHM 1 |
39 | | # endif |
40 | | # ifdef HAVE_SHM_MMAP_ANON |
41 | | # define USE_MMAP 1 |
42 | | # endif |
43 | | #elif defined(__sparc) || defined(__sun) |
44 | | # ifdef HAVE_SHM_MMAP_POSIX |
45 | | # define USE_SHM_OPEN 1 |
46 | | # endif |
47 | | # ifdef HAVE_SHM_IPC |
48 | | # define USE_SHM 1 |
49 | | # endif |
50 | | # if defined(__i386) |
51 | | # ifdef HAVE_SHM_MMAP_ANON |
52 | | # define USE_MMAP 1 |
53 | | # endif |
54 | | # endif |
55 | | #else |
56 | | # ifdef HAVE_SHM_MMAP_POSIX |
57 | | # define USE_SHM_OPEN 1 |
58 | | # endif |
59 | | # ifdef HAVE_SHM_MMAP_ANON |
60 | | # define USE_MMAP 1 |
61 | | # endif |
62 | | # ifdef HAVE_SHM_IPC |
63 | | # define USE_SHM 1 |
64 | | # endif |
65 | | #endif |
66 | | |
67 | 2 | #define ALLOC_FAILURE 0 |
68 | 4 | #define ALLOC_SUCCESS 1 |
69 | 2 | #define FAILED_REATTACHED 2 |
70 | 2 | #define SUCCESSFULLY_REATTACHED 4 |
71 | | #define ALLOC_FAIL_MAPPING 8 |
72 | | #define ALLOC_FALLBACK 9 |
73 | 0 | #define NO_SHM_BACKEND 10 |
74 | | |
75 | | typedef struct _zend_shared_segment { |
76 | | size_t size; |
77 | | size_t end; |
78 | | size_t pos; /* position for simple stack allocator */ |
79 | | void *p; |
80 | | } zend_shared_segment; |
81 | | |
82 | | typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, const char **error_in); |
83 | | typedef int (*detach_segment_t)(zend_shared_segment *shared_segment); |
84 | | |
85 | | typedef struct { |
86 | | create_segments_t create_segments; |
87 | | detach_segment_t detach_segment; |
88 | | size_t (*segment_type_size)(void); |
89 | | } zend_shared_memory_handlers; |
90 | | |
91 | | typedef struct _handler_entry { |
92 | | const char *name; |
93 | | const zend_shared_memory_handlers *handler; |
94 | | } zend_shared_memory_handler_entry; |
95 | | |
96 | | typedef struct _zend_shared_memory_state { |
97 | | size_t *positions; /* current positions for each segment */ |
98 | | size_t shared_free; /* amount of free shared memory */ |
99 | | } zend_shared_memory_state; |
100 | | |
101 | | typedef struct _zend_smm_shared_globals { |
102 | | /* Shared Memory Manager */ |
103 | | zend_shared_segment **shared_segments; |
104 | | /* Number of allocated shared segments */ |
105 | | int shared_segments_count; |
106 | | /* Amount of free shared memory */ |
107 | | size_t shared_free; |
108 | | /* Amount of shared memory allocated by garbage */ |
109 | | size_t wasted_shared_memory; |
110 | | /* No more shared memory flag */ |
111 | | bool memory_exhausted; |
112 | | /* Saved Shared Allocator State */ |
113 | | zend_shared_memory_state shared_memory_state; |
114 | | /* Pointer to the application's shared data structures */ |
115 | | void *app_shared_globals; |
116 | | /* Reserved shared memory */ |
117 | | void *reserved; |
118 | | size_t reserved_size; |
119 | | } zend_smm_shared_globals; |
120 | | |
121 | | ZEND_EXT_API extern zend_smm_shared_globals *smm_shared_globals; |
122 | | |
123 | 150 | #define ZSMMG(element) (smm_shared_globals->element) |
124 | | |
125 | | #define SHARED_ALLOC_REATTACHED (SUCCESS+1) |
126 | | |
127 | | BEGIN_EXTERN_C() |
128 | | |
129 | | int zend_shared_alloc_startup(size_t requested_size, size_t reserved_size); |
130 | | void zend_shared_alloc_shutdown(void); |
131 | | |
132 | | /* allocate shared memory block */ |
133 | | void *zend_shared_alloc(size_t size); |
134 | | |
135 | | /** |
136 | | * Wrapper for zend_shared_alloc() which aligns at 64-byte boundary if |
137 | | * AVX or SSE2 are used. |
138 | | */ |
139 | 0 | static inline void *zend_shared_alloc_aligned(size_t size) { |
140 | 0 | #if defined(__AVX__) || defined(__SSE2__) |
141 | | /* Align to 64-byte boundary */ |
142 | 0 | void *p = zend_shared_alloc(size + 64); |
143 | 0 | return (void *)(((uintptr_t)p + 63L) & ~63L); |
144 | | #else |
145 | | return zend_shared_alloc(size); |
146 | | #endif |
147 | 0 | } Unexecuted instantiation: shared_alloc_mmap.c:zend_shared_alloc_aligned Unexecuted instantiation: shared_alloc_posix.c:zend_shared_alloc_aligned Unexecuted instantiation: shared_alloc_shm.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_accelerator_hash.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_accelerator_module.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_accelerator_util_funcs.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_file_cache.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_persist_calc.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_persist.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_shared_alloc.c:zend_shared_alloc_aligned Unexecuted instantiation: ZendAccelerator.c:zend_shared_alloc_aligned Unexecuted instantiation: zend_jit.c:zend_shared_alloc_aligned |
148 | | |
149 | | /* copy into shared memory */ |
150 | | void *zend_shared_memdup_get_put_free(void *source, size_t size); |
151 | | void *zend_shared_memdup_put_free(void *source, size_t size); |
152 | | void *zend_shared_memdup_free(void *source, size_t size); |
153 | | void *zend_shared_memdup_get_put(void *source, size_t size); |
154 | | void *zend_shared_memdup_put(void *source, size_t size); |
155 | | void *zend_shared_memdup(void *source, size_t size); |
156 | | |
157 | | int zend_shared_memdup_size(void *p, size_t size); |
158 | | |
159 | | bool zend_accel_in_shm(void *ptr); |
160 | | |
161 | | typedef union _align_test { |
162 | | void *ptr; |
163 | | double dbl; |
164 | | zend_long lng; |
165 | | } align_test; |
166 | | |
167 | | #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L |
168 | | # define PLATFORM_ALIGNMENT (alignof(align_test) < 8 ? 8 : alignof(align_test)) |
169 | | #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L |
170 | | # define PLATFORM_ALIGNMENT (_Alignof(align_test) < 8 ? 8 : _Alignof(align_test)) |
171 | | #elif ZEND_GCC_VERSION >= 2000 || defined(__clang__) |
172 | | # define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test)) |
173 | | #else |
174 | | # define PLATFORM_ALIGNMENT (sizeof(align_test)) |
175 | | #endif |
176 | | |
177 | | #define ZEND_ALIGNED_SIZE(size) \ |
178 | 12 | ZEND_MM_ALIGNED_SIZE_EX(size, PLATFORM_ALIGNMENT) |
179 | | |
180 | | /* exclusive locking */ |
181 | | void zend_shared_alloc_lock(void); |
182 | | void zend_shared_alloc_unlock(void); /* returns the allocated size during lock..unlock */ |
183 | | void zend_shared_alloc_safe_unlock(void); |
184 | | |
185 | | /* old/new mapping functions */ |
186 | | void zend_shared_alloc_init_xlat_table(void); |
187 | | void zend_shared_alloc_destroy_xlat_table(void); |
188 | | void zend_shared_alloc_clear_xlat_table(void); |
189 | | uint32_t zend_shared_alloc_checkpoint_xlat_table(void); |
190 | | void zend_shared_alloc_restore_xlat_table(uint32_t checkpoint); |
191 | | void zend_shared_alloc_register_xlat_entry(const void *key, const void *value); |
192 | | void *zend_shared_alloc_get_xlat_entry(const void *key); |
193 | | |
194 | | size_t zend_shared_alloc_get_free_memory(void); |
195 | | void zend_shared_alloc_save_state(void); |
196 | | void zend_shared_alloc_restore_state(void); |
197 | | const char *zend_accel_get_shared_model(void); |
198 | | |
199 | | /** |
200 | | * Memory write protection |
201 | | * |
202 | | * @param protected true to protect shared memory (read-only), false |
203 | | * to unprotect shared memory (writable) |
204 | | */ |
205 | | void zend_accel_shared_protect(bool protected); |
206 | | |
207 | | #ifdef USE_MMAP |
208 | | extern const zend_shared_memory_handlers zend_alloc_mmap_handlers; |
209 | | #endif |
210 | | |
211 | | #ifdef USE_SHM |
212 | | extern const zend_shared_memory_handlers zend_alloc_shm_handlers; |
213 | | #endif |
214 | | |
215 | | #ifdef USE_SHM_OPEN |
216 | | extern const zend_shared_memory_handlers zend_alloc_posix_handlers; |
217 | | #endif |
218 | | |
219 | | #ifdef ZEND_WIN32 |
220 | | extern const zend_shared_memory_handlers zend_alloc_win32_handlers; |
221 | | void zend_shared_alloc_create_lock(void); |
222 | | void zend_shared_alloc_lock_win32(void); |
223 | | void zend_shared_alloc_unlock_win32(void); |
224 | | #endif |
225 | | |
226 | | END_EXTERN_C() |
227 | | |
228 | | #endif /* ZEND_SHARED_ALLOC_H */ |