xref: /PHP-5.5/ext/opcache/zend_shared_alloc.h (revision 9fb48edf)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2015 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 
72 typedef struct _zend_shared_segment {
73     size_t  size;
74     size_t  pos;  /* position for simple stack allocator */
75     void   *p;
76 } zend_shared_segment;
77 
78 typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, char **error_in);
79 typedef int (*detach_segment_t)(zend_shared_segment *shared_segment);
80 
81 typedef struct {
82 	create_segments_t create_segments;
83 	detach_segment_t detach_segment;
84 	size_t (*segment_type_size)(void);
85 } zend_shared_memory_handlers;
86 
87 typedef struct _handler_entry {
88 	const char                  *name;
89 	zend_shared_memory_handlers *handler;
90 } zend_shared_memory_handler_entry;
91 
92 typedef struct _zend_shared_memory_state {
93 	int *positions;   /* current positions for each segment */
94 	size_t shared_free; /* amount of free shared memory */
95 } zend_shared_memory_state;
96 
97 typedef struct _zend_smm_shared_globals {
98     /* Shared Memory Manager */
99     zend_shared_segment      **shared_segments;
100     /* Number of allocated shared segments */
101     int                        shared_segments_count;
102     /* Amount of free shared memory */
103     size_t                     shared_free;
104     /* Amount of shared memory allocated by garbage */
105     int                        wasted_shared_memory;
106     /* No more shared memory flag */
107     zend_bool                  memory_exhausted;
108     /* Saved Shared Allocator State */
109     zend_shared_memory_state   shared_memory_state;
110 	/* Pointer to the application's shared data structures */
111 	void                      *app_shared_globals;
112 } zend_smm_shared_globals;
113 
114 extern zend_smm_shared_globals *smm_shared_globals;
115 
116 #define ZSMMG(element)		(smm_shared_globals->element)
117 
118 #define SHARED_ALLOC_REATTACHED		(SUCCESS+1)
119 
120 int zend_shared_alloc_startup(size_t requested_size);
121 void zend_shared_alloc_shutdown(void);
122 
123 /* allocate shared memory block */
124 void *zend_shared_alloc(size_t size);
125 
126 /* copy into shared memory */
127 void *_zend_shared_memdup(void *p, size_t size, zend_bool free_source TSRMLS_DC);
128 int  zend_shared_memdup_size(void *p, size_t size);
129 
130 typedef union _align_test {
131 	void   *ptr;
132 	double  dbl;
133 	long    lng;
134 } align_test;
135 
136 #if ZEND_GCC_VERSION >= 2000
137 # define PLATFORM_ALIGNMENT (__alignof__ (align_test))
138 #else
139 # define PLATFORM_ALIGNMENT (sizeof(align_test))
140 #endif
141 
142 #define ZEND_ALIGNED_SIZE(size) \
143 	((size + PLATFORM_ALIGNMENT - 1) & ~(PLATFORM_ALIGNMENT - 1))
144 
145 /* exclusive locking */
146 void zend_shared_alloc_lock(TSRMLS_D);
147 void zend_shared_alloc_unlock(TSRMLS_D); /* returns the allocated size during lock..unlock */
148 void zend_shared_alloc_safe_unlock(TSRMLS_D);
149 
150 /* old/new mapping functions */
151 void zend_shared_alloc_clear_xlat_table(void);
152 void zend_shared_alloc_register_xlat_entry(const void *old, const void *new);
153 void *zend_shared_alloc_get_xlat_entry(const void *old);
154 
155 size_t zend_shared_alloc_get_free_memory(void);
156 void zend_shared_alloc_save_state(void);
157 void zend_shared_alloc_restore_state(void);
158 const char *zend_accel_get_shared_model(void);
159 
160 /* memory write protection */
161 void zend_accel_shared_protect(int mode TSRMLS_DC);
162 
163 #ifdef USE_MMAP
164 extern zend_shared_memory_handlers zend_alloc_mmap_handlers;
165 #endif
166 
167 #ifdef USE_SHM
168 extern zend_shared_memory_handlers zend_alloc_shm_handlers;
169 #endif
170 
171 #ifdef USE_SHM_OPEN
172 extern zend_shared_memory_handlers zend_alloc_posix_handlers;
173 #endif
174 
175 #ifdef ZEND_WIN32
176 extern zend_shared_memory_handlers zend_alloc_win32_handlers;
177 void zend_shared_alloc_create_lock(void);
178 void zend_shared_alloc_lock_win32(void);
179 void zend_shared_alloc_unlock_win32(void);
180 #endif
181 
182 #endif /* ZEND_SHARED_ALLOC_H */
183