source: trunk/xcache.h @ 840

Last change on this file since 840 was 840, checked in by moo, 3 years ago

copy md5 digest correctly in notmemcpy mode

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