| [1] | 1 | #ifndef __XCACHE_H |
|---|
| 2 | #define __XCACHE_H |
|---|
| 3 | #define XCACHE_NAME "XCache" |
|---|
| [816] | 4 | #define XCACHE_VERSION "1.3.3-dev" |
|---|
| [1] | 5 | #define XCACHE_AUTHOR "mOo" |
|---|
| [738] | 6 | #define XCACHE_COPYRIGHT "Copyright (c) 2005-2011" |
|---|
| [1] | 7 | #define XCACHE_URL "http://xcache.lighttpd.net" |
|---|
| [625] | 8 | #define XCACHE_WIKI_URL XCACHE_URL "/wiki" |
|---|
| [1] | 9 | |
|---|
| 10 | #include <php.h> |
|---|
| 11 | #include <zend_compile.h> |
|---|
| 12 | #include <zend_API.h> |
|---|
| [617] | 13 | #include <zend.h> |
|---|
| [1] | 14 | #include "php_ini.h" |
|---|
| 15 | #include "zend_hash.h" |
|---|
| 16 | |
|---|
| 17 | #ifdef HAVE_CONFIG_H |
|---|
| 18 | #include <config.h> |
|---|
| 19 | #endif |
|---|
| [148] | 20 | #include "xc_shm.h" |
|---|
| [1] | 21 | #include "lock.h" |
|---|
| 22 | |
|---|
| [165] | 23 | #define HAVE_INODE |
|---|
| [726] | 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 |
|---|
| [1] | 26 | #endif |
|---|
| [726] | 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)) |
|---|
| [78] | 31 | # define ZEND_ENGINE_2_2 |
|---|
| 32 | #endif |
|---|
| [726] | 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 |
|---|
| [481] | 35 | #endif |
|---|
| [1] | 36 | |
|---|
| [19] | 37 | #define NOTHING |
|---|
| [1] | 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 | |
|---|
| [625] | 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) |
|---|
| [617] | 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 |
|---|
| [813] | 56 | #ifndef Z_ISREF |
|---|
| 57 | # define Z_ISREF(z) (z).is_ref |
|---|
| 58 | #endif |
|---|
| [617] | 59 | #ifndef Z_SET_ISREF |
|---|
| [626] | 60 | # define Z_SET_ISREF(z) (z).is_ref = 1 |
|---|
| [617] | 61 | #endif |
|---|
| 62 | #ifndef Z_UNSET_ISREF |
|---|
| [626] | 63 | # define Z_UNSET_ISREF(z) (z).is_ref = 0 |
|---|
| [617] | 64 | #endif |
|---|
| [813] | 65 | #ifndef Z_REFCOUNT |
|---|
| 66 | # define Z_REFCOUNT(z) (z).refcount |
|---|
| 67 | #endif |
|---|
| [617] | 68 | #ifndef Z_SET_REFCOUNT |
|---|
| [626] | 69 | # define Z_SET_REFCOUNT(z, rc) (z).refcount = rc |
|---|
| [617] | 70 | #endif |
|---|
| [618] | 71 | #ifndef IS_CONSTANT_TYPE_MASK |
|---|
| [650] | 72 | # define IS_CONSTANT_TYPE_MASK (~IS_CONSTANT_INDEX) |
|---|
| [618] | 73 | #endif |
|---|
| [617] | 74 | |
|---|
| [263] | 75 | /* {{{ dirty fix for PHP 6 */ |
|---|
| 76 | #ifdef add_assoc_long_ex |
|---|
| 77 | static 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 |
|---|
| 85 | static 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 |
|---|
| 93 | static 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 |
|---|
| [726] | 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 |
|---|
| [772] | 105 | # define Z_OP_TYPEOF_TYPE zend_uchar |
|---|
| [726] | 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 |
|---|
| [772] | 112 | # define Z_OP_TYPEOF_TYPE int |
|---|
| [726] | 113 | typedef znode znode_op; |
|---|
| 114 | |
|---|
| 115 | # define Z_CLASS_INFO(className) (className) |
|---|
| 116 | #endif |
|---|
| 117 | |
|---|
| [263] | 118 | /* }}} */ |
|---|
| 119 | |
|---|
| [1] | 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 | )) |
|---|
| [200] | 133 | #define BUCKET_KEY_S(b) (UNISW((b)->arKey, (b)->key.arKey.s)) |
|---|
| 134 | #define BUCKET_KEY_U(b) (UNISW((b)->arKey, (b)->key.arKey.u)) |
|---|
| 135 | #define BUCKET_KEY_TYPE(b) (UNISW(IS_STRING, (b)->key.type)) |
|---|
| [2] | 136 | #ifdef IS_UNICODE |
|---|
| [104] | 137 | # define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey) |
|---|
| [2] | 138 | #else |
|---|
| [11] | 139 | # define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey) |
|---|
| [2] | 140 | #endif |
|---|
| [1] | 141 | #define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b)) |
|---|
| 142 | |
|---|
| 143 | #ifndef IS_UNICODE |
|---|
| 144 | typedef char *zstr; |
|---|
| [103] | 145 | # define ZSTR_S(s) (s) |
|---|
| 146 | # define ZSTR_U(s) (s) |
|---|
| 147 | # define ZSTR_V(s) (s) |
|---|
| 148 | # define ZSTR_PS(s) (s) |
|---|
| 149 | # define ZSTR_PU(s) (s) |
|---|
| 150 | # define ZSTR_PV(s) (s) |
|---|
| [1] | 151 | #else |
|---|
| [103] | 152 | # define ZSTR_S(zs) ((zs).s) |
|---|
| 153 | # define ZSTR_U(zs) ((zs).u) |
|---|
| 154 | # define ZSTR_V(zs) ((zs).v) |
|---|
| 155 | # define ZSTR_PS(pzs) ((pzs)->s) |
|---|
| 156 | # define ZSTR_PU(pzs) ((pzs)->u) |
|---|
| 157 | # define ZSTR_PV(pzs) ((pzs)->v) |
|---|
| [1] | 158 | #endif |
|---|
| 159 | |
|---|
| [103] | 160 | #ifndef ZSTR |
|---|
| 161 | # define ZSTR(s) (s) |
|---|
| 162 | #endif |
|---|
| 163 | |
|---|
| 164 | #ifndef Z_UNIVAL |
|---|
| 165 | # define Z_UNIVAL(zval) (zval).value.str.val |
|---|
| 166 | # define Z_UNILEN(zval) (zval).value.str.len |
|---|
| 167 | #endif |
|---|
| 168 | |
|---|
| [1] | 169 | /* {{{ u hash wrapper */ |
|---|
| 170 | #ifndef IS_UNICODE |
|---|
| 171 | # define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \ |
|---|
| 172 | zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest) |
|---|
| 173 | |
|---|
| [622] | 174 | # define zend_u_hash_quick_add(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \ |
|---|
| 175 | zend_hash_quick_add(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) |
|---|
| 176 | |
|---|
| [88] | 177 | # define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \ |
|---|
| 178 | zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest) |
|---|
| 179 | |
|---|
| [622] | 180 | # define zend_u_hash_quick_update(ht, type, arKey, nKeyLength, h, pData, nDataSize, pDest) \ |
|---|
| 181 | zend_hash_quick_update(ht, arKey, nKeyLength, h, pData, nDataSize, pDest) |
|---|
| 182 | |
|---|
| [1] | 183 | # define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \ |
|---|
| 184 | zend_hash_find(ht, arKey, nKeyLength, pData) |
|---|
| 185 | |
|---|
| [200] | 186 | # define zend_u_hash_quick_find(ht, type, arKey, nKeyLength, h, pData) \ |
|---|
| 187 | zend_hash_quick_find(ht, arKey, nKeyLength, h, pData) |
|---|
| 188 | |
|---|
| [1] | 189 | # define add_u_assoc_zval_ex(arg, type, key, key_len, value) \ |
|---|
| 190 | add_assoc_zval_ex(arg, key, key_len, value) |
|---|
| 191 | |
|---|
| [268] | 192 | # define zend_u_is_auto_global(type, name, name_len) \ |
|---|
| 193 | zend_is_auto_global(name, name_len) |
|---|
| [1] | 194 | #endif |
|---|
| 195 | /* }}} */ |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | #define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0]))) |
|---|
| 199 | #define ECALLOC_ONE(x) ECALLOC_N(x, 1) |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | typedef ulong xc_hash_value_t; |
|---|
| 204 | typedef struct { |
|---|
| 205 | int bits; |
|---|
| 206 | int size; |
|---|
| 207 | int mask; |
|---|
| 208 | } xc_hash_t; |
|---|
| 209 | |
|---|
| 210 | /* the class entry type to be stored in class_table */ |
|---|
| 211 | typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t; |
|---|
| 212 | |
|---|
| 213 | /* xc_cest_t to (zend_class_entry*) */ |
|---|
| 214 | #define CestToCePtr(st) (ZESW(\ |
|---|
| 215 | &(st), \ |
|---|
| 216 | st \ |
|---|
| 217 | ) ) |
|---|
| 218 | |
|---|
| 219 | /* ZCEP=zend class entry ptr */ |
|---|
| 220 | #define ZCEP_REFCOUNT_PTR(pce) (ZESW( \ |
|---|
| 221 | (pce)->refcount, \ |
|---|
| 222 | &((pce)->refcount) \ |
|---|
| 223 | )) |
|---|
| 224 | |
|---|
| 225 | #define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce) |
|---|
| 226 | |
|---|
| 227 | typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC); |
|---|
| 228 | |
|---|
| 229 | /* {{{ xc_cache_t */ |
|---|
| 230 | typedef struct _xc_entry_t xc_entry_t; |
|---|
| [11] | 231 | typedef struct { |
|---|
| [1] | 232 | int cacheid; |
|---|
| 233 | xc_hash_t *hcache; /* hash to cacheid */ |
|---|
| 234 | |
|---|
| [11] | 235 | time_t compiling; |
|---|
| [1] | 236 | zend_ulong misses; |
|---|
| 237 | zend_ulong hits; |
|---|
| 238 | zend_ulong clogs; |
|---|
| 239 | zend_ulong ooms; |
|---|
| [457] | 240 | zend_ulong errors; |
|---|
| [1] | 241 | xc_lock_t *lck; |
|---|
| 242 | xc_shm_t *shm; /* to which shm contains us */ |
|---|
| 243 | xc_mem_t *mem; /* to which mem contains us */ |
|---|
| 244 | |
|---|
| 245 | xc_entry_t **entries; |
|---|
| [32] | 246 | int entries_count; |
|---|
| [1] | 247 | xc_entry_t *deletes; |
|---|
| [32] | 248 | int deletes_count; |
|---|
| [1] | 249 | xc_hash_t *hentry; /* hash to entry */ |
|---|
| [114] | 250 | |
|---|
| 251 | time_t last_gc_deletes; |
|---|
| 252 | time_t last_gc_expires; |
|---|
| [623] | 253 | |
|---|
| 254 | time_t hits_by_hour_cur_time; |
|---|
| 255 | zend_uint hits_by_hour_cur_slot; |
|---|
| 256 | zend_ulong hits_by_hour[24]; |
|---|
| 257 | time_t hits_by_second_cur_time; |
|---|
| 258 | zend_uint hits_by_second_cur_slot; |
|---|
| 259 | zend_ulong hits_by_second[5]; |
|---|
| [1] | 260 | } xc_cache_t; |
|---|
| 261 | /* }}} */ |
|---|
| 262 | /* {{{ xc_classinfo_t */ |
|---|
| 263 | typedef struct { |
|---|
| [14] | 264 | #ifdef IS_UNICODE |
|---|
| 265 | zend_uchar type; |
|---|
| 266 | #endif |
|---|
| [103] | 267 | zstr key; |
|---|
| [1] | 268 | zend_uint key_size; |
|---|
| [622] | 269 | ulong h; |
|---|
| [1] | 270 | xc_cest_t cest; |
|---|
| [625] | 271 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| [212] | 272 | int oplineno; |
|---|
| [625] | 273 | #endif |
|---|
| [1] | 274 | } xc_classinfo_t; |
|---|
| 275 | /* }}} */ |
|---|
| [95] | 276 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 277 | /* {{{ xc_constinfo_t */ |
|---|
| 278 | typedef struct { |
|---|
| 279 | #ifdef IS_UNICODE |
|---|
| 280 | zend_uchar type; |
|---|
| 281 | #endif |
|---|
| [103] | 282 | zstr key; |
|---|
| [95] | 283 | zend_uint key_size; |
|---|
| [622] | 284 | ulong h; |
|---|
| [95] | 285 | zend_constant constant; |
|---|
| 286 | } xc_constinfo_t; |
|---|
| 287 | /* }}} */ |
|---|
| 288 | #endif |
|---|
| [1] | 289 | /* {{{ xc_funcinfo_t */ |
|---|
| 290 | typedef struct { |
|---|
| [14] | 291 | #ifdef IS_UNICODE |
|---|
| 292 | zend_uchar type; |
|---|
| 293 | #endif |
|---|
| [103] | 294 | zstr key; |
|---|
| [1] | 295 | zend_uint key_size; |
|---|
| [622] | 296 | ulong h; |
|---|
| [1] | 297 | zend_function func; |
|---|
| 298 | } xc_funcinfo_t; |
|---|
| 299 | /* }}} */ |
|---|
| [268] | 300 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 301 | /* {{{ xc_autoglobal_t */ |
|---|
| 302 | typedef struct { |
|---|
| 303 | #ifdef IS_UNICODE |
|---|
| 304 | zend_uchar type; |
|---|
| 305 | #endif |
|---|
| 306 | zstr key; |
|---|
| 307 | zend_uint key_len; |
|---|
| [622] | 308 | ulong h; |
|---|
| [268] | 309 | } xc_autoglobal_t; |
|---|
| 310 | /* }}} */ |
|---|
| 311 | #endif |
|---|
| [1] | 312 | typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t; |
|---|
| 313 | /* {{{ xc_entry_data_php_t */ |
|---|
| 314 | typedef struct { |
|---|
| 315 | size_t sourcesize; |
|---|
| 316 | #ifdef HAVE_INODE |
|---|
| 317 | int device; /* the filesystem device */ |
|---|
| 318 | int inode; /* the filesystem inode */ |
|---|
| 319 | #endif |
|---|
| 320 | time_t mtime; /* the mtime of origin source file */ |
|---|
| 321 | |
|---|
| 322 | zend_op_array *op_array; |
|---|
| 323 | |
|---|
| [95] | 324 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 325 | zend_uint constinfo_cnt; |
|---|
| 326 | xc_constinfo_t *constinfos; |
|---|
| 327 | #endif |
|---|
| 328 | |
|---|
| [1] | 329 | zend_uint funcinfo_cnt; |
|---|
| 330 | xc_funcinfo_t *funcinfos; |
|---|
| 331 | |
|---|
| 332 | zend_uint classinfo_cnt; |
|---|
| 333 | xc_classinfo_t *classinfos; |
|---|
| [625] | 334 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
|---|
| [212] | 335 | zend_bool have_early_binding; |
|---|
| [625] | 336 | #endif |
|---|
| [268] | 337 | |
|---|
| 338 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 339 | zend_uint autoglobal_cnt; |
|---|
| 340 | xc_autoglobal_t *autoglobals; |
|---|
| 341 | #endif |
|---|
| [1] | 342 | } xc_entry_data_php_t; |
|---|
| 343 | /* }}} */ |
|---|
| 344 | /* {{{ xc_entry_data_var_t */ |
|---|
| 345 | typedef struct { |
|---|
| 346 | zval *value; |
|---|
| 347 | } xc_entry_data_var_t; |
|---|
| 348 | /* }}} */ |
|---|
| 349 | typedef zvalue_value xc_entry_name_t; |
|---|
| 350 | /* {{{ xc_entry_t */ |
|---|
| 351 | struct _xc_entry_t { |
|---|
| 352 | xc_entry_type_t type; |
|---|
| 353 | xc_hash_value_t hvalue; |
|---|
| 354 | xc_entry_t *next; |
|---|
| 355 | xc_cache_t *cache; /* which cache it's on */ |
|---|
| 356 | |
|---|
| 357 | size_t size; |
|---|
| 358 | zend_ulong refcount; |
|---|
| 359 | zend_ulong hits; |
|---|
| 360 | time_t ctime; /* the ctime of this entry */ |
|---|
| 361 | time_t atime; /* the atime of this entry */ |
|---|
| 362 | time_t dtime; /* the deletion time of this entry */ |
|---|
| [114] | 363 | long ttl; /* ttl of time entry, var only */ |
|---|
| [1] | 364 | |
|---|
| 365 | #ifdef IS_UNICODE |
|---|
| 366 | zend_uchar name_type; |
|---|
| 367 | #endif |
|---|
| 368 | xc_entry_name_t name; |
|---|
| 369 | |
|---|
| 370 | union { |
|---|
| 371 | xc_entry_data_php_t *php; |
|---|
| 372 | xc_entry_data_var_t *var; |
|---|
| 373 | } data; |
|---|
| [233] | 374 | |
|---|
| 375 | zend_bool have_references; |
|---|
| [1] | 376 | }; |
|---|
| 377 | /* }}} */ |
|---|
| 378 | |
|---|
| 379 | extern zend_module_entry xcache_module_entry; |
|---|
| 380 | #define phpext_xcache_ptr &xcache_module_entry |
|---|
| 381 | |
|---|
| 382 | int xc_is_rw(const void *p); |
|---|
| 383 | int xc_is_ro(const void *p); |
|---|
| 384 | int xc_is_shm(const void *p); |
|---|
| [619] | 385 | void xc_gc_add_op_array(zend_op_array *op_array TSRMLS_DC); |
|---|
| [1] | 386 | |
|---|
| 387 | #endif /* __XCACHE_H */ |
|---|