source: trunk/xcache.h @ 548

Last change on this file since 548 was 548, checked in by moo, 6 years ago

PHP_5_3 support: uses ZEND_COMPILE_IGNORE_INTERNAL_CLASSES and ZEND_COMPILE_DELAYED_BINDING stuffs for PHP_5_3 support finally

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