source: trunk/xcache.h @ 866

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

is non-unicode only; prepare for handling include_path + stat=0; project state is now unstable

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