source: trunk/xcache.h @ 716

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

PHP 6: initial catch up with php 6 literals and trait

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