source: trunk/xcache.h @ 982

Last change on this file since 982 was 982, checked in by moo, 2 years ago

move files to subdirs

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