XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
6.0 KiB

  1. #ifndef __XCACHE_H
  2. #define __XCACHE_H
  3. #define XCACHE_NAME "XCache"
  4. #define XCACHE_VERSION "1.1.0"
  5. #define XCACHE_AUTHOR "mOo"
  6. #define XCACHE_COPYRIGHT "Copyright (c) 2005-2006"
  7. #define XCACHE_URL "http://xcache.lighttpd.net"
  8. #include <php.h>
  9. #include <zend_compile.h>
  10. #include <zend_API.h>
  11. #include "php_ini.h"
  12. #include "zend_hash.h"
  13. #ifdef HAVE_CONFIG_H
  14. #include <config.h>
  15. #endif
  16. #include "xc_shm.h"
  17. #include "lock.h"
  18. #ifndef ZEND_WIN32
  19. /* UnDefine if your filesystem doesn't support inodes */
  20. # define HAVE_INODE
  21. #endif
  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. #define NOTHING
  29. /* ZendEngine code Switcher */
  30. #ifndef ZEND_ENGINE_2
  31. # define ZESW(v1, v2) v1
  32. #else
  33. # define ZESW(v1, v2) v2
  34. #endif
  35. /* unicode */
  36. #ifdef IS_UNICODE
  37. # define UNISW(text, unicode) unicode
  38. #else
  39. # define UNISW(text, unicode) text
  40. #endif
  41. #define BUCKET_KEY_SIZE(b) \
  42. (UNISW( \
  43. (b)->nKeyLength, \
  44. ((b)->key.type == IS_UNICODE) \
  45. ? UBYTES(b->nKeyLength) \
  46. : b->nKeyLength \
  47. ))
  48. #define BUCKET_KEY(b) (UNISW((b)->arKey, (b)->key.arKey.s))
  49. #define BUCKET_UKEY(b) (UNISW((b)->arKey, (b)->key.arKey.u))
  50. #define BUCKET_KEY_TYPE(b) (UNISW(0, (b)->key.type))
  51. #ifdef IS_UNICODE
  52. # define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, key.arKey)
  53. #else
  54. # define BUCKET_HEAD_SIZE(b) XtOffsetOf(Bucket, arKey)
  55. #endif
  56. #define BUCKET_SIZE(b) (BUCKET_HEAD_SIZE(b) + BUCKET_KEY_SIZE(b))
  57. #ifndef IS_UNICODE
  58. typedef char *zstr;
  59. # define ZSTR_S(s) (s)
  60. # define ZSTR_U(s) (s)
  61. # define ZSTR_V(s) (s)
  62. # define ZSTR_PS(s) (s)
  63. # define ZSTR_PU(s) (s)
  64. # define ZSTR_PV(s) (s)
  65. #else
  66. # define ZSTR_S(zs) ((zs).s)
  67. # define ZSTR_U(zs) ((zs).u)
  68. # define ZSTR_V(zs) ((zs).v)
  69. # define ZSTR_PS(pzs) ((pzs)->s)
  70. # define ZSTR_PU(pzs) ((pzs)->u)
  71. # define ZSTR_PV(pzs) ((pzs)->v)
  72. #endif
  73. #ifndef ZSTR
  74. # define ZSTR(s) (s)
  75. #endif
  76. #ifndef Z_UNIVAL
  77. # define Z_UNIVAL(zval) (zval).value.str.val
  78. # define Z_UNILEN(zval) (zval).value.str.len
  79. #endif
  80. /* {{{ u hash wrapper */
  81. #ifndef IS_UNICODE
  82. # define zend_u_hash_add(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
  83. zend_hash_add(ht, arKey, nKeyLength, pData, nDataSize, pDest)
  84. # define zend_u_hash_update(ht, type, arKey, nKeyLength, pData, nDataSize, pDest) \
  85. zend_hash_update(ht, arKey, nKeyLength, pData, nDataSize, pDest)
  86. # define zend_u_hash_find(ht, type, arKey, nKeyLength, pData) \
  87. zend_hash_find(ht, arKey, nKeyLength, pData)
  88. # define add_u_assoc_zval_ex(arg, type, key, key_len, value) \
  89. add_assoc_zval_ex(arg, key, key_len, value)
  90. #endif
  91. /* }}} */
  92. #define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
  93. #define ECALLOC_ONE(x) ECALLOC_N(x, 1)
  94. typedef ulong xc_hash_value_t;
  95. typedef struct {
  96. int bits;
  97. int size;
  98. int mask;
  99. } xc_hash_t;
  100. /* the class entry type to be stored in class_table */
  101. typedef ZESW(zend_class_entry, zend_class_entry*) xc_cest_t;
  102. /* xc_cest_t to (zend_class_entry*) */
  103. #define CestToCePtr(st) (ZESW(\
  104. &(st), \
  105. st \
  106. ) )
  107. /* ZCEP=zend class entry ptr */
  108. #define ZCEP_REFCOUNT_PTR(pce) (ZESW( \
  109. (pce)->refcount, \
  110. &((pce)->refcount) \
  111. ))
  112. #define ZCE_REFCOUNT_PTR(ce) ZCE_REFCOUNT_PTR(&ce)
  113. typedef zend_op_array *(zend_compile_file_t)(zend_file_handle *h, int type TSRMLS_DC);
  114. /* {{{ xc_cache_t */
  115. typedef struct _xc_entry_t xc_entry_t;
  116. typedef struct {
  117. int cacheid;
  118. xc_hash_t *hcache; /* hash to cacheid */
  119. time_t compiling;
  120. zend_ulong misses;
  121. zend_ulong hits;
  122. zend_ulong clogs;
  123. zend_ulong ooms;
  124. xc_lock_t *lck;
  125. xc_shm_t *shm; /* to which shm contains us */
  126. xc_mem_t *mem; /* to which mem contains us */
  127. xc_entry_t **entries;
  128. int entries_count;
  129. xc_entry_t *deletes;
  130. int deletes_count;
  131. xc_hash_t *hentry; /* hash to entry */
  132. time_t last_gc_deletes;
  133. time_t last_gc_expires;
  134. } xc_cache_t;
  135. /* }}} */
  136. /* {{{ xc_classinfo_t */
  137. typedef struct {
  138. #ifdef IS_UNICODE
  139. zend_uchar type;
  140. #endif
  141. zstr key;
  142. zend_uint key_size;
  143. xc_cest_t cest;
  144. } xc_classinfo_t;
  145. /* }}} */
  146. #ifdef HAVE_XCACHE_CONSTANT
  147. /* {{{ xc_constinfo_t */
  148. typedef struct {
  149. #ifdef IS_UNICODE
  150. zend_uchar type;
  151. #endif
  152. zstr key;
  153. zend_uint key_size;
  154. zend_constant constant;
  155. } xc_constinfo_t;
  156. /* }}} */
  157. #endif
  158. /* {{{ xc_funcinfo_t */
  159. typedef struct {
  160. #ifdef IS_UNICODE
  161. zend_uchar type;
  162. #endif
  163. zstr key;
  164. zend_uint key_size;
  165. zend_function func;
  166. } xc_funcinfo_t;
  167. /* }}} */
  168. typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
  169. /* {{{ xc_entry_data_php_t */
  170. typedef struct {
  171. size_t sourcesize;
  172. #ifdef HAVE_INODE
  173. int device; /* the filesystem device */
  174. int inode; /* the filesystem inode */
  175. #endif
  176. time_t mtime; /* the mtime of origin source file */
  177. zend_op_array *op_array;
  178. #ifdef HAVE_XCACHE_CONSTANT
  179. zend_uint constinfo_cnt;
  180. xc_constinfo_t *constinfos;
  181. #endif
  182. zend_uint funcinfo_cnt;
  183. xc_funcinfo_t *funcinfos;
  184. zend_uint classinfo_cnt;
  185. xc_classinfo_t *classinfos;
  186. } xc_entry_data_php_t;
  187. /* }}} */
  188. /* {{{ xc_entry_data_var_t */
  189. typedef struct {
  190. zval *value;
  191. } xc_entry_data_var_t;
  192. /* }}} */
  193. typedef zvalue_value xc_entry_name_t;
  194. /* {{{ xc_entry_t */
  195. struct _xc_entry_t {
  196. xc_entry_type_t type;
  197. xc_hash_value_t hvalue;
  198. xc_entry_t *next;
  199. xc_cache_t *cache; /* which cache it's on */
  200. size_t size;
  201. zend_ulong refcount;
  202. zend_ulong hits;
  203. time_t ctime; /* the ctime of this entry */
  204. time_t atime; /* the atime of this entry */
  205. time_t dtime; /* the deletion time of this entry */
  206. long ttl; /* ttl of time entry, var only */
  207. #ifdef IS_UNICODE
  208. zend_uchar name_type;
  209. #endif
  210. xc_entry_name_t name;
  211. union {
  212. xc_entry_data_php_t *php;
  213. xc_entry_data_var_t *var;
  214. } data;
  215. };
  216. /* }}} */
  217. extern zend_module_entry xcache_module_entry;
  218. #define phpext_xcache_ptr &xcache_module_entry
  219. int xc_is_rw(const void *p);
  220. int xc_is_ro(const void *p);
  221. int xc_is_shm(const void *p);
  222. #endif /* __XCACHE_H */