source: svn/trunk/xcache.h @ 311

Last change on this file since 311 was 311, checked in by Xuefer, 10 years ago

restruct cached compile, add md5 table to recognize and merge file with same content

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