source: trunk/xcache.h @ 684

Last change on this file since 684 was 684, checked in by moo, 5 years ago

the logic of implemenation of dir/file path fixing was wrong. file/dir path should be cached before hardlink

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