source: branches/1.3/xcache.h @ 772

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

merge [769] from trunk

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1#ifndef __XCACHE_H
2#define __XCACHE_H
3#define XCACHE_NAME       "XCache"
4#define XCACHE_VERSION    "1.3.2-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_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#   define Z_OP_TYPEOF_TYPE zend_uchar
100
101#   define Z_CLASS_INFO(className) (className).info.user
102#else
103#   define Z_OP(op) (op).u
104#   define Z_OP_CONSTANT(op) (op).u.constant
105#   define Z_OP_TYPE(op) (op).op_type
106#   define Z_OP_TYPEOF_TYPE int
107typedef znode znode_op;
108
109#   define Z_CLASS_INFO(className) (className)
110#endif
111
112/* }}} */
113
114/* unicode */
115#ifdef IS_UNICODE
116#   define UNISW(text, unicode) unicode
117#else
118#   define UNISW(text, unicode) text
119#endif
120#define BUCKET_KEY_SIZE(b) \
121        (UNISW( \
122            (b)->nKeyLength, \
123                ((b)->key.type == IS_UNICODE) \
124                ? UBYTES(b->nKeyLength) \
125                : b->nKeyLength \
126                ))
127#define BUCKET_KEY_S(b)    (UNISW((b)->arKey, (b)->key.arKey.s))
128#define BUCKET_KEY_U(b)    (UNISW((b)->arKey, (b)->key.arKey.u))
129#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
130#ifdef IS_UNICODE
131#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
132#else
133#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
134#endif
135#define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
136
137#ifndef IS_UNICODE
138typedef char *zstr;
139#   define ZSTR_S(s)     (s)
140#   define ZSTR_U(s)     (s)
141#   define ZSTR_V(s)     (s)
142#   define ZSTR_PS(s)    (s)
143#   define ZSTR_PU(s)    (s)
144#   define ZSTR_PV(s)    (s)
145#else
146#   define ZSTR_S(zs)    ((zs).s)
147#   define ZSTR_U(zs)    ((zs).u)
148#   define ZSTR_V(zs)    ((zs).v)
149#   define ZSTR_PS(pzs)  ((pzs)->s)
150#   define ZSTR_PU(pzs)  ((pzs)->u)
151#   define ZSTR_PV(pzs)  ((pzs)->v)
152#endif
153
154#ifndef ZSTR
155#   define ZSTR(s)      (s)
156#endif
157
158#ifndef Z_UNIVAL
159#   define Z_UNIVAL(zval) (zval).value.str.val
160#   define Z_UNILEN(zval) (zval).value.str.len
161#endif
162
163/* {{{ u hash wrapper */
164#ifndef IS_UNICODE
165#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
166       zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest)
167
168#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
169       zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
170
171#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
172       zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
173
174#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
175       zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
176
177#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
178       zend_hash_find(ht, arKey, nKeyLength, pData)
179
180#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
181       zend_hash_quick_find(ht, arKey, nKeyLength, h, pData)
182
183#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
184        add_assoc_zval_ex(arg, key, key_len, value)
185
186#   define zend_u_is_auto_global(type, name, name_len) \
187        zend_is_auto_global(name, name_len)
188#endif
189/* }}} */
190
191
192#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
193#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
194
195
196
197typedef ulong xc_hash_value_t;
198typedef struct {
199    int bits;
200    int size;
201    int mask;
202} xc_hash_t;
203
204/* the class entry type to be stored in class_table */
205typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
206
207/* xc_cest_t to (zend_class_entry*) */
208#define CestToCePtr(st) (ZESW(\
209            &(st), \
210            st \
211            ) )
212
213/* ZCEP=zend class entry ptr */
214#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
215            (pce)->refcount, \
216            &((pce)->refcount) \
217            ))
218
219#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
220
221typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
222
223/* {{{ xc_cache_t */
224typedef struct _xc_entry_t xc_entry_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_t *deletes;
242    int deletes_count;
243    xc_hash_t  *hentry; /* hash to entry */
244
245    time_t     last_gc_deletes;
246    time_t     last_gc_expires;
247
248    time_t     hits_by_hour_cur_time;
249    zend_uint  hits_by_hour_cur_slot;
250    zend_ulong hits_by_hour[24];
251    time_t     hits_by_second_cur_time;
252    zend_uint  hits_by_second_cur_slot;
253    zend_ulong hits_by_second[5];
254} xc_cache_t;
255/* }}} */
256/* {{{ xc_classinfo_t */
257typedef struct {
258#ifdef IS_UNICODE
259    zend_uchar type;
260#endif
261    zstr      key;
262    zend_uint key_size;
263    ulong     h;
264    xc_cest_t cest;
265#ifndef ZEND_COMPILE_DELAYED_BINDING
266    int       oplineno;
267#endif
268} xc_classinfo_t;
269/* }}} */
270#ifdef HAVE_XCACHE_CONSTANT
271/* {{{ xc_constinfo_t */
272typedef struct {
273#ifdef IS_UNICODE
274    zend_uchar type;
275#endif
276    zstr      key;
277    zend_uint key_size;
278    ulong     h;
279    zend_constant constant;
280} xc_constinfo_t;
281/* }}} */
282#endif
283/* {{{ xc_funcinfo_t */
284typedef struct {
285#ifdef IS_UNICODE
286    zend_uchar type;
287#endif
288    zstr      key;
289    zend_uint key_size;
290    ulong     h;
291    zend_function func;
292} xc_funcinfo_t;
293/* }}} */
294#ifdef ZEND_ENGINE_2_1
295/* {{{ xc_autoglobal_t */
296typedef struct {
297#ifdef IS_UNICODE
298    zend_uchar type;
299#endif
300    zstr       key;
301    zend_uint  key_len;
302    ulong      h;
303} xc_autoglobal_t;
304/* }}} */
305#endif
306typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
307/* {{{ xc_entry_data_php_t */
308typedef struct {
309    size_t sourcesize;
310#ifdef HAVE_INODE
311    int device;             /* the filesystem device */
312    int inode;              /* the filesystem inode */
313#endif
314    time_t mtime;           /* the mtime of origin source file */
315
316    zend_op_array *op_array;
317
318#ifdef HAVE_XCACHE_CONSTANT
319    zend_uint constinfo_cnt;
320    xc_constinfo_t *constinfos;
321#endif
322
323    zend_uint funcinfo_cnt;
324    xc_funcinfo_t *funcinfos;
325
326    zend_uint classinfo_cnt;
327    xc_classinfo_t *classinfos;
328#ifndef ZEND_COMPILE_DELAYED_BINDING
329    zend_bool have_early_binding;
330#endif
331
332#ifdef ZEND_ENGINE_2_1
333    zend_uint autoglobal_cnt;
334    xc_autoglobal_t *autoglobals;
335#endif
336} xc_entry_data_php_t;
337/* }}} */
338/* {{{ xc_entry_data_var_t */
339typedef struct {
340    zval   *value;
341} xc_entry_data_var_t;
342/* }}} */
343typedef zvalue_value xc_entry_name_t;
344/* {{{ xc_entry_t */
345struct _xc_entry_t {
346    xc_entry_type_t type;
347    xc_hash_value_t hvalue;
348    xc_entry_t *next;
349    xc_cache_t *cache;      /* which cache it's on */
350
351    size_t     size;
352    zend_ulong refcount;
353    zend_ulong hits;
354    time_t     ctime;           /* the ctime of this entry */
355    time_t     atime;           /* the atime of this entry */
356    time_t     dtime;           /* the deletion time of this entry */
357    long       ttl;             /* ttl of time entry, var only */
358
359#ifdef IS_UNICODE
360    zend_uchar name_type;
361#endif
362    xc_entry_name_t name;
363
364    union {
365        xc_entry_data_php_t *php;
366        xc_entry_data_var_t *var;
367    } data;
368
369    zend_bool  have_references;
370};
371/* }}} */
372
373extern zend_module_entry xcache_module_entry;
374#define phpext_xcache_ptr &xcache_module_entry
375
376int xc_is_rw(const void *p);
377int xc_is_ro(const void *p);
378int xc_is_shm(const void *p);
379void xc_gc_add_op_array(zend_op_array *op_array TSRMLS_DC);
380
381#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.