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