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