source: trunk/xcache.h @ 854

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

remove type/cache/hvalue from cached copy to reduce memory usage a little bit

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