xref: /PHP-7.4/ext/opcache/zend_shared_alloc.h (revision 7d5e2e53)
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    | 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@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  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, 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 	zend_shared_memory_handlers *handler;
94 } zend_shared_memory_handler_entry;
95 
96 typedef struct _zend_shared_memory_state {
97 	int *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     zend_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 } zend_smm_shared_globals;
117 
118 extern zend_smm_shared_globals *smm_shared_globals;
119 
120 #define ZSMMG(element)		(smm_shared_globals->element)
121 
122 #define SHARED_ALLOC_REATTACHED		(SUCCESS+1)
123 
124 int zend_shared_alloc_startup(size_t requested_size);
125 void zend_shared_alloc_shutdown(void);
126 
127 /* allocate shared memory block */
128 void *zend_shared_alloc(size_t size);
129 
130 /* copy into shared memory */
131 void *zend_shared_memdup_get_put_free(void *source, size_t size);
132 void *zend_shared_memdup_put_free(void *source, size_t size);
133 void *zend_shared_memdup_free(void *source, size_t size);
134 void *zend_shared_memdup_get_put(void *source, size_t size);
135 void *zend_shared_memdup_put(void *source, size_t size);
136 void *zend_shared_memdup(void *source, size_t size);
137 void *zend_shared_memdup_arena_put(void *source, size_t size);
138 void *zend_shared_memdup_arena(void *source, size_t size);
139 
140 int  zend_shared_memdup_size(void *p, size_t size);
141 
142 int zend_accel_in_shm(void *ptr);
143 
144 typedef union _align_test {
145 	void   *ptr;
146 	double  dbl;
147 	zend_long  lng;
148 } align_test;
149 
150 #if ZEND_GCC_VERSION >= 2000
151 # define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test))
152 #else
153 # define PLATFORM_ALIGNMENT (sizeof(align_test))
154 #endif
155 
156 #define ZEND_ALIGNED_SIZE(size) \
157 	ZEND_MM_ALIGNED_SIZE_EX(size, PLATFORM_ALIGNMENT)
158 
159 /* exclusive locking */
160 void zend_shared_alloc_lock(void);
161 void zend_shared_alloc_unlock(void); /* returns the allocated size during lock..unlock */
162 void zend_shared_alloc_safe_unlock(void);
163 
164 /* old/new mapping functions */
165 void zend_shared_alloc_init_xlat_table(void);
166 void zend_shared_alloc_destroy_xlat_table(void);
167 void zend_shared_alloc_clear_xlat_table(void);
168 uint32_t zend_shared_alloc_checkpoint_xlat_table(void);
169 void zend_shared_alloc_restore_xlat_table(uint32_t checkpoint);
170 void zend_shared_alloc_register_xlat_entry(const void *old, const void *new);
171 void *zend_shared_alloc_get_xlat_entry(const void *old);
172 
173 size_t zend_shared_alloc_get_free_memory(void);
174 void zend_shared_alloc_save_state(void);
175 void zend_shared_alloc_restore_state(void);
176 const char *zend_accel_get_shared_model(void);
177 
178 /* memory write protection */
179 void zend_accel_shared_protect(int mode);
180 
181 #ifdef USE_MMAP
182 extern zend_shared_memory_handlers zend_alloc_mmap_handlers;
183 #endif
184 
185 #ifdef USE_SHM
186 extern zend_shared_memory_handlers zend_alloc_shm_handlers;
187 #endif
188 
189 #ifdef USE_SHM_OPEN
190 extern zend_shared_memory_handlers zend_alloc_posix_handlers;
191 #endif
192 
193 #ifdef ZEND_WIN32
194 extern zend_shared_memory_handlers zend_alloc_win32_handlers;
195 void zend_shared_alloc_create_lock(void);
196 void zend_shared_alloc_lock_win32(void);
197 void zend_shared_alloc_unlock_win32(void);
198 #endif
199 
200 #endif /* ZEND_SHARED_ALLOC_H */
201