source: branches/1.3/xcache.h @ 738

Last change on this file since 738 was 738, checked in by moo, 4 years ago

update copyright

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