source: trunk/xcache.h @ 937

Last change on this file since 937 was 937, checked in by moo, 22 months ago

still WIP: rewrite sandbox to use callback

  • Property svn:eol-style set to native
File size: 12.1 KB
Line 
1#ifndef __XCACHE_H
2#define __XCACHE_H
3#define XCACHE_NAME       "XCache"
4#ifndef XCACHE_VERSION
5#   define XCACHE_VERSION "2.0.1-dev"
6#endif
7#define XCACHE_AUTHOR     "mOo"
8#define XCACHE_COPYRIGHT  "Copyright (c) 2005-2012"
9#define XCACHE_URL        "http://xcache.lighttpd.net"
10#define XCACHE_WIKI_URL   XCACHE_URL "/wiki"
11
12#include <php.h>
13#include <zend_compile.h>
14#include <zend_API.h>
15#include <zend.h>
16#include "php_ini.h"
17#include "zend_hash.h"
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22#include "xc_shm.h"
23#include "lock.h"
24
25#define HAVE_INODE
26#if !defined(ZEND_ENGINE_2_4) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4 || PHP_MAJOR_VERSION > 5)
27#   define ZEND_ENGINE_2_4
28#endif
29#if !defined(ZEND_ENGINE_2_3) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 3 || defined(ZEND_ENGINE_2_4))
30#   define ZEND_ENGINE_2_3
31#endif
32#if !defined(ZEND_ENGINE_2_2) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 2 || defined(ZEND_ENGINE_2_3))
33#   define ZEND_ENGINE_2_2
34#endif
35#if !defined(ZEND_ENGINE_2_1) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 || defined(ZEND_ENGINE_2_2))
36#   define ZEND_ENGINE_2_1
37#endif
38
39#define NOTHING
40/* ZendEngine code Switcher */
41#ifndef ZEND_ENGINE_2
42#   define ZESW(v1, v2) v1
43#else
44#   define ZESW(v1, v2) v2
45#endif
46#ifdef ZEND_ENGINE_2_4
47#   define ZEND_24(pre24, v24) v24
48#else
49#   define ZEND_24(pre24, v24) pre24
50#endif
51
52#ifdef do_alloca_with_limit
53#   define my_do_alloca(size, use_heap) do_alloca_with_limit(size, use_heap)
54#   define my_free_alloca(size, use_heap) free_alloca_with_limit(size, use_heap)
55#elif defined(ALLOCA_FLAG)
56#   define my_do_alloca(size, use_heap) do_alloca(size, use_heap)
57#   define my_free_alloca(size, use_heap) free_alloca(size, use_heap)
58#else
59#   define my_do_alloca(size, use_heap) do_alloca(size)
60#   define my_free_alloca(size, use_heap) free_alloca(size)
61#   define ALLOCA_FLAG(x)
62#endif
63#ifndef Z_ISREF
64#   define Z_ISREF(z) (z).is_ref
65#endif
66#ifndef Z_SET_ISREF
67#   define Z_SET_ISREF(z) (z).is_ref = 1
68#endif
69#ifndef Z_UNSET_ISREF
70#   define Z_UNSET_ISREF(z) (z).is_ref = 0
71#endif
72#ifndef Z_REFCOUNT
73#   define Z_REFCOUNT(z) (z).refcount
74#endif
75#ifndef Z_SET_REFCOUNT
76#   define Z_SET_REFCOUNT(z, rc) (z).refcount = rc
77#endif
78#ifndef IS_CONSTANT_TYPE_MASK
79#   define IS_CONSTANT_TYPE_MASK (~IS_CONSTANT_INDEX)
80#endif
81
82/* {{{ dirty fix for PHP 6 */
83#ifdef add_assoc_long_ex
84static inline void my_add_assoc_long_ex(zval *arg, char *key, uint key_len, long value)
85{
86    add_assoc_long_ex(arg, key, key_len, value);
87}
88#   undef add_assoc_long_ex
89#   define add_assoc_long_ex my_add_assoc_long_ex
90#endif
91#ifdef add_assoc_bool_ex
92static inline void my_add_assoc_bool_ex(zval *arg, char *key, uint key_len, zend_bool value)
93{
94    add_assoc_bool_ex(arg, key, key_len, value);
95}
96#   undef add_assoc_bool_ex
97#   define add_assoc_bool_ex my_add_assoc_bool_ex
98#endif
99#ifdef add_assoc_null_ex
100static inline void my_add_assoc_null_ex(zval *arg, char *key, uint key_len)
101{
102    add_assoc_null_ex(arg, key, key_len);
103}
104#   undef add_assoc_null_ex
105#   define add_assoc_null_ex my_add_assoc_null_ex
106#endif
107
108#ifdef ZEND_ENGINE_2_4
109#   define Z_OP(op) (op)
110#   define Z_OP_CONSTANT(op) (op).literal->constant
111#   define Z_OP_TYPE(op) op##_##type
112#   define Z_OP_TYPEOF_TYPE zend_uchar
113
114#   define Z_CLASS_INFO(className) (className).info.user
115#else
116#   define Z_OP(op) (op).u
117#   define Z_OP_CONSTANT(op) (op).u.constant
118#   define Z_OP_TYPE(op) (op).op_type
119#   define Z_OP_TYPEOF_TYPE int
120typedef znode znode_op;
121
122#   define Z_CLASS_INFO(className) (className)
123#endif
124
125/* }}} */
126
127/* unicode */
128#ifdef IS_UNICODE
129#   define UNISW(text, unicode) unicode
130#else
131#   define UNISW(text, unicode) text
132#endif
133#define BUCKET_KEY_SIZE(b) \
134        (UNISW( \
135            (b)->nKeyLength, \
136                ((b)->key.type == IS_UNICODE) \
137                ? UBYTES(b->nKeyLength) \
138                : b->nKeyLength \
139                ))
140#define BUCKET_KEY(b)      (UNISW((b)->arKey, (b)->key.arKey))
141#define BUCKET_KEY_S(b)    ZSTR_S(BUCKET_KEY(b))
142#define BUCKET_KEY_U(b)    ZSTR_U(BUCKET_KEY(b))
143#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
144#ifdef IS_UNICODE
145#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
146#else
147#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
148#endif
149
150#ifdef ZEND_ENGINE_2_4
151#   define BUCKET_SIZE(b) (sizeof(Bucket) + BUCKET_KEY_SIZE(b))
152#else
153#   define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
154#endif
155
156#ifndef IS_UNICODE
157typedef char *zstr;
158typedef const char *const_zstr;
159#ifdef ZEND_ENGINE_2_4
160typedef const char *const24_zstr;
161typedef const char *const24_str;
162#else
163typedef char *const24_zstr;
164typedef char *const24_str;
165#endif
166
167#   define ZSTR_S(s)     (s)
168#   define ZSTR_U(s)     (s)
169#   define ZSTR_V(s)     (s)
170#   define ZSTR_PS(s)    (s)
171#   define ZSTR_PU(s)    (s)
172#   define ZSTR_PV(s)    (s)
173#else
174typedef const zstr const_zstr;
175#   define ZSTR_S(zs)    ((zs).s)
176#   define ZSTR_U(zs)    ((zs).u)
177#   define ZSTR_V(zs)    ((zs).v)
178#   define ZSTR_PS(pzs)  ((pzs)->s)
179#   define ZSTR_PU(pzs)  ((pzs)->u)
180#   define ZSTR_PV(pzs)  ((pzs)->v)
181#endif
182
183#ifndef ZSTR
184#   define ZSTR(s)      (s)
185#endif
186
187#ifndef Z_UNIVAL
188#   define Z_UNIVAL(zval) (zval).value.str.val
189#   define Z_UNILEN(zval) (zval).value.str.len
190#endif
191
192/* {{{ u hash wrapper */
193#ifndef IS_UNICODE
194#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
195       zend_hash_add(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, pData, nDataSize, pDest)
196
197#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
198       zend_hash_quick_add(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, h, pData, nDataSize, pDest)
199
200#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
201       zend_hash_update(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, pData, nDataSize, pDest)
202
203#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
204       zend_hash_quick_update(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, h, pData, nDataSize, pDest)
205
206#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
207       zend_hash_find(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, pData)
208
209#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
210       zend_hash_quick_find(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, h, pData)
211
212#   define zend_u_hash_exists(ht, type, arKey, nKeyLength) \
213       zend_hash_exists(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength)
214
215#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
216        add_assoc_zval_ex(arg, key, key_len, value)
217
218#   define zend_u_is_auto_global(type, name, name_len) \
219        zend_is_auto_global(name, name_len)
220#endif
221/* }}} */
222
223
224#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
225#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
226
227
228
229typedef ulong xc_hash_value_t;
230typedef struct {
231    size_t bits;
232    size_t size;
233    xc_hash_value_t mask;
234} xc_hash_t;
235
236/* the class entry type to be stored in class_table */
237typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
238
239/* xc_cest_t to (zend_class_entry*) */
240#define CestToCePtr(st) (ZESW(\
241            &(st), \
242            st \
243            ) )
244
245/* ZCEP=zend class entry ptr */
246#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
247            (pce)->refcount, \
248            &((pce)->refcount) \
249            ))
250
251#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
252
253typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
254
255typedef struct _xc_entry_t xc_entry_t;
256typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
257/* {{{ xc_cache_t */
258typedef struct {
259    int cacheid;
260    xc_hash_t  *hcache; /* hash to cacheid */
261
262    time_t     compiling;
263    zend_ulong updates;
264    zend_ulong hits;
265    zend_ulong clogs;
266    zend_ulong ooms;
267    zend_ulong errors;
268    xc_lock_t  *lck;
269    xc_shm_t   *shm; /* which shm contains us */
270    xc_mem_t   *mem; /* which mem contains us */
271
272    xc_entry_t **entries;
273    int entries_count;
274    xc_entry_data_php_t **phps;
275    int phps_count;
276    xc_entry_t *deletes;
277    int deletes_count;
278    xc_hash_t  *hentry; /* hash settings to entry */
279    xc_hash_t  *hphp;   /* hash settings to php */
280
281    time_t     last_gc_deletes;
282    time_t     last_gc_expires;
283
284    time_t     hits_by_hour_cur_time;
285    zend_uint  hits_by_hour_cur_slot;
286    zend_ulong hits_by_hour[24];
287    time_t     hits_by_second_cur_time;
288    zend_uint  hits_by_second_cur_slot;
289    zend_ulong hits_by_second[5];
290} xc_cache_t;
291/* }}} */
292/* {{{ xc_op_array_info_detail_t */
293typedef struct {
294    zend_uint index;
295    zend_uint info;
296} xc_op_array_info_detail_t;
297/* }}} */
298/* {{{ xc_op_array_info_t */
299typedef struct {
300#ifdef ZEND_ENGINE_2_4
301    zend_uint literalinfo_cnt;
302    xc_op_array_info_detail_t *literalinfos;
303#else
304    zend_uint oplineinfo_cnt;
305    xc_op_array_info_detail_t *oplineinfos;
306#endif
307} xc_op_array_info_t;
308/* }}} */
309/* {{{ xc_classinfo_t */
310typedef struct {
311#ifdef IS_UNICODE
312    zend_uchar   type;
313#endif
314    const24_zstr key;
315    zend_uint    key_size;
316    ulong        h;
317    zend_uint  methodinfo_cnt;
318    xc_op_array_info_t *methodinfos;
319    xc_cest_t    cest;
320#ifndef ZEND_COMPILE_DELAYED_BINDING
321    int          oplineno;
322#endif
323} xc_classinfo_t;
324/* }}} */
325#ifdef HAVE_XCACHE_CONSTANT
326/* {{{ xc_constinfo_t */
327typedef struct {
328#ifdef IS_UNICODE
329    zend_uchar    type;
330#endif
331    const24_zstr  key;
332    zend_uint     key_size;
333    ulong         h;
334    zend_constant constant;
335} xc_constinfo_t;
336/* }}} */
337#endif
338/* {{{ xc_funcinfo_t */
339typedef struct {
340#ifdef IS_UNICODE
341    zend_uchar     type;
342#endif
343    const24_zstr   key;
344    zend_uint      key_size;
345    ulong          h;
346    xc_op_array_info_t op_array_info;
347    zend_function func;
348} xc_funcinfo_t;
349/* }}} */
350#ifdef ZEND_ENGINE_2_1
351/* {{{ xc_autoglobal_t */
352typedef struct {
353#ifdef IS_UNICODE
354    zend_uchar   type;
355#endif
356    const24_zstr key;
357    zend_uint    key_len;
358    ulong        h;
359} xc_autoglobal_t;
360/* }}} */
361#endif
362typedef struct {
363    char digest[16];
364} xc_md5sum_t;
365/* {{{ xc_compilererror_t */
366typedef struct {
367    int type;
368    uint lineno;
369    int error_len;
370    char *error;
371} xc_compilererror_t;
372/* }}} */
373/* {{{ xc_entry_data_php_t */
374struct _xc_entry_data_php_t {
375    xc_entry_data_php_t *next;
376    xc_hash_value_t      hvalue;
377
378    xc_md5sum_t md5;        /* md5sum of the source */
379    zend_ulong  refcount;   /* count of entries referencing to this data */
380
381    zend_ulong hits;        /* hits of this php */
382    size_t     size;
383
384    xc_op_array_info_t op_array_info;
385    zend_op_array *op_array;
386
387#ifdef HAVE_XCACHE_CONSTANT
388    zend_uint constinfo_cnt;
389    xc_constinfo_t *constinfos;
390#endif
391
392    zend_uint funcinfo_cnt;
393    xc_funcinfo_t *funcinfos;
394
395    zend_uint classinfo_cnt;
396    xc_classinfo_t *classinfos;
397#ifndef ZEND_COMPILE_DELAYED_BINDING
398    zend_bool have_early_binding;
399#endif
400
401#ifdef ZEND_ENGINE_2_1
402    zend_uint autoglobal_cnt;
403    xc_autoglobal_t *autoglobals;
404#endif
405
406#ifdef E_STRICT
407    zend_uint compilererror_cnt;
408    xc_compilererror_t *compilererrors;
409#endif
410
411    zend_bool  have_references;
412};
413/* }}} */
414typedef zvalue_value xc_entry_name_t;
415/* {{{ xc_entry_t */
416struct _xc_entry_t {
417    xc_entry_t *next;
418
419    size_t     size;
420    time_t     ctime;           /* creation ctime of this entry */
421    time_t     atime;           /*   access atime of this entry */
422    time_t     dtime;           /*  deletion time of this entry */
423    zend_ulong hits;
424    zend_ulong ttl;
425
426    xc_entry_name_t name;
427};
428
429typedef struct {
430    xc_entry_t entry;
431    xc_entry_data_php_t *php;
432
433    zend_ulong refcount;    /* count of php instances holding this entry */
434    time_t file_mtime;
435    size_t file_size;
436#ifdef HAVE_INODE
437    int file_device;
438    int file_inode;
439#endif
440
441    int    filepath_len;
442    ZEND_24(NOTHING, const) char *filepath;
443    int    dirpath_len;
444    char  *dirpath;
445#ifdef IS_UNICODE
446    int    ufilepath_len;
447    UChar *ufilepath;
448    int    udirpath_len;
449    UChar *udirpath;
450#endif
451} xc_entry_php_t;
452
453typedef struct {
454    xc_entry_t entry;
455#ifdef IS_UNICODE
456    zend_uchar name_type;
457#endif
458    zval      *value;
459    zend_bool  have_references;
460} xc_entry_var_t;
461/* }}} */
462typedef struct xc_entry_hash_t { /* {{{ */
463    xc_hash_value_t cacheid;
464    xc_hash_value_t entryslotid;
465} xc_entry_hash_t;
466/* }}} */
467
468extern zend_module_entry xcache_module_entry;
469#define phpext_xcache_ptr &xcache_module_entry
470
471int xc_is_rw(const void *p);
472int xc_is_ro(const void *p);
473int xc_is_shm(const void *p);
474/* {{{ xc_gc_op_array_t */
475typedef struct {
476#ifdef ZEND_ENGINE_2
477    zend_uint num_args;
478    zend_arg_info *arg_info;
479#endif
480    zend_op *opcodes;
481} xc_gc_op_array_t;
482/* }}} */
483void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC);
484void xc_fix_op_array_info(const xc_entry_php_t *xce, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copy, const xc_op_array_info_t *op_array_info TSRMLS_DC);
485
486#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.