source: trunk/xcache.h @ 769

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

fix type of op_type mismsatch for PHP 5-, assert before changing opline_num

  • Property svn:eol-style set to native
File size: 11.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 == 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(b)      (UNISW((b)->arKey, (b)->key.arKey))
128#define BUCKET_KEY_S(b)    ZSTR_S(BUCKET_KEY(b))
129#define BUCKET_KEY_U(b)    ZSTR_U(BUCKET_KEY(b))
130#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
131#ifdef IS_UNICODE
132#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
133#else
134#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
135#endif
136#define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
137
138#ifndef IS_UNICODE
139typedef char *zstr;
140#   define ZSTR_S(s)     (s)
141#   define ZSTR_U(s)     (s)
142#   define ZSTR_V(s)     (s)
143#   define ZSTR_PS(s)    (s)
144#   define ZSTR_PU(s)    (s)
145#   define ZSTR_PV(s)    (s)
146#else
147#   define ZSTR_S(zs)    ((zs).s)
148#   define ZSTR_U(zs)    ((zs).u)
149#   define ZSTR_V(zs)    ((zs).v)
150#   define ZSTR_PS(pzs)  ((pzs)->s)
151#   define ZSTR_PU(pzs)  ((pzs)->u)
152#   define ZSTR_PV(pzs)  ((pzs)->v)
153#endif
154
155#ifndef ZSTR
156#   define ZSTR(s)      (s)
157#endif
158
159#ifndef Z_UNIVAL
160#   define Z_UNIVAL(zval) (zval).value.str.val
161#   define Z_UNILEN(zval) (zval).value.str.len
162#endif
163
164/* {{{ u hash wrapper */
165#ifndef IS_UNICODE
166#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
167       zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest)
168
169#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
170       zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
171
172#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
173       zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
174
175#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
176       zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
177
178#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
179       zend_hash_find(ht, arKey, nKeyLength, pData)
180
181#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
182       zend_hash_quick_find(ht, arKey, nKeyLength, h, pData)
183
184#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
185        add_assoc_zval_ex(arg, key, key_len, value)
186
187#   define zend_u_is_auto_global(type, name, name_len) \
188        zend_is_auto_global(name, name_len)
189#endif
190/* }}} */
191
192
193#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
194#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
195
196
197
198typedef ulong xc_hash_value_t;
199typedef struct {
200    int bits;
201    int size;
202    int mask;
203} xc_hash_t;
204
205/* the class entry type to be stored in class_table */
206typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
207
208/* xc_cest_t to (zend_class_entry*) */
209#define CestToCePtr(st) (ZESW(\
210            &(st), \
211            st \
212            ) )
213
214/* ZCEP=zend class entry ptr */
215#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
216            (pce)->refcount, \
217            &((pce)->refcount) \
218            ))
219
220#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
221
222typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
223
224typedef struct _xc_entry_t xc_entry_t;
225typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
226/* {{{ xc_cache_t */
227typedef struct {
228    int cacheid;
229    xc_hash_t  *hcache; /* hash to cacheid */
230
231    time_t     compiling;
232    zend_ulong misses;
233    zend_ulong hits;
234    zend_ulong clogs;
235    zend_ulong ooms;
236    zend_ulong errors;
237    xc_lock_t  *lck;
238    xc_shm_t   *shm; /* to which shm contains us */
239    xc_mem_t   *mem; /* to which mem contains us */
240
241    xc_entry_t **entries;
242    int entries_count;
243    xc_entry_data_php_t **phps;
244    int phps_count;
245    xc_entry_t *deletes;
246    int deletes_count;
247    xc_hash_t  *hentry; /* hash settings to entry */
248    xc_hash_t  *hphp;   /* hash settings to php */
249
250    time_t     last_gc_deletes;
251    time_t     last_gc_expires;
252
253    time_t     hits_by_hour_cur_time;
254    zend_uint  hits_by_hour_cur_slot;
255    zend_ulong hits_by_hour[24];
256    time_t     hits_by_second_cur_time;
257    zend_uint  hits_by_second_cur_slot;
258    zend_ulong hits_by_second[5];
259} xc_cache_t;
260/* }}} */
261/* {{{ xc_op_array_info_t */
262typedef struct {
263#ifdef ZEND_ENGINE_2_4
264    zend_uint literalinfo_cnt;
265    int *literalinfos;
266#else
267    zend_uint oplineinfo_cnt;
268    int *oplineinfos;
269#endif
270} xc_op_array_info_t;
271/* }}} */
272/* {{{ xc_classinfo_t */
273typedef struct {
274#ifdef IS_UNICODE
275    zend_uchar type;
276#endif
277    zstr      key;
278    zend_uint key_size;
279    ulong     h;
280    zend_uint methodinfo_cnt;
281    xc_op_array_info_t *methodinfos;
282    xc_cest_t cest;
283#ifndef ZEND_COMPILE_DELAYED_BINDING
284    int       oplineno;
285#endif
286} xc_classinfo_t;
287/* }}} */
288#ifdef HAVE_XCACHE_CONSTANT
289/* {{{ xc_constinfo_t */
290typedef struct {
291#ifdef IS_UNICODE
292    zend_uchar type;
293#endif
294    zstr      key;
295    zend_uint key_size;
296    ulong     h;
297    zend_constant constant;
298} xc_constinfo_t;
299/* }}} */
300#endif
301/* {{{ xc_funcinfo_t */
302typedef struct {
303#ifdef IS_UNICODE
304    zend_uchar type;
305#endif
306    zstr      key;
307    zend_uint key_size;
308    ulong     h;
309    xc_op_array_info_t op_array_info;
310    zend_function func;
311} xc_funcinfo_t;
312/* }}} */
313#ifdef ZEND_ENGINE_2_1
314/* {{{ xc_autoglobal_t */
315typedef struct {
316#ifdef IS_UNICODE
317    zend_uchar type;
318#endif
319    zstr       key;
320    zend_uint  key_len;
321    ulong      h;
322} xc_autoglobal_t;
323/* }}} */
324#endif
325typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
326typedef char xc_md5sum_t[16];
327/* {{{ xc_compilererror_t */
328typedef struct {
329    int type;
330    uint lineno;
331    int error_len;
332    char *error;
333} xc_compilererror_t;
334/* }}} */
335/* {{{ xc_entry_data_php_t */
336struct _xc_entry_data_php_t {
337    xc_hash_value_t hvalue; /* hash of md5 */
338    xc_entry_data_php_t *next;
339    xc_cache_t *cache;      /* which cache it's on */
340
341    xc_md5sum_t md5;        /* md5sum of the source */
342    zend_ulong  refcount;   /* count of entries referencing to this data */
343
344    size_t sourcesize;
345    zend_ulong hits;        /* hits of this php */
346    size_t     size;
347
348    xc_op_array_info_t op_array_info;
349    zend_op_array *op_array;
350
351#ifdef HAVE_XCACHE_CONSTANT
352    zend_uint constinfo_cnt;
353    xc_constinfo_t *constinfos;
354#endif
355
356    zend_uint funcinfo_cnt;
357    xc_funcinfo_t *funcinfos;
358
359    zend_uint classinfo_cnt;
360    xc_classinfo_t *classinfos;
361#ifndef ZEND_COMPILE_DELAYED_BINDING
362    zend_bool have_early_binding;
363#endif
364
365#ifdef ZEND_ENGINE_2_1
366    zend_uint autoglobal_cnt;
367    xc_autoglobal_t *autoglobals;
368#endif
369
370#ifdef E_STRICT
371    zend_uint compilererror_cnt;
372    xc_compilererror_t *compilererrors;
373#endif
374
375    zend_bool  have_references;
376};
377/* }}} */
378/* {{{ xc_entry_data_var_t */
379typedef struct {
380    zval   *value;
381
382    zend_bool  have_references;
383} xc_entry_data_var_t;
384/* }}} */
385typedef zvalue_value xc_entry_name_t;
386/* {{{ xc_entry_t */
387struct _xc_entry_t {
388    xc_entry_type_t type;
389    xc_hash_value_t hvalue;
390    xc_entry_t *next;
391    xc_cache_t *cache;      /* which cache it's on */
392
393    size_t     size;
394    zend_ulong refcount;    /* count of instances holding this entry */
395    zend_ulong hits;
396    time_t     ctime;           /* the ctime of this entry */
397    time_t     atime;           /* the atime of this entry */
398    time_t     dtime;           /* the deletion time of this entry */
399    long       ttl;             /* ttl of time entry, var only */
400
401#ifdef IS_UNICODE
402    zend_uchar name_type;
403#endif
404    xc_entry_name_t name;
405
406    union {
407        xc_entry_data_php_t *php;
408        xc_entry_data_var_t *var;
409    } data;
410
411    time_t mtime;           /* the mtime of origin source file */
412#ifdef HAVE_INODE
413    int device;             /* the filesystem device */
414    int inode;              /* the filesystem inode */
415#endif
416
417    /* php only */
418    int    filepath_len;
419    char  *filepath;
420    int    dirpath_len;
421    char  *dirpath;
422#ifdef IS_UNICODE
423    UChar *ufilepath;
424    int    ufilepath_len;
425    UChar *udirpath;
426    int    udirpath_len;
427#endif
428
429};
430/* }}} */
431
432extern zend_module_entry xcache_module_entry;
433#define phpext_xcache_ptr &xcache_module_entry
434
435int xc_is_rw(const void *p);
436int xc_is_ro(const void *p);
437int xc_is_shm(const void *p);
438/* {{{ xc_gc_op_array_t */
439typedef struct {
440#ifdef ZEND_ENGINE_2
441    zend_uint num_args;
442    zend_arg_info *arg_info;
443#endif
444    zend_uint last;
445    zend_op *opcodes;
446} xc_gc_op_array_t;
447/* }}} */
448void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC);
449void xc_fix_op_array_info(const xc_entry_t *xce, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copied, const xc_op_array_info_t *op_array_info TSRMLS_DC);
450
451#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.