source: trunk/xcache.h @ 859

Last change on this file since 859 was 859, checked in by moo, 3 years ago

expand_path for DIR FILE support

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