source: trunk/xcache.h @ 662

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

back patch __FILE and __DIR on cache restore

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