source: tags/2.0.1/xcache.h

Last change on this file was 968, checked in by moo, 2 years ago

updated version/date

  • Property svn:eol-style set to native
File size: 12.0 KB
Line 
1#ifndef __XCACHE_H
2#define __XCACHE_H
3#define XCACHE_NAME       "XCache"
4#ifndef XCACHE_VERSION
5#   define XCACHE_VERSION "2.0.1"
6#endif
7#define XCACHE_AUTHOR     "mOo"
8#define XCACHE_COPYRIGHT  "Copyright (c) 2005-2012"
9#define XCACHE_URL        "http://xcache.lighttpd.net"
10#define XCACHE_WIKI_URL   XCACHE_URL "/wiki"
11
12#include <php.h>
13#include <zend_compile.h>
14#include <zend_API.h>
15#include <zend.h>
16#include "php_ini.h"
17#include "zend_hash.h"
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22#include "xc_shm.h"
23#include "lock.h"
24
25#if !defined(ZEND_ENGINE_2_4) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4 || PHP_MAJOR_VERSION > 5)
26#   define ZEND_ENGINE_2_4
27#endif
28#if !defined(ZEND_ENGINE_2_3) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 3 || defined(ZEND_ENGINE_2_4))
29#   define ZEND_ENGINE_2_3
30#endif
31#if !defined(ZEND_ENGINE_2_2) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 2 || defined(ZEND_ENGINE_2_3))
32#   define ZEND_ENGINE_2_2
33#endif
34#if !defined(ZEND_ENGINE_2_1) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 || defined(ZEND_ENGINE_2_2))
35#   define ZEND_ENGINE_2_1
36#endif
37
38#define NOTHING
39/* ZendEngine code Switcher */
40#ifndef ZEND_ENGINE_2
41#   define ZESW(v1, v2) v1
42#else
43#   define ZESW(v1, v2) v2
44#endif
45#ifdef ZEND_ENGINE_2_4
46#   define ZEND_24(pre24, v24) v24
47#else
48#   define ZEND_24(pre24, v24) pre24
49#endif
50
51#ifdef do_alloca_with_limit
52#   define my_do_alloca(size, use_heap) do_alloca_with_limit(size, use_heap)
53#   define my_free_alloca(size, use_heap) free_alloca_with_limit(size, use_heap)
54#elif defined(ALLOCA_FLAG)
55#   define my_do_alloca(size, use_heap) do_alloca(size, use_heap)
56#   define my_free_alloca(size, use_heap) free_alloca(size, use_heap)
57#else
58#   define my_do_alloca(size, use_heap) do_alloca(size)
59#   define my_free_alloca(size, use_heap) free_alloca(size)
60#   define ALLOCA_FLAG(x)
61#endif
62#ifndef Z_ISREF
63#   define Z_ISREF(z) (z).is_ref
64#endif
65#ifndef Z_SET_ISREF
66#   define Z_SET_ISREF(z) (z).is_ref = 1
67#endif
68#ifndef Z_UNSET_ISREF
69#   define Z_UNSET_ISREF(z) (z).is_ref = 0
70#endif
71#ifndef Z_REFCOUNT
72#   define Z_REFCOUNT(z) (z).refcount
73#endif
74#ifndef Z_SET_REFCOUNT
75#   define Z_SET_REFCOUNT(z, rc) (z).refcount = rc
76#endif
77#ifndef IS_CONSTANT_TYPE_MASK
78#   define IS_CONSTANT_TYPE_MASK (~IS_CONSTANT_INDEX)
79#endif
80
81/* {{{ dirty fix for PHP 6 */
82#ifdef add_assoc_long_ex
83static inline void my_add_assoc_long_ex(zval *arg, char *key, uint key_len, long value)
84{
85    add_assoc_long_ex(arg, key, key_len, value);
86}
87#   undef add_assoc_long_ex
88#   define add_assoc_long_ex my_add_assoc_long_ex
89#endif
90#ifdef add_assoc_bool_ex
91static inline void my_add_assoc_bool_ex(zval *arg, char *key, uint key_len, zend_bool value)
92{
93    add_assoc_bool_ex(arg, key, key_len, value);
94}
95#   undef add_assoc_bool_ex
96#   define add_assoc_bool_ex my_add_assoc_bool_ex
97#endif
98#ifdef add_assoc_null_ex
99static inline void my_add_assoc_null_ex(zval *arg, char *key, uint key_len)
100{
101    add_assoc_null_ex(arg, key, key_len);
102}
103#   undef add_assoc_null_ex
104#   define add_assoc_null_ex my_add_assoc_null_ex
105#endif
106
107#ifdef ZEND_ENGINE_2_4
108#   define Z_OP(op) (op)
109#   define Z_OP_CONSTANT(op) (op).literal->constant
110#   define Z_OP_TYPE(op) op##_##type
111#   define Z_OP_TYPEOF_TYPE zend_uchar
112
113#   define Z_CLASS_INFO(className) (className).info.user
114#else
115#   define Z_OP(op) (op).u
116#   define Z_OP_CONSTANT(op) (op).u.constant
117#   define Z_OP_TYPE(op) (op).op_type
118#   define Z_OP_TYPEOF_TYPE int
119typedef znode znode_op;
120
121#   define Z_CLASS_INFO(className) (className)
122#endif
123
124/* }}} */
125
126/* unicode */
127#ifdef IS_UNICODE
128#   define UNISW(text, unicode) unicode
129#else
130#   define UNISW(text, unicode) text
131#endif
132#define BUCKET_KEY_SIZE(b) \
133        (UNISW( \
134            (b)->nKeyLength, \
135                ((b)->key.type == IS_UNICODE) \
136                ? UBYTES(b->nKeyLength) \
137                : b->nKeyLength \
138                ))
139#define BUCKET_KEY(b)      (UNISW((b)->arKey, (b)->key.arKey))
140#define BUCKET_KEY_S(b)    ZSTR_S(BUCKET_KEY(b))
141#define BUCKET_KEY_U(b)    ZSTR_U(BUCKET_KEY(b))
142#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
143#ifdef IS_UNICODE
144#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
145#else
146#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
147#endif
148
149#ifdef ZEND_ENGINE_2_4
150#   define BUCKET_SIZE(b) (sizeof(Bucket) + BUCKET_KEY_SIZE(b))
151#else
152#   define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
153#endif
154
155#ifndef IS_UNICODE
156typedef char *zstr;
157typedef const char *const_zstr;
158#ifdef ZEND_ENGINE_2_4
159typedef const char *const24_zstr;
160typedef const char *const24_str;
161#else
162typedef char *const24_zstr;
163typedef char *const24_str;
164#endif
165
166#   define ZSTR_S(s)     (s)
167#   define ZSTR_U(s)     (s)
168#   define ZSTR_V(s)     (s)
169#   define ZSTR_PS(s)    (s)
170#   define ZSTR_PU(s)    (s)
171#   define ZSTR_PV(s)    (s)
172#else
173typedef const zstr const_zstr;
174#   define ZSTR_S(zs)    ((zs).s)
175#   define ZSTR_U(zs)    ((zs).u)
176#   define ZSTR_V(zs)    ((zs).v)
177#   define ZSTR_PS(pzs)  ((pzs)->s)
178#   define ZSTR_PU(pzs)  ((pzs)->u)
179#   define ZSTR_PV(pzs)  ((pzs)->v)
180#endif
181
182#ifndef ZSTR
183#   define ZSTR(s)      (s)
184#endif
185
186#ifndef Z_UNIVAL
187#   define Z_UNIVAL(zval) (zval).value.str.val
188#   define Z_UNILEN(zval) (zval).value.str.len
189#endif
190
191/* {{{ u hash wrapper */
192#ifndef IS_UNICODE
193#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
194       zend_hash_add(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, pData, nDataSize, pDest)
195
196#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
197       zend_hash_quick_add(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, h, pData, nDataSize, pDest)
198
199#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
200       zend_hash_update(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, pData, nDataSize, pDest)
201
202#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
203       zend_hash_quick_update(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, h, pData, nDataSize, pDest)
204
205#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
206       zend_hash_find(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, pData)
207
208#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
209       zend_hash_quick_find(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength, h, pData)
210
211#   define zend_u_hash_exists(ht, type, arKey, nKeyLength) \
212       zend_hash_exists(ht, ZEND_24((char *), NOTHING) arKey, nKeyLength)
213
214#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
215        add_assoc_zval_ex(arg, key, key_len, value)
216
217#   define zend_u_is_auto_global(type, name, name_len) \
218        zend_is_auto_global(name, name_len)
219#endif
220/* }}} */
221
222
223#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
224#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
225
226
227
228typedef ulong xc_hash_value_t;
229typedef struct {
230    size_t bits;
231    size_t size;
232    xc_hash_value_t mask;
233} xc_hash_t;
234
235/* the class entry type to be stored in class_table */
236typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
237
238/* xc_cest_t to (zend_class_entry*) */
239#define CestToCePtr(st) (ZESW(\
240            &(st), \
241            st \
242            ) )
243
244/* ZCEP=zend class entry ptr */
245#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
246            (pce)->refcount, \
247            &((pce)->refcount) \
248            ))
249
250#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
251
252typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
253
254typedef struct _xc_entry_t xc_entry_t;
255typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
256/* {{{ xc_cache_t */
257typedef struct {
258    int cacheid;
259    xc_hash_t  *hcache; /* hash to cacheid */
260
261    time_t     compiling;
262    zend_ulong updates;
263    zend_ulong hits;
264    zend_ulong clogs;
265    zend_ulong ooms;
266    zend_ulong errors;
267    xc_lock_t  *lck;
268    xc_shm_t   *shm; /* which shm contains us */
269    xc_mem_t   *mem; /* which mem contains us */
270
271    xc_entry_t **entries;
272    int entries_count;
273    xc_entry_data_php_t **phps;
274    int phps_count;
275    xc_entry_t *deletes;
276    int deletes_count;
277    xc_hash_t  *hentry; /* hash settings to entry */
278    xc_hash_t  *hphp;   /* hash settings to php */
279
280    time_t     last_gc_deletes;
281    time_t     last_gc_expires;
282
283    time_t     hits_by_hour_cur_time;
284    zend_uint  hits_by_hour_cur_slot;
285    zend_ulong hits_by_hour[24];
286    time_t     hits_by_second_cur_time;
287    zend_uint  hits_by_second_cur_slot;
288    zend_ulong hits_by_second[5];
289} xc_cache_t;
290/* }}} */
291/* {{{ xc_op_array_info_detail_t */
292typedef struct {
293    zend_uint index;
294    zend_uint info;
295} xc_op_array_info_detail_t;
296/* }}} */
297/* {{{ xc_op_array_info_t */
298typedef struct {
299#ifdef ZEND_ENGINE_2_4
300    zend_uint literalinfo_cnt;
301    xc_op_array_info_detail_t *literalinfos;
302#else
303    zend_uint oplineinfo_cnt;
304    xc_op_array_info_detail_t *oplineinfos;
305#endif
306} xc_op_array_info_t;
307/* }}} */
308/* {{{ xc_classinfo_t */
309typedef struct {
310#ifdef IS_UNICODE
311    zend_uchar   type;
312#endif
313    const24_zstr key;
314    zend_uint    key_size;
315    ulong        h;
316    zend_uint  methodinfo_cnt;
317    xc_op_array_info_t *methodinfos;
318    xc_cest_t    cest;
319#ifndef ZEND_COMPILE_DELAYED_BINDING
320    int          oplineno;
321#endif
322} xc_classinfo_t;
323/* }}} */
324#ifdef HAVE_XCACHE_CONSTANT
325/* {{{ xc_constinfo_t */
326typedef struct {
327#ifdef IS_UNICODE
328    zend_uchar    type;
329#endif
330    const24_zstr  key;
331    zend_uint     key_size;
332    ulong         h;
333    zend_constant constant;
334} xc_constinfo_t;
335/* }}} */
336#endif
337/* {{{ xc_funcinfo_t */
338typedef struct {
339#ifdef IS_UNICODE
340    zend_uchar     type;
341#endif
342    const24_zstr   key;
343    zend_uint      key_size;
344    ulong          h;
345    xc_op_array_info_t op_array_info;
346    zend_function func;
347} xc_funcinfo_t;
348/* }}} */
349#ifdef ZEND_ENGINE_2_1
350/* {{{ xc_autoglobal_t */
351typedef struct {
352#ifdef IS_UNICODE
353    zend_uchar   type;
354#endif
355    const24_zstr key;
356    zend_uint    key_len;
357    ulong        h;
358} xc_autoglobal_t;
359/* }}} */
360#endif
361typedef struct {
362    char digest[16];
363} xc_md5sum_t;
364/* {{{ xc_compilererror_t */
365typedef struct {
366    int type;
367    uint lineno;
368    int error_len;
369    char *error;
370} xc_compilererror_t;
371/* }}} */
372/* {{{ xc_entry_data_php_t */
373struct _xc_entry_data_php_t {
374    xc_entry_data_php_t *next;
375    xc_hash_value_t      hvalue;
376
377    xc_md5sum_t md5;        /* md5sum of the source */
378    zend_ulong  refcount;   /* count of entries referencing to this data */
379
380    zend_ulong hits;        /* hits of this php */
381    size_t     size;
382
383    xc_op_array_info_t op_array_info;
384    zend_op_array *op_array;
385
386#ifdef HAVE_XCACHE_CONSTANT
387    zend_uint constinfo_cnt;
388    xc_constinfo_t *constinfos;
389#endif
390
391    zend_uint funcinfo_cnt;
392    xc_funcinfo_t *funcinfos;
393
394    zend_uint classinfo_cnt;
395    xc_classinfo_t *classinfos;
396#ifndef ZEND_COMPILE_DELAYED_BINDING
397    zend_bool have_early_binding;
398#endif
399
400#ifdef ZEND_ENGINE_2_1
401    zend_uint autoglobal_cnt;
402    xc_autoglobal_t *autoglobals;
403#endif
404
405#ifdef E_STRICT
406    zend_uint compilererror_cnt;
407    xc_compilererror_t *compilererrors;
408#endif
409
410    zend_bool  have_references;
411};
412/* }}} */
413typedef zvalue_value xc_entry_name_t;
414/* {{{ xc_entry_t */
415struct _xc_entry_t {
416    xc_entry_t *next;
417
418    size_t     size;
419    time_t     ctime;           /* creation ctime of this entry */
420    time_t     atime;           /*   access atime of this entry */
421    time_t     dtime;           /*  deletion time of this entry */
422    zend_ulong hits;
423    zend_ulong ttl;
424
425    xc_entry_name_t name;
426};
427
428typedef struct {
429    xc_entry_t entry;
430    xc_entry_data_php_t *php;
431
432    zend_ulong refcount;    /* count of php instances holding this entry */
433    time_t file_mtime;
434    size_t file_size;
435    int file_device;
436    int file_inode;
437
438    int    filepath_len;
439    ZEND_24(NOTHING, const) char *filepath;
440    int    dirpath_len;
441    char  *dirpath;
442#ifdef IS_UNICODE
443    int    ufilepath_len;
444    UChar *ufilepath;
445    int    udirpath_len;
446    UChar *udirpath;
447#endif
448} xc_entry_php_t;
449
450typedef struct {
451    xc_entry_t entry;
452#ifdef IS_UNICODE
453    zend_uchar name_type;
454#endif
455    zval      *value;
456    zend_bool  have_references;
457} xc_entry_var_t;
458/* }}} */
459typedef struct xc_entry_hash_t { /* {{{ */
460    xc_hash_value_t cacheid;
461    xc_hash_value_t entryslotid;
462} xc_entry_hash_t;
463/* }}} */
464
465extern zend_module_entry xcache_module_entry;
466#define phpext_xcache_ptr &xcache_module_entry
467
468int xc_is_rw(const void *p);
469int xc_is_ro(const void *p);
470int xc_is_shm(const void *p);
471/* {{{ xc_gc_op_array_t */
472typedef struct {
473#ifdef ZEND_ENGINE_2
474    zend_uint num_args;
475    zend_arg_info *arg_info;
476#endif
477    zend_op *opcodes;
478} xc_gc_op_array_t;
479/* }}} */
480void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC);
481void xc_fix_op_array_info(const xc_entry_php_t *xce, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copy, const xc_op_array_info_t *op_array_info TSRMLS_DC);
482
483#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.