source: trunk/xcache.h @ 823

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

PHP_5_4: HashTable? bucket changes

  • Property svn:eol-style set to native
File size: 11.5 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
44
[550]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)
[485]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
[798]56#ifndef Z_ISREF
57#   define Z_ISREF(z) (z).is_ref
58#endif
[485]59#ifndef Z_SET_ISREF
[562]60#   define Z_SET_ISREF(z) (z).is_ref = 1
[485]61#endif
62#ifndef Z_UNSET_ISREF
[562]63#   define Z_UNSET_ISREF(z) (z).is_ref = 0
[485]64#endif
[798]65#ifndef Z_REFCOUNT
66#   define Z_REFCOUNT(z) (z).refcount
67#endif
[485]68#ifndef Z_SET_REFCOUNT
[562]69#   define Z_SET_REFCOUNT(z, rc) (z).refcount = rc
[485]70#endif
[491]71#ifndef IS_CONSTANT_TYPE_MASK
[649]72#   define IS_CONSTANT_TYPE_MASK (~IS_CONSTANT_INDEX)
[491]73#endif
[485]74
[263]75/* {{{ dirty fix for PHP 6 */
76#ifdef add_assoc_long_ex
77static 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
85static 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
93static 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
[716]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
[769]105#   define Z_OP_TYPEOF_TYPE zend_uchar
[716]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
[769]112#   define Z_OP_TYPEOF_TYPE int
[716]113typedef znode znode_op;
114
115#   define Z_CLASS_INFO(className) (className)
116#endif
117
[263]118/* }}} */
119
[1]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                ))
[659]133#define BUCKET_KEY(b)      (UNISW((b)->arKey, (b)->key.arKey))
134#define BUCKET_KEY_S(b)    ZSTR_S(BUCKET_KEY(b))
135#define BUCKET_KEY_U(b)    ZSTR_U(BUCKET_KEY(b))
[200]136#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
[2]137#ifdef IS_UNICODE
[104]138#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
[2]139#else
[11]140#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
[2]141#endif
[1]142
[823]143#ifdef ZEND_ENGINE_2_4
144#   define BUCKET_SIZE(b) (sizeof(Bucket) + BUCKET_KEY_SIZE(b))
145#else
146#   define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
147#endif
148
[1]149#ifndef IS_UNICODE
150typedef char *zstr;
[770]151typedef const char *const_zstr;
[103]152#   define ZSTR_S(s)     (s)
153#   define ZSTR_U(s)     (s)
154#   define ZSTR_V(s)     (s)
155#   define ZSTR_PS(s)    (s)
156#   define ZSTR_PU(s)    (s)
157#   define ZSTR_PV(s)    (s)
[1]158#else
[770]159typedef const zstr const_zstr;
[103]160#   define ZSTR_S(zs)    ((zs).s)
161#   define ZSTR_U(zs)    ((zs).u)
162#   define ZSTR_V(zs)    ((zs).v)
163#   define ZSTR_PS(pzs)  ((pzs)->s)
164#   define ZSTR_PU(pzs)  ((pzs)->u)
165#   define ZSTR_PV(pzs)  ((pzs)->v)
[1]166#endif
167
[103]168#ifndef ZSTR
169#   define ZSTR(s)      (s)
170#endif
171
172#ifndef Z_UNIVAL
173#   define Z_UNIVAL(zval) (zval).value.str.val
174#   define Z_UNILEN(zval) (zval).value.str.len
175#endif
176
[1]177/* {{{ u hash wrapper */
178#ifndef IS_UNICODE
179#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
180       zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest)
181
[506]182#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
183       zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
184
[88]185#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
186       zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
187
[506]188#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
189       zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
190
[1]191#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
192       zend_hash_find(ht, arKey, nKeyLength, pData)
193
[200]194#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
195       zend_hash_quick_find(ht, arKey, nKeyLength, h, pData)
196
[1]197#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
198        add_assoc_zval_ex(arg, key, key_len, value)
199
[268]200#   define zend_u_is_auto_global(type, name, name_len) \
201        zend_is_auto_global(name, name_len)
[1]202#endif
203/* }}} */
204
205
206#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
207#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
208
209
210
211typedef ulong xc_hash_value_t;
212typedef struct {
213    int bits;
214    int size;
215    int mask;
216} xc_hash_t;
217
218/* the class entry type to be stored in class_table */
219typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
220
221/* xc_cest_t to (zend_class_entry*) */
222#define CestToCePtr(st) (ZESW(\
223            &(st), \
224            st \
225            ) )
226
227/* ZCEP=zend class entry ptr */
228#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
229            (pce)->refcount, \
230            &((pce)->refcount) \
231            ))
232
233#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
234
235typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
236
[311]237typedef struct _xc_entry_t xc_entry_t;
238typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
[1]239/* {{{ xc_cache_t */
[11]240typedef struct {
[1]241    int cacheid;
242    xc_hash_t  *hcache; /* hash to cacheid */
243
[11]244    time_t     compiling;
[1]245    zend_ulong misses;
246    zend_ulong hits;
247    zend_ulong clogs;
248    zend_ulong ooms;
[456]249    zend_ulong errors;
[1]250    xc_lock_t  *lck;
251    xc_shm_t   *shm; /* to which shm contains us */
252    xc_mem_t   *mem; /* to which mem contains us */
253
254    xc_entry_t **entries;
[32]255    int entries_count;
[311]256    xc_entry_data_php_t **phps;
257    int phps_count;
[1]258    xc_entry_t *deletes;
[32]259    int deletes_count;
[311]260    xc_hash_t  *hentry; /* hash settings to entry */
261    xc_hash_t  *hphp;   /* hash settings to php */
[114]262
263    time_t     last_gc_deletes;
264    time_t     last_gc_expires;
[522]265
266    time_t     hits_by_hour_cur_time;
267    zend_uint  hits_by_hour_cur_slot;
268    zend_ulong hits_by_hour[24];
269    time_t     hits_by_second_cur_time;
270    zend_uint  hits_by_second_cur_slot;
271    zend_ulong hits_by_second[5];
[1]272} xc_cache_t;
273/* }}} */
[662]274/* {{{ xc_op_array_info_t */
275typedef struct {
[721]276#ifdef ZEND_ENGINE_2_4
277    zend_uint literalinfo_cnt;
278    int *literalinfos;
279#else
[662]280    zend_uint oplineinfo_cnt;
281    int *oplineinfos;
[721]282#endif
[662]283} xc_op_array_info_t;
284/* }}} */
[1]285/* {{{ xc_classinfo_t */
286typedef struct {
[14]287#ifdef IS_UNICODE
288    zend_uchar type;
289#endif
[103]290    zstr      key;
[1]291    zend_uint key_size;
[506]292    ulong     h;
[662]293    zend_uint methodinfo_cnt;
294    xc_op_array_info_t *methodinfos;
[1]295    xc_cest_t cest;
[548]296#ifndef ZEND_COMPILE_DELAYED_BINDING
[212]297    int       oplineno;
[548]298#endif
[1]299} xc_classinfo_t;
300/* }}} */
[95]301#ifdef HAVE_XCACHE_CONSTANT
302/* {{{ xc_constinfo_t */
303typedef struct {
304#ifdef IS_UNICODE
305    zend_uchar type;
306#endif
[103]307    zstr      key;
[95]308    zend_uint key_size;
[506]309    ulong     h;
[95]310    zend_constant constant;
311} xc_constinfo_t;
312/* }}} */
313#endif
[1]314/* {{{ xc_funcinfo_t */
315typedef struct {
[14]316#ifdef IS_UNICODE
317    zend_uchar type;
318#endif
[103]319    zstr      key;
[1]320    zend_uint key_size;
[506]321    ulong     h;
[662]322    xc_op_array_info_t op_array_info;
[1]323    zend_function func;
324} xc_funcinfo_t;
325/* }}} */
[268]326#ifdef ZEND_ENGINE_2_1
327/* {{{ xc_autoglobal_t */
328typedef struct {
329#ifdef IS_UNICODE
330    zend_uchar type;
331#endif
332    zstr       key;
333    zend_uint  key_len;
[506]334    ulong      h;
[268]335} xc_autoglobal_t;
336/* }}} */
337#endif
[1]338typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
[311]339typedef char xc_md5sum_t[16];
[496]340/* {{{ xc_compilererror_t */
341typedef struct {
[648]342    int type;
[496]343    uint lineno;
344    int error_len;
345    char *error;
346} xc_compilererror_t;
347/* }}} */
[1]348/* {{{ xc_entry_data_php_t */
[311]349struct _xc_entry_data_php_t {
350    xc_hash_value_t hvalue; /* hash of md5 */
351    xc_entry_data_php_t *next;
352    xc_cache_t *cache;      /* which cache it's on */
353
354    xc_md5sum_t md5;        /* md5sum of the source */
355    zend_ulong  refcount;   /* count of entries referencing to this data */
356
[1]357    size_t sourcesize;
[311]358    zend_ulong hits;        /* hits of this php */
359    size_t     size;
[1]360
[662]361    xc_op_array_info_t op_array_info;
[1]362    zend_op_array *op_array;
363
[95]364#ifdef HAVE_XCACHE_CONSTANT
365    zend_uint constinfo_cnt;
366    xc_constinfo_t *constinfos;
367#endif
368
[1]369    zend_uint funcinfo_cnt;
370    xc_funcinfo_t *funcinfos;
371
372    zend_uint classinfo_cnt;
373    xc_classinfo_t *classinfos;
[548]374#ifndef ZEND_COMPILE_DELAYED_BINDING
[212]375    zend_bool have_early_binding;
[548]376#endif
[268]377
378#ifdef ZEND_ENGINE_2_1
379    zend_uint autoglobal_cnt;
380    xc_autoglobal_t *autoglobals;
381#endif
[311]382
[496]383#ifdef E_STRICT
384    zend_uint compilererror_cnt;
385    xc_compilererror_t *compilererrors;
386#endif
387
[311]388    zend_bool  have_references;
389};
[1]390/* }}} */
391/* {{{ xc_entry_data_var_t */
392typedef struct {
393    zval   *value;
[311]394
395    zend_bool  have_references;
[1]396} xc_entry_data_var_t;
397/* }}} */
398typedef zvalue_value xc_entry_name_t;
399/* {{{ xc_entry_t */
400struct _xc_entry_t {
401    xc_entry_type_t type;
402    xc_hash_value_t hvalue;
403    xc_entry_t *next;
404    xc_cache_t *cache;      /* which cache it's on */
405
406    size_t     size;
[311]407    zend_ulong refcount;    /* count of instances holding this entry */
[1]408    zend_ulong hits;
409    time_t     ctime;           /* the ctime of this entry */
410    time_t     atime;           /* the atime of this entry */
411    time_t     dtime;           /* the deletion time of this entry */
[114]412    long       ttl;             /* ttl of time entry, var only */
[1]413
414#ifdef IS_UNICODE
415    zend_uchar name_type;
416#endif
417    xc_entry_name_t name;
418
419    union {
420        xc_entry_data_php_t *php;
421        xc_entry_data_var_t *var;
422    } data;
[233]423
[311]424    time_t mtime;           /* the mtime of origin source file */
425#ifdef HAVE_INODE
426    int device;             /* the filesystem device */
427    int inode;              /* the filesystem inode */
428#endif
[684]429
430    /* php only */
431    int    filepath_len;
432    char  *filepath;
433    int    dirpath_len;
434    char  *dirpath;
435#ifdef IS_UNICODE
436    UChar *ufilepath;
437    int    ufilepath_len;
438    UChar *udirpath;
439    int    udirpath_len;
440#endif
441
[1]442};
443/* }}} */
444
445extern zend_module_entry xcache_module_entry;
446#define phpext_xcache_ptr &xcache_module_entry
447
448int xc_is_rw(const void *p);
449int xc_is_ro(const void *p);
450int xc_is_shm(const void *p);
[662]451/* {{{ xc_gc_op_array_t */
452typedef struct {
453#ifdef ZEND_ENGINE_2
454    zend_uint num_args;
455    zend_arg_info *arg_info;
456#endif
457    zend_op *opcodes;
458} xc_gc_op_array_t;
459/* }}} */
460void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC);
[684]461void xc_fix_op_array_info(const xc_entry_t *xce, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copied, const xc_op_array_info_t *op_array_info TSRMLS_DC);
[1]462
463#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.