source: trunk/xcache.h @ 721

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

PHP_6: prepared in literal way for FILE DIR fix up

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