source: trunk/xcache.h @ 798

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

fix build: ISREF undefined in <=PHP_5_2

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