xref: /PHP-8.1/ext/opcache/zend_shared_alloc.h (revision e0ca4dca)
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 #ifndef ZEND_SHARED_ALLOC_H
23 #define ZEND_SHARED_ALLOC_H
24 
25 #include "zend.h"
26 #include "ZendAccelerator.h"
27 
28 #if defined(__APPLE__) && defined(__MACH__) /* darwin */
29 # ifdef HAVE_SHM_MMAP_POSIX
30 #  define USE_SHM_OPEN  1
31 # endif
32 # ifdef HAVE_SHM_MMAP_ANON
33 #  define USE_MMAP      1
34 # endif
35 #elif defined(__linux__) || defined(_AIX)
36 # ifdef HAVE_SHM_MMAP_POSIX
37 #  define USE_SHM_OPEN  1
38 # endif
39 # ifdef HAVE_SHM_IPC
40 #  define USE_SHM       1
41 # endif
42 # ifdef HAVE_SHM_MMAP_ANON
43 #  define USE_MMAP      1
44 # endif
45 #elif defined(__sparc) || defined(__sun)
46 # ifdef HAVE_SHM_MMAP_POSIX
47 #  define USE_SHM_OPEN  1
48 # endif
49 # ifdef HAVE_SHM_IPC
50 #  define USE_SHM       1
51 # endif
52 # if defined(__i386)
53 #  ifdef HAVE_SHM_MMAP_ANON
54 #   define USE_MMAP     1
55 #  endif
56 # endif
57 #else
58 # ifdef HAVE_SHM_MMAP_POSIX
59 #  define USE_SHM_OPEN  1
60 # endif
61 # ifdef HAVE_SHM_MMAP_ANON
62 #  define USE_MMAP      1
63 # endif
64 # ifdef HAVE_SHM_IPC
65 #  define USE_SHM       1
66 # endif
67 #endif
68 
69 #define ALLOC_FAILURE           0
70 #define ALLOC_SUCCESS           1
71 #define FAILED_REATTACHED       2
72 #define SUCCESSFULLY_REATTACHED 4
73 #define ALLOC_FAIL_MAPPING      8
74 #define ALLOC_FALLBACK          9
75 
76 typedef struct _zend_shared_segment {
77     size_t  size;
78     size_t  end;
79     size_t  pos;  /* position for simple stack allocator */
80     void   *p;
81 } zend_shared_segment;
82 
83 typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, char **error_in);
84 typedef int (*detach_segment_t)(zend_shared_segment *shared_segment);
85 
86 typedef struct {
87 	create_segments_t create_segments;
88 	detach_segment_t detach_segment;
89 	size_t (*segment_type_size)(void);
90 } zend_shared_memory_handlers;
91 
92 typedef struct _handler_entry {
93 	const char                  *name;
94 	zend_shared_memory_handlers *handler;
95 } zend_shared_memory_handler_entry;
96 
97 typedef struct _zend_shared_memory_state {
98 	size_t *positions;  /* current positions for each segment */
99 	size_t shared_free; /* amount of free shared memory */
100 } zend_shared_memory_state;
101 
102 typedef struct _zend_smm_shared_globals {
103     /* Shared Memory Manager */
104     zend_shared_segment      **shared_segments;
105     /* Number of allocated shared segments */
106     int                        shared_segments_count;
107     /* Amount of free shared memory */
108     size_t                     shared_free;
109     /* Amount of shared memory allocated by garbage */
110     size_t                     wasted_shared_memory;
111     /* No more shared memory flag */
112     bool                  memory_exhausted;
113     /* Saved Shared Allocator State */
114     zend_shared_memory_state   shared_memory_state;
115 	/* Pointer to the application's shared data structures */
116 	void                      *app_shared_globals;
117 	/* Reserved shared memory */
118 	void                      *reserved;
119 	size_t                     reserved_size;
120 } zend_smm_shared_globals;
121 
122 extern zend_smm_shared_globals *smm_shared_globals;
123 
124 #define ZSMMG(element)		(smm_shared_globals->element)
125 
126 #define SHARED_ALLOC_REATTACHED		(SUCCESS+1)
127 
128 int zend_shared_alloc_startup(size_t requested_size, size_t reserved_size);
129 void zend_shared_alloc_shutdown(void);
130 
131 /* allocate shared memory block */
132 void *zend_shared_alloc_pages(size_t requested_size);
133 void *zend_shared_alloc(size_t size);
134 
135 /* copy into shared memory */
136 void *zend_shared_memdup_get_put_free(void *source, size_t size);
137 void *zend_shared_memdup_put_free(void *source, size_t size);
138 void *zend_shared_memdup_free(void *source, size_t size);
139 void *zend_shared_memdup_get_put(void *source, size_t size);
140 void *zend_shared_memdup_put(void *source, size_t size);
141 void *zend_shared_memdup(void *source, size_t size);
142 
143 int  zend_shared_memdup_size(void *p, size_t size);
144 
145 int zend_accel_in_shm(void *ptr);
146 
147 typedef union _align_test {
148 	void   *ptr;
149 	double  dbl;
150 	zend_long  lng;
151 } align_test;
152 
153 #if ZEND_GCC_VERSION >= 2000
154 # define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test))
155 #else
156 # define PLATFORM_ALIGNMENT (sizeof(align_test))
157 #endif
158 
159 #define ZEND_ALIGNED_SIZE(size) \
160 	ZEND_MM_ALIGNED_SIZE_EX(size, PLATFORM_ALIGNMENT)
161 
162 /* exclusive locking */
163 void zend_shared_alloc_lock(void);
164 void zend_shared_alloc_unlock(void); /* returns the allocated size during lock..unlock */
165 void zend_shared_alloc_safe_unlock(void);
166 
167 /* old/new mapping functions */
168 void zend_shared_alloc_init_xlat_table(void);
169 void zend_shared_alloc_destroy_xlat_table(void);
170 void zend_shared_alloc_clear_xlat_table(void);
171 uint32_t zend_shared_alloc_checkpoint_xlat_table(void);
172 void zend_shared_alloc_restore_xlat_table(uint32_t checkpoint);
173 void zend_shared_alloc_register_xlat_entry(const void *old, const void *new);
174 void *zend_shared_alloc_get_xlat_entry(const void *old);
175 
176 size_t zend_shared_alloc_get_free_memory(void);
177 void zend_shared_alloc_save_state(void);
178 void zend_shared_alloc_restore_state(void);
179 const char *zend_accel_get_shared_model(void);
180 
181 /* memory write protection */
182 void zend_accel_shared_protect(int mode);
183 
184 #ifdef USE_MMAP
185 extern zend_shared_memory_handlers zend_alloc_mmap_handlers;
186 #endif
187 
188 #ifdef USE_SHM
189 extern zend_shared_memory_handlers zend_alloc_shm_handlers;
190 #endif
191 
192 #ifdef USE_SHM_OPEN
193 extern zend_shared_memory_handlers zend_alloc_posix_handlers;
194 #endif
195 
196 #ifdef ZEND_WIN32
197 extern zend_shared_memory_handlers zend_alloc_win32_handlers;
198 void zend_shared_alloc_create_lock(void);
199 void zend_shared_alloc_lock_win32(void);
200 void zend_shared_alloc_unlock_win32(void);
201 #endif
202 
203 #endif /* ZEND_SHARED_ALLOC_H */
204