source: tags/2.0.0-beta1/xcache.h @ 774

Last change on this file since 774 was 774, checked in by moo, 4 years ago

tagged 2.0.0-beta1

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