source: trunk/xcache.h @ 547

Last change on this file since 547 was 547, checked in by moo, 6 years ago

more verbose and user friendly error/warning note

  • Property svn:eol-style set to native
File size: 9.4 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-2007"
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_1) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1 || PHP_MAJOR_VERSION > 5)
25#   define ZEND_ENGINE_2_1
26#endif
27#if !defined(ZEND_ENGINE_2_2) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 2 || PHP_MAJOR_VERSION > 5)
28#   define ZEND_ENGINE_2_2
29#endif
30#if !defined(ZEND_ENGINE_2_3) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 3 || PHP_MAJOR_VERSION > 5)
31#   define ZEND_ENGINE_2_3
32#endif
33
34#define NOTHING
35/* ZendEngine code Switcher */
36#ifndef ZEND_ENGINE_2
37#   define ZESW(v1, v2) v1
38#else
39#   define ZESW(v1, v2) v2
40#endif
41
42#ifdef ALLOCA_FLAG
43#   define my_do_alloca(size, use_heap) do_alloca(size, use_heap)
44#   define my_free_alloca(size, use_heap) free_alloca(size, use_heap)
45#else
46#   define my_do_alloca(size, use_heap) do_alloca(size)
47#   define my_free_alloca(size, use_heap) free_alloca(size)
48#   define ALLOCA_FLAG(x)
49#endif
50#ifndef Z_SET_ISREF
51#   define Z_SET_ISREF(z) (z).is_ref = 1;
52#endif
53#ifndef Z_UNSET_ISREF
54#   define Z_UNSET_ISREF(z) (z).is_ref = 0;
55#endif
56#ifndef Z_SET_REFCOUNT
57#   define Z_SET_REFCOUNT(z, rc) (z).refcount = rc;
58#endif
59#ifndef IS_CONSTANT_TYPE_MASK
60#   define IS_CONSTANT_TYPE_MASK 0xf
61#endif
62
63/* {{{ dirty fix for PHP 6 */
64#ifdef add_assoc_long_ex
65static inline void my_add_assoc_long_ex(zval *arg, char *key, uint key_len, long value)
66{
67    add_assoc_long_ex(arg, key, key_len, value);
68}
69#   undef add_assoc_long_ex
70#   define add_assoc_long_ex my_add_assoc_long_ex
71#endif
72#ifdef add_assoc_bool_ex
73static inline void my_add_assoc_bool_ex(zval *arg, char *key, uint key_len, zend_bool value)
74{
75    add_assoc_bool_ex(arg, key, key_len, value);
76}
77#   undef add_assoc_bool_ex
78#   define add_assoc_bool_ex my_add_assoc_bool_ex
79#endif
80#ifdef add_assoc_null_ex
81static inline void my_add_assoc_null_ex(zval *arg, char *key, uint key_len)
82{
83    add_assoc_null_ex(arg, key, key_len);
84}
85#   undef add_assoc_null_ex
86#   define add_assoc_null_ex my_add_assoc_null_ex
87#endif
88/* }}} */
89
90/* unicode */
91#ifdef IS_UNICODE
92#   define UNISW(text, unicode) unicode
93#else
94#   define UNISW(text, unicode) text
95#endif
96#define BUCKET_KEY_SIZE(b) \
97        (UNISW( \
98            (b)->nKeyLength, \
99                ((b)->key.type == IS_UNICODE) \
100                ? UBYTES(b->nKeyLength) \
101                : b->nKeyLength \
102                ))
103#define BUCKET_KEY_S(b)    (UNISW((b)->arKey, (b)->key.arKey.s))
104#define BUCKET_KEY_U(b)    (UNISW((b)->arKey, (b)->key.arKey.u))
105#define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING,  (b)->key.type))
106#ifdef IS_UNICODE
107#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
108#else
109#   define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
110#endif
111#define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
112
113#ifndef IS_UNICODE
114typedef char *zstr;
115#   define ZSTR_S(s)     (s)
116#   define ZSTR_U(s)     (s)
117#   define ZSTR_V(s)     (s)
118#   define ZSTR_PS(s)    (s)
119#   define ZSTR_PU(s)    (s)
120#   define ZSTR_PV(s)    (s)
121#else
122#   define ZSTR_S(zs)    ((zs).s)
123#   define ZSTR_U(zs)    ((zs).u)
124#   define ZSTR_V(zs)    ((zs).v)
125#   define ZSTR_PS(pzs)  ((pzs)->s)
126#   define ZSTR_PU(pzs)  ((pzs)->u)
127#   define ZSTR_PV(pzs)  ((pzs)->v)
128#endif
129
130#ifndef ZSTR
131#   define ZSTR(s)      (s)
132#endif
133
134#ifndef Z_UNIVAL
135#   define Z_UNIVAL(zval) (zval).value.str.val
136#   define Z_UNILEN(zval) (zval).value.str.len
137#endif
138
139/* {{{ u hash wrapper */
140#ifndef IS_UNICODE
141#   define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
142       zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest)
143
144#   define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
145       zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
146
147#   define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
148       zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
149
150#   define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \
151       zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest)
152
153#   define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
154       zend_hash_find(ht, arKey, nKeyLength, pData)
155
156#   define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \
157       zend_hash_quick_find(ht, arKey, nKeyLength, h, pData)
158
159#   define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
160        add_assoc_zval_ex(arg, key, key_len, value)
161
162#   define zend_u_is_auto_global(type, name, name_len) \
163        zend_is_auto_global(name, name_len)
164#endif
165/* }}} */
166
167
168#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
169#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
170
171
172
173typedef ulong xc_hash_value_t;
174typedef struct {
175    int bits;
176    int size;
177    int mask;
178} xc_hash_t;
179
180/* the class entry type to be stored in class_table */
181typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
182
183/* xc_cest_t to (zend_class_entry*) */
184#define CestToCePtr(st) (ZESW(\
185            &(st), \
186            st \
187            ) )
188
189/* ZCEP=zend class entry ptr */
190#define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
191            (pce)->refcount, \
192            &((pce)->refcount) \
193            ))
194
195#define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
196
197typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
198
199typedef struct _xc_entry_t xc_entry_t;
200typedef struct _xc_entry_data_php_t xc_entry_data_php_t;
201/* {{{ xc_cache_t */
202typedef struct {
203    int cacheid;
204    xc_hash_t  *hcache; /* hash to cacheid */
205
206    time_t     compiling;
207    zend_ulong misses;
208    zend_ulong hits;
209    zend_ulong clogs;
210    zend_ulong ooms;
211    zend_ulong errors;
212    xc_lock_t  *lck;
213    xc_shm_t   *shm; /* to which shm contains us */
214    xc_mem_t   *mem; /* to which mem contains us */
215
216    xc_entry_t **entries;
217    int entries_count;
218    xc_entry_data_php_t **phps;
219    int phps_count;
220    xc_entry_t *deletes;
221    int deletes_count;
222    xc_hash_t  *hentry; /* hash settings to entry */
223    xc_hash_t  *hphp;   /* hash settings to php */
224
225    time_t     last_gc_deletes;
226    time_t     last_gc_expires;
227
228    time_t     hits_by_hour_cur_time;
229    zend_uint  hits_by_hour_cur_slot;
230    zend_ulong hits_by_hour[24];
231    time_t     hits_by_second_cur_time;
232    zend_uint  hits_by_second_cur_slot;
233    zend_ulong hits_by_second[5];
234} xc_cache_t;
235/* }}} */
236/* {{{ xc_classinfo_t */
237typedef struct {
238#ifdef IS_UNICODE
239    zend_uchar type;
240#endif
241    zstr      key;
242    zend_uint key_size;
243    ulong     h;
244    xc_cest_t cest;
245    int       oplineno;
246} xc_classinfo_t;
247/* }}} */
248#ifdef HAVE_XCACHE_CONSTANT
249/* {{{ xc_constinfo_t */
250typedef struct {
251#ifdef IS_UNICODE
252    zend_uchar type;
253#endif
254    zstr      key;
255    zend_uint key_size;
256    ulong     h;
257    zend_constant constant;
258} xc_constinfo_t;
259/* }}} */
260#endif
261/* {{{ xc_funcinfo_t */
262typedef struct {
263#ifdef IS_UNICODE
264    zend_uchar type;
265#endif
266    zstr      key;
267    zend_uint key_size;
268    ulong     h;
269    zend_function func;
270} xc_funcinfo_t;
271/* }}} */
272#ifdef ZEND_ENGINE_2_1
273/* {{{ xc_autoglobal_t */
274typedef struct {
275#ifdef IS_UNICODE
276    zend_uchar type;
277#endif
278    zstr       key;
279    zend_uint  key_len;
280    ulong      h;
281} xc_autoglobal_t;
282/* }}} */
283#endif
284typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
285typedef char xc_md5sum_t[16];
286/* {{{ xc_compilererror_t */
287typedef struct {
288    uint lineno;
289    int error_len;
290    char *error;
291} xc_compilererror_t;
292/* }}} */
293/* {{{ xc_entry_data_php_t */
294struct _xc_entry_data_php_t {
295    xc_hash_value_t hvalue; /* hash of md5 */
296    xc_entry_data_php_t *next;
297    xc_cache_t *cache;      /* which cache it's on */
298
299    xc_md5sum_t md5;        /* md5sum of the source */
300    zend_ulong  refcount;   /* count of entries referencing to this data */
301
302    size_t sourcesize;
303    zend_ulong hits;        /* hits of this php */
304    size_t     size;
305
306    zend_op_array *op_array;
307
308#ifdef HAVE_XCACHE_CONSTANT
309    zend_uint constinfo_cnt;
310    xc_constinfo_t *constinfos;
311#endif
312
313    zend_uint funcinfo_cnt;
314    xc_funcinfo_t *funcinfos;
315
316    zend_uint classinfo_cnt;
317    xc_classinfo_t *classinfos;
318    zend_bool have_early_binding;
319
320#ifdef ZEND_ENGINE_2_1
321    zend_uint autoglobal_cnt;
322    xc_autoglobal_t *autoglobals;
323#endif
324
325#ifdef E_STRICT
326    zend_uint compilererror_cnt;
327    xc_compilererror_t *compilererrors;
328#endif
329
330    zend_bool  have_references;
331};
332/* }}} */
333/* {{{ xc_entry_data_var_t */
334typedef struct {
335    zval   *value;
336
337    zend_bool  have_references;
338} xc_entry_data_var_t;
339/* }}} */
340typedef zvalue_value xc_entry_name_t;
341/* {{{ xc_entry_t */
342struct _xc_entry_t {
343    xc_entry_type_t type;
344    xc_hash_value_t hvalue;
345    xc_entry_t *next;
346    xc_cache_t *cache;      /* which cache it's on */
347
348    size_t     size;
349    zend_ulong refcount;    /* count of instances holding this entry */
350    zend_ulong hits;
351    time_t     ctime;           /* the ctime of this entry */
352    time_t     atime;           /* the atime of this entry */
353    time_t     dtime;           /* the deletion time of this entry */
354    long       ttl;             /* ttl of time entry, var only */
355
356#ifdef IS_UNICODE
357    zend_uchar name_type;
358#endif
359    xc_entry_name_t name;
360
361    union {
362        xc_entry_data_php_t *php;
363        xc_entry_data_var_t *var;
364    } data;
365
366    time_t mtime;           /* the mtime of origin source file */
367#ifdef HAVE_INODE
368    int device;             /* the filesystem device */
369    int inode;              /* the filesystem inode */
370#endif
371};
372/* }}} */
373
374extern zend_module_entry xcache_module_entry;
375#define phpext_xcache_ptr &xcache_module_entry
376
377int xc_is_rw(const void *p);
378int xc_is_ro(const void *p);
379int xc_is_shm(const void *p);
380void xc_gc_add_op_array(zend_op_array *op_array TSRMLS_DC);
381
382#endif /* __XCACHE_H */
Note: See TracBrowser for help on using the repository browser.