1 | #ifndef __XCACHE_H |
---|
2 | #define __XCACHE_H |
---|
3 | #define XCACHE_NAME "XCache" |
---|
4 | #define XCACHE_VERSION "1.3.2" |
---|
5 | #define XCACHE_AUTHOR "mOo" |
---|
6 | #define XCACHE_COPYRIGHT "Copyright (c) 2005-2011" |
---|
7 | #define XCACHE_URL "http://xcache.lighttpd.net" |
---|
8 | #define XCACHE_WIKI_URL XCACHE_URL "/wiki" |
---|
9 | |
---|
10 | #include <php.h> |
---|
11 | #include <zend_compile.h> |
---|
12 | #include <zend_API.h> |
---|
13 | #include <zend.h> |
---|
14 | #include "php_ini.h" |
---|
15 | #include "zend_hash.h" |
---|
16 | |
---|
17 | #ifdef HAVE_CONFIG_H |
---|
18 | #include <config.h> |
---|
19 | #endif |
---|
20 | #include "xc_shm.h" |
---|
21 | #include "lock.h" |
---|
22 | |
---|
23 | #define HAVE_INODE |
---|
24 | #if !defined(ZEND_ENGINE_2_4) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 3 && PHP_RELEASE_VERSION >= 99 || PHP_MAJOR_VERSION > 5) |
---|
25 | # define ZEND_ENGINE_2_4 |
---|
26 | #endif |
---|
27 | #if !defined(ZEND_ENGINE_2_3) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 3 || defined(ZEND_ENGINE_2_4)) |
---|
28 | # define ZEND_ENGINE_2_3 |
---|
29 | #endif |
---|
30 | #if !defined(ZEND_ENGINE_2_2) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 2 || defined(ZEND_ENGINE_2_3)) |
---|
31 | # define ZEND_ENGINE_2_2 |
---|
32 | #endif |
---|
33 | #if !defined(ZEND_ENGINE_2_1) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 || defined(ZEND_ENGINE_2_2)) |
---|
34 | # define ZEND_ENGINE_2_1 |
---|
35 | #endif |
---|
36 | |
---|
37 | #define NOTHING |
---|
38 | /* ZendEngine code Switcher */ |
---|
39 | #ifndef ZEND_ENGINE_2 |
---|
40 | # define ZESW(v1, v2) v1 |
---|
41 | #else |
---|
42 | # define ZESW(v1, v2) v2 |
---|
43 | #endif |
---|
44 | |
---|
45 | #ifdef do_alloca_with_limit |
---|
46 | # define my_do_alloca(size, use_heap) do_alloca_with_limit(size, use_heap) |
---|
47 | # define my_free_alloca(size, use_heap) free_alloca_with_limit(size, use_heap) |
---|
48 | #elif defined(ALLOCA_FLAG) |
---|
49 | # define my_do_alloca(size, use_heap) do_alloca(size, use_heap) |
---|
50 | # define my_free_alloca(size, use_heap) free_alloca(size, use_heap) |
---|
51 | #else |
---|
52 | # define my_do_alloca(size, use_heap) do_alloca(size) |
---|
53 | # define my_free_alloca(size, use_heap) free_alloca(size) |
---|
54 | # define ALLOCA_FLAG(x) |
---|
55 | #endif |
---|
56 | #ifndef Z_ISREF |
---|
57 | # define Z_ISREF(z) (z).is_ref |
---|
58 | #endif |
---|
59 | #ifndef Z_SET_ISREF |
---|
60 | # define Z_SET_ISREF(z) (z).is_ref = 1 |
---|
61 | #endif |
---|
62 | #ifndef Z_UNSET_ISREF |
---|
63 | # define Z_UNSET_ISREF(z) (z).is_ref = 0 |
---|
64 | #endif |
---|
65 | #ifndef Z_REFCOUNT |
---|
66 | # define Z_REFCOUNT(z) (z).refcount |
---|
67 | #endif |
---|
68 | #ifndef Z_SET_REFCOUNT |
---|
69 | # define Z_SET_REFCOUNT(z, rc) (z).refcount = rc |
---|
70 | #endif |
---|
71 | #ifndef IS_CONSTANT_TYPE_MASK |
---|
72 | # define IS_CONSTANT_TYPE_MASK (~IS_CONSTANT_INDEX) |
---|
73 | #endif |
---|
74 | |
---|
75 | /* {{{ dirty fix for PHP 6 */ |
---|
76 | #ifdef add_assoc_long_ex |
---|
77 | static inline void my_add_assoc_long_ex(zval *arg, char *key, uint key_len, long value) |
---|
78 | { |
---|
79 | add_assoc_long_ex(arg, key, key_len, value); |
---|
80 | } |
---|
81 | # undef add_assoc_long_ex |
---|
82 | # define add_assoc_long_ex my_add_assoc_long_ex |
---|
83 | #endif |
---|
84 | #ifdef add_assoc_bool_ex |
---|
85 | static inline void my_add_assoc_bool_ex(zval *arg, char *key, uint key_len, zend_bool value) |
---|
86 | { |
---|
87 | add_assoc_bool_ex(arg, key, key_len, value); |
---|
88 | } |
---|
89 | # undef add_assoc_bool_ex |
---|
90 | # define add_assoc_bool_ex my_add_assoc_bool_ex |
---|
91 | #endif |
---|
92 | #ifdef add_assoc_null_ex |
---|
93 | static inline void my_add_assoc_null_ex(zval *arg, char *key, uint key_len) |
---|
94 | { |
---|
95 | add_assoc_null_ex(arg, key, key_len); |
---|
96 | } |
---|
97 | # undef add_assoc_null_ex |
---|
98 | # define add_assoc_null_ex my_add_assoc_null_ex |
---|
99 | #endif |
---|
100 | |
---|
101 | #ifdef ZEND_ENGINE_2_4 |
---|
102 | # define Z_OP(op) (op) |
---|
103 | # define Z_OP_CONSTANT(op) (op).literal->constant |
---|
104 | # define Z_OP_TYPE(op) op##_##type |
---|
105 | # define Z_OP_TYPEOF_TYPE zend_uchar |
---|
106 | |
---|
107 | # define Z_CLASS_INFO(className) (className).info.user |
---|
108 | #else |
---|
109 | # define Z_OP(op) (op).u |
---|
110 | # define Z_OP_CONSTANT(op) (op).u.constant |
---|
111 | # define Z_OP_TYPE(op) (op).op_type |
---|
112 | # define Z_OP_TYPEOF_TYPE int |
---|
113 | typedef znode znode_op; |
---|
114 | |
---|
115 | # define Z_CLASS_INFO(className) (className) |
---|
116 | #endif |
---|
117 | |
---|
118 | /* }}} */ |
---|
119 | |
---|
120 | /* unicode */ |
---|
121 | #ifdef IS_UNICODE |
---|
122 | # define UNISW(text, unicode) unicode |
---|
123 | #else |
---|
124 | # define UNISW(text, unicode) text |
---|
125 | #endif |
---|
126 | #define BUCKET_KEY_SIZE(b) \ |
---|
127 | (UNISW( \ |
---|
128 | (b)->nKeyLength, \ |
---|
129 | ((b)->key.type == IS_UNICODE) \ |
---|
130 | ? UBYTES(b->nKeyLength) \ |
---|
131 | : b->nKeyLength \ |
---|
132 | )) |
---|
133 | #define BUCKET_KEY_S(b) (UNISW((b)->arKey, (b)->key.arKey.s)) |
---|
134 | #define BUCKET_KEY_U(b) (UNISW((b)->arKey, (b)->key.arKey.u)) |
---|
135 | #define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING, (b)->key.type)) |
---|
136 | #ifdef IS_UNICODE |
---|
137 | # define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey) |
---|
138 | #else |
---|
139 | # define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey) |
---|
140 | #endif |
---|
141 | #define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b)) |
---|
142 | |
---|
143 | #ifndef IS_UNICODE |
---|
144 | typedef char *zstr; |
---|
145 | # define ZSTR_S(s) (s) |
---|
146 | # define ZSTR_U(s) (s) |
---|
147 | # define ZSTR_V(s) (s) |
---|
148 | # define ZSTR_PS(s) (s) |
---|
149 | # define ZSTR_PU(s) (s) |
---|
150 | # define ZSTR_PV(s) (s) |
---|
151 | #else |
---|
152 | # define ZSTR_S(zs) ((zs).s) |
---|
153 | # define ZSTR_U(zs) ((zs).u) |
---|
154 | # define ZSTR_V(zs) ((zs).v) |
---|
155 | # define ZSTR_PS(pzs) ((pzs)->s) |
---|
156 | # define ZSTR_PU(pzs) ((pzs)->u) |
---|
157 | # define ZSTR_PV(pzs) ((pzs)->v) |
---|
158 | #endif |
---|
159 | |
---|
160 | #ifndef ZSTR |
---|
161 | # define ZSTR(s) (s) |
---|
162 | #endif |
---|
163 | |
---|
164 | #ifndef Z_UNIVAL |
---|
165 | # define Z_UNIVAL(zval) (zval).value.str.val |
---|
166 | # define Z_UNILEN(zval) (zval).value.str.len |
---|
167 | #endif |
---|
168 | |
---|
169 | /* {{{ u hash wrapper */ |
---|
170 | #ifndef IS_UNICODE |
---|
171 | # define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \ |
---|
172 | zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest) |
---|
173 | |
---|
174 | # define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \ |
---|
175 | zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) |
---|
176 | |
---|
177 | # define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \ |
---|
178 | zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest) |
---|
179 | |
---|
180 | # define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \ |
---|
181 | zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) |
---|
182 | |
---|
183 | # define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \ |
---|
184 | zend_hash_find(ht, arKey, nKeyLength, pData) |
---|
185 | |
---|
186 | # define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \ |
---|
187 | zend_hash_quick_find(ht, arKey, nKeyLength, h, pData) |
---|
188 | |
---|
189 | # define add_u_assoc_zval_ex(arg, type, key, key_len, value) \ |
---|
190 | add_assoc_zval_ex(arg, key, key_len, value) |
---|
191 | |
---|
192 | # define zend_u_is_auto_global(type, name, name_len) \ |
---|
193 | zend_is_auto_global(name, name_len) |
---|
194 | #endif |
---|
195 | /* }}} */ |
---|
196 | |
---|
197 | |
---|
198 | #define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0]))) |
---|
199 | #define ECALLOC_ONE(x) ECALLOC_N(x, 1) |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | typedef ulong xc_hash_value_t; |
---|
204 | typedef struct { |
---|
205 | int bits; |
---|
206 | int size; |
---|
207 | int mask; |
---|
208 | } xc_hash_t; |
---|
209 | |
---|
210 | /* the class entry type to be stored in class_table */ |
---|
211 | typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t; |
---|
212 | |
---|
213 | /* xc_cest_t to (zend_class_entry*) */ |
---|
214 | #define CestToCePtr(st) (ZESW(\ |
---|
215 | &(st), \ |
---|
216 | st \ |
---|
217 | ) ) |
---|
218 | |
---|
219 | /* ZCEP=zend class entry ptr */ |
---|
220 | #define ZCEP_REFCOUNT_PTR(pce) (ZESW( \ |
---|
221 | (pce)->refcount, \ |
---|
222 | &((pce)->refcount) \ |
---|
223 | )) |
---|
224 | |
---|
225 | #define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce) |
---|
226 | |
---|
227 | typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC); |
---|
228 | |
---|
229 | /* {{{ xc_cache_t */ |
---|
230 | typedef struct _xc_entry_t xc_entry_t; |
---|
231 | typedef struct { |
---|
232 | int cacheid; |
---|
233 | xc_hash_t *hcache; /* hash to cacheid */ |
---|
234 | |
---|
235 | time_t compiling; |
---|
236 | zend_ulong misses; |
---|
237 | zend_ulong hits; |
---|
238 | zend_ulong clogs; |
---|
239 | zend_ulong ooms; |
---|
240 | zend_ulong errors; |
---|
241 | xc_lock_t *lck; |
---|
242 | xc_shm_t *shm; /* to which shm contains us */ |
---|
243 | xc_mem_t *mem; /* to which mem contains us */ |
---|
244 | |
---|
245 | xc_entry_t **entries; |
---|
246 | int entries_count; |
---|
247 | xc_entry_t *deletes; |
---|
248 | int deletes_count; |
---|
249 | xc_hash_t *hentry; /* hash to entry */ |
---|
250 | |
---|
251 | time_t last_gc_deletes; |
---|
252 | time_t last_gc_expires; |
---|
253 | |
---|
254 | time_t hits_by_hour_cur_time; |
---|
255 | zend_uint hits_by_hour_cur_slot; |
---|
256 | zend_ulong hits_by_hour[24]; |
---|
257 | time_t hits_by_second_cur_time; |
---|
258 | zend_uint hits_by_second_cur_slot; |
---|
259 | zend_ulong hits_by_second[5]; |
---|
260 | } xc_cache_t; |
---|
261 | /* }}} */ |
---|
262 | /* {{{ xc_classinfo_t */ |
---|
263 | typedef struct { |
---|
264 | #ifdef IS_UNICODE |
---|
265 | zend_uchar type; |
---|
266 | #endif |
---|
267 | zstr key; |
---|
268 | zend_uint key_size; |
---|
269 | ulong h; |
---|
270 | xc_cest_t cest; |
---|
271 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
272 | int oplineno; |
---|
273 | #endif |
---|
274 | } xc_classinfo_t; |
---|
275 | /* }}} */ |
---|
276 | #ifdef HAVE_XCACHE_CONSTANT |
---|
277 | /* {{{ xc_constinfo_t */ |
---|
278 | typedef struct { |
---|
279 | #ifdef IS_UNICODE |
---|
280 | zend_uchar type; |
---|
281 | #endif |
---|
282 | zstr key; |
---|
283 | zend_uint key_size; |
---|
284 | ulong h; |
---|
285 | zend_constant constant; |
---|
286 | } xc_constinfo_t; |
---|
287 | /* }}} */ |
---|
288 | #endif |
---|
289 | /* {{{ xc_funcinfo_t */ |
---|
290 | typedef struct { |
---|
291 | #ifdef IS_UNICODE |
---|
292 | zend_uchar type; |
---|
293 | #endif |
---|
294 | zstr key; |
---|
295 | zend_uint key_size; |
---|
296 | ulong h; |
---|
297 | zend_function func; |
---|
298 | } xc_funcinfo_t; |
---|
299 | /* }}} */ |
---|
300 | #ifdef ZEND_ENGINE_2_1 |
---|
301 | /* {{{ xc_autoglobal_t */ |
---|
302 | typedef struct { |
---|
303 | #ifdef IS_UNICODE |
---|
304 | zend_uchar type; |
---|
305 | #endif |
---|
306 | zstr key; |
---|
307 | zend_uint key_len; |
---|
308 | ulong h; |
---|
309 | } xc_autoglobal_t; |
---|
310 | /* }}} */ |
---|
311 | #endif |
---|
312 | typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t; |
---|
313 | /* {{{ xc_entry_data_php_t */ |
---|
314 | typedef struct { |
---|
315 | size_t sourcesize; |
---|
316 | #ifdef HAVE_INODE |
---|
317 | int device; /* the filesystem device */ |
---|
318 | int inode; /* the filesystem inode */ |
---|
319 | #endif |
---|
320 | time_t mtime; /* the mtime of origin source file */ |
---|
321 | |
---|
322 | zend_op_array *op_array; |
---|
323 | |
---|
324 | #ifdef HAVE_XCACHE_CONSTANT |
---|
325 | zend_uint constinfo_cnt; |
---|
326 | xc_constinfo_t *constinfos; |
---|
327 | #endif |
---|
328 | |
---|
329 | zend_uint funcinfo_cnt; |
---|
330 | xc_funcinfo_t *funcinfos; |
---|
331 | |
---|
332 | zend_uint classinfo_cnt; |
---|
333 | xc_classinfo_t *classinfos; |
---|
334 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
335 | zend_bool have_early_binding; |
---|
336 | #endif |
---|
337 | |
---|
338 | #ifdef ZEND_ENGINE_2_1 |
---|
339 | zend_uint autoglobal_cnt; |
---|
340 | xc_autoglobal_t *autoglobals; |
---|
341 | #endif |
---|
342 | } xc_entry_data_php_t; |
---|
343 | /* }}} */ |
---|
344 | /* {{{ xc_entry_data_var_t */ |
---|
345 | typedef struct { |
---|
346 | zval *value; |
---|
347 | } xc_entry_data_var_t; |
---|
348 | /* }}} */ |
---|
349 | typedef zvalue_value xc_entry_name_t; |
---|
350 | /* {{{ xc_entry_t */ |
---|
351 | struct _xc_entry_t { |
---|
352 | xc_entry_type_t type; |
---|
353 | xc_hash_value_t hvalue; |
---|
354 | xc_entry_t *next; |
---|
355 | xc_cache_t *cache; /* which cache it's on */ |
---|
356 | |
---|
357 | size_t size; |
---|
358 | zend_ulong refcount; |
---|
359 | zend_ulong hits; |
---|
360 | time_t ctime; /* the ctime of this entry */ |
---|
361 | time_t atime; /* the atime of this entry */ |
---|
362 | time_t dtime; /* the deletion time of this entry */ |
---|
363 | long ttl; /* ttl of time entry, var only */ |
---|
364 | |
---|
365 | #ifdef IS_UNICODE |
---|
366 | zend_uchar name_type; |
---|
367 | #endif |
---|
368 | xc_entry_name_t name; |
---|
369 | |
---|
370 | union { |
---|
371 | xc_entry_data_php_t *php; |
---|
372 | xc_entry_data_var_t *var; |
---|
373 | } data; |
---|
374 | |
---|
375 | zend_bool have_references; |
---|
376 | }; |
---|
377 | /* }}} */ |
---|
378 | |
---|
379 | extern zend_module_entry xcache_module_entry; |
---|
380 | #define phpext_xcache_ptr &xcache_module_entry |
---|
381 | |
---|
382 | int xc_is_rw(const void *p); |
---|
383 | int xc_is_ro(const void *p); |
---|
384 | int xc_is_shm(const void *p); |
---|
385 | void xc_gc_add_op_array(zend_op_array *op_array TSRMLS_DC); |
---|
386 | |
---|
387 | #endif /* __XCACHE_H */ |
---|