source: trunk/xcache.h @ 659

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

PHP 6 fix

  • Property svn:eol-style set to native
File size: 9.8 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/* {{{ 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
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                ))
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))
109#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
110#ifdef IS_UNICODE
111#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
112#else
113#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
114#endif
115#define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
116
117#ifndef IS_UNICODE
118typedef char *zstr;
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)
125#else
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)
132#endif
133
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
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
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
151#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
152       zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
153
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
157#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
158       zend_hash_find(ht, arKey, nKeyLength, pData)
159
160#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
161       zend_hash_quick_find(ht, arKey, nKeyLength, h, pData)
162
163#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
164        add_assoc_zval_ex(arg, key, key_len, value)
165
166#   define zend_u_is_auto_global(type, name, name_len) \
167        zend_is_auto_global(name, name_len)
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
203typedef struct _xc_entry_t xc_entry_t;
204typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
205/* {{{ xc_cache_t */
206typedef struct {
207    int cacheid;
208    xc_hash_t  *hcache; /* hash to cacheid */
209
210    time_t     compiling;
211    zend_ulong misses;
212    zend_ulong hits;
213    zend_ulong clogs;
214    zend_ulong ooms;
215    zend_ulong errors;
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;
221    int entries_count;
222    xc_entry_data_php_t **phps;
223    int phps_count;
224    xc_entry_t *deletes;
225    int deletes_count;
226    xc_hash_t  *hentry; /* hash settings to entry */
227    xc_hash_t  *hphp;   /* hash settings to php */
228
229    time_t     last_gc_deletes;
230    time_t     last_gc_expires;
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];
238} xc_cache_t;
239/* }}} */
240/* {{{ xc_classinfo_t */
241typedef struct {
242#ifdef IS_UNICODE
243    zend_uchar type;
244#endif
245    zstr      key;
246    zend_uint key_size;
247    ulong     h;
248    xc_cest_t cest;
249#ifndef ZEND_COMPILE_DELAYED_BINDING
250    int       oplineno;
251#endif
252} xc_classinfo_t;
253/* }}} */
254#ifdef HAVE_XCACHE_CONSTANT
255/* {{{ xc_constinfo_t */
256typedef struct {
257#ifdef IS_UNICODE
258    zend_uchar type;
259#endif
260    zstr      key;
261    zend_uint key_size;
262    ulong     h;
263    zend_constant constant;
264} xc_constinfo_t;
265/* }}} */
266#endif
267/* {{{ xc_funcinfo_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_function func;
276} xc_funcinfo_t;
277/* }}} */
278#ifdef ZEND_ENGINE_2_1
279/* {{{ xc_autoglobal_t */
280typedef struct {
281#ifdef IS_UNICODE
282    zend_uchar type;
283#endif
284    zstr       key;
285    zend_uint  key_len;
286    ulong      h;
287} xc_autoglobal_t;
288/* }}} */
289#endif
290typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
291typedef char xc_md5sum_t[16];
292/* {{{ xc_compilererror_t */
293typedef struct {
294    int type;
295    uint lineno;
296    int error_len;
297    char *error;
298} xc_compilererror_t;
299/* }}} */
300/* {{{ xc_entry_data_php_t */
301struct _xc_entry_data_php_t {
302    xc_hash_value_t hvalue; /* hash of md5 */
303    xc_entry_data_php_t *next;
304    xc_cache_t *cache;      /* which cache it's on */
305
306    xc_md5sum_t md5;        /* md5sum of the source */
307    zend_ulong  refcount;   /* count of entries referencing to this data */
308
309    size_t sourcesize;
310    zend_ulong hits;        /* hits of this php */
311    size_t     size;
312
313    zend_op_array *op_array;
314
315#ifdef HAVE_XCACHE_CONSTANT
316    zend_uint constinfo_cnt;
317    xc_constinfo_t *constinfos;
318#endif
319
320    zend_uint funcinfo_cnt;
321    xc_funcinfo_t *funcinfos;
322
323    zend_uint classinfo_cnt;
324    xc_classinfo_t *classinfos;
325#ifndef ZEND_COMPILE_DELAYED_BINDING
326    zend_bool have_early_binding;
327#endif
328
329#ifdef ZEND_ENGINE_2_1
330    zend_uint autoglobal_cnt;
331    xc_autoglobal_t *autoglobals;
332#endif
333
334#ifdef E_STRICT
335    zend_uint compilererror_cnt;
336    xc_compilererror_t *compilererrors;
337#endif
338
339    zend_bool  have_references;
340};
341/* }}} */
342/* {{{ xc_entry_data_var_t */
343typedef struct {
344    zval   *value;
345
346    zend_bool  have_references;
347} xc_entry_data_var_t;
348/* }}} */
349typedef zvalue_value xc_entry_name_t;
350/* {{{ xc_entry_t */
351struct _xc_entry_t {
352    xc_entry_type_t type;
353    xc_hash_value_t hvalue;
354    xc_entry_t *next;
355    xc_cache_t *cache;      /* which cache it's on */
356
357    size_t     size;
358    zend_ulong refcount;    /* count of instances holding this entry */
359    zend_ulong hits;
360    time_t     ctime;           /* the ctime of this entry */
361    time_t     atime;           /* the atime of this entry */
362    time_t     dtime;           /* the deletion time of this entry */
363    long       ttl;             /* ttl of time entry, var only */
364
365#ifdef IS_UNICODE
366    zend_uchar name_type;
367#endif
368    xc_entry_name_t name;
369
370    union {
371        xc_entry_data_php_t *php;
372        xc_entry_data_var_t *var;
373    } data;
374
375    time_t mtime;           /* the mtime of origin source file */
376#ifdef HAVE_INODE
377    int device;             /* the filesystem device */
378    int inode;              /* the filesystem inode */
379#endif
380};
381/* }}} */
382
383extern zend_module_entry xcache_module_entry;
384#define phpext_xcache_ptr &xcache_module_entry
385
386int xc_is_rw(const void *p);
387int xc_is_ro(const void *p);
388int xc_is_shm(const void *p);
389void xc_gc_add_op_array(zend_op_array *op_array TSRMLS_DC);
390
391#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.