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.

322 lines
9.4 KiB

  1. divert(-1)
  2. dnl ================ start ======================
  3. dnl define(`XCACHE_ENABLE_TEST')
  4. dnl define(`DEBUG_SIZE')
  5. define(`USEMEMCPY')
  6. dnl ================ main
  7. dnl {{{ basic
  8. define(`REDEF', `ifdef(`$1', `undefine(`$1')') define(`$1', `$2')')
  9. define(`MAKE_MACRONAME', `translit(`$1', ` ():
  10. ', `_____')')
  11. define(`ONCE', `ifdef(MAKE_MACRONAME(`ONCE $1'), `', `define(MAKE_MACRONAME(`ONCE $1'))$1')')
  12. define(`m4_errprint', `ONCE(`errprint(`$1
  13. ')')')
  14. ifdef(`len', `
  15. define(`m4_len', defn(`len'))
  16. undefine(`len')
  17. ')
  18. define(`dirof', `patsubst(`$1', `[/\\][^/\\]*$', `')')
  19. ifdef(`__dir__', `', `
  20. define(`__dir__', `dirof(__file__)')
  21. ')
  22. define(`XCACHE_STRS', `($1), (sizeof($1))')
  23. define(`XCACHE_STRL', `($1), (sizeof($1) - 1)')
  24. define(`SRC', `ifelse(`$1', `', `src', `src->$1')')
  25. define(`DST', `ifelse(`$1', `', `dst', `dst->$1')')
  26. dnl ============
  27. define(`INDENT', `xc_dprint_indent(indent);')
  28. dnl }}}
  29. dnl {{{ PTR_FROM_VIRTUAL_EX(1:type, 2:elm)
  30. define(`PTR_FROM_VIRTUAL_EX', `$2')
  31. dnl }}}
  32. dnl {{{ ALLOC(1:dst, 2:type, 3:count=1, 4:clean=false, 5:realtype=$2)
  33. define(`ALLOC', `
  34. pushdef(`COUNT', `ifelse(`$3', `', `1', `$3')')
  35. ifdef(`ALLOC_SIZE_HELPER', `
  36. pushdef(`SIZE', `ALLOC_SIZE_HELPER()')
  37. ', `
  38. pushdef(`SIZE', `sizeof($2)ifelse(`$3', `', `', ` * $3')')
  39. ')
  40. pushdef(`REALTYPE', `ifelse(`$5', , `$2', `$5')')
  41. /* allocate */
  42. IFCALC(`
  43. IFAUTOCHECK(`
  44. {
  45. unsigned long allocsize = SIZE, allocline = __LINE__;
  46. xc_vector_push_back(&processor->allocsizes, &allocsize);
  47. xc_vector_push_back(&processor->allocsizes, &allocline);
  48. }
  49. ')
  50. processor->size = (size_t) ALIGN(processor->size);
  51. processor->size += SIZE;
  52. ')
  53. IFSTORE(`
  54. IFAUTOCHECK(`{
  55. if (!xc_vector_size(&processor->allocsizes)) {
  56. fprintf(stderr, "mismatch `$@' at line %d\n", __LINE__);
  57. }
  58. else {
  59. unsigned long expect = xc_vector_pop_back(unsigned long, &processor->allocsizes);
  60. unsigned long atline = xc_vector_pop_back(unsigned long, &processor->allocsizes);
  61. unsigned long real = SIZE;
  62. if (expect != real) {
  63. fprintf(stderr, "mismatch `$@' at line %d(was %lu): real %lu - expect %lu = %lu\n", __LINE__, atline, real, expect, real - expect);
  64. }
  65. }
  66. }')
  67. ifdef(`DEBUG_SIZE', ` {
  68. void *oldp = processor->p;
  69. ')
  70. $1 = (REALTYPE *) (processor->p = (char *) ALIGN(processor->p));
  71. ifelse(`$4', `', `
  72. IFAUTOCHECK(`memsetptr($1, (void *) (unsigned long) __LINE__, SIZE);')
  73. ', `
  74. memset($1, 0, SIZE);
  75. ')
  76. processor->p += SIZE;
  77. ifdef(`DEBUG_SIZE', `
  78. xc_totalsize += (char *) processor->p - (char *) oldp;
  79. fprintf(stderr, "%d\t%d\t`'SIZE()\n", (char *) processor->p - (char *) oldp, xc_totalsize);
  80. }
  81. ')
  82. ')
  83. IFRESTORE(`ifelse(`$4', `', `
  84. ifelse(
  85. REALTYPE*COUNT, `zval*1', `ALLOC_ZVAL($1);',
  86. REALTYPE*COUNT, `HashTable*1', `ALLOC_HASHTABLE($1);',
  87. `', `', `$1 = (REALTYPE *) emalloc(SIZE);')
  88. IFAUTOCHECK(`memsetptr($1, (void *) __LINE__, SIZE);')
  89. ', `
  90. $1 = (REALTYPE *) ecalloc(COUNT, sizeof($2));
  91. ')
  92. ')
  93. popdef(`REALTYPE')
  94. popdef(`COUNT')
  95. popdef(`SIZE')
  96. ')
  97. dnl CALLOC(1:dst, 2:type [, 3:count=1, 4:realtype=$2 ])
  98. define(`CALLOC', `ALLOC(`$1', `$2', `$3', `1', `$4')')
  99. dnl }}}
  100. dnl {{{ PROC_CLASS_ENTRY_P(1:elm)
  101. define(`PROC_CLASS_ENTRY_P', `PROC_CLASS_ENTRY_P_EX(`DST(`$1')', `SRC(`$1')', `$1')`'DONE(`$1')')
  102. dnl PROC_CLASS_ENTRY_P_EX(1:dst, 2:src, 3:elm-name)
  103. define(`PROC_CLASS_ENTRY_P_EX', `
  104. if ($2) {
  105. IFSTORE(`$1 = (zend_class_entry *) xc_get_class_num(processor, $2);')
  106. IFRESTORE(`$1 = xc_get_class(processor, (zend_ulong) $2);')
  107. #ifdef IS_UNICODE
  108. IFDASM(`add_assoc_unicodel_ex(DST(), XCACHE_STRS("$3"), ZSTR_U($2->name), $2->name_length, 1);')
  109. #else
  110. IFDASM(`add_assoc_stringl_ex(DST(), XCACHE_STRS("$3"), (char *) $2->name, $2->name_length, 1);')
  111. #endif
  112. }
  113. else {
  114. COPYNULL_EX(`$1', `$3')
  115. }
  116. ')
  117. dnl }}}
  118. dnl {{{ IFAUTOCHECKEX
  119. define(`IFAUTOCHECKEX', `ifdef(`XCACHE_ENABLE_TEST', `$1', `$2')')
  120. dnl }}}
  121. dnl {{{ IFAUTOCHECK
  122. define(`IFAUTOCHECK', `IFAUTOCHECKEX(`
  123. #ifndef NDEBUG
  124. $1
  125. #endif
  126. ')')
  127. dnl }}}
  128. dnl {{{ DBG
  129. define(`DBG', `ifdef(`XCACHE_ENABLE_TEST', `
  130. /* `$1' */
  131. ')')
  132. dnl }}}
  133. dnl {{{ EXPORT(1:code)
  134. define(`EXPORT', `/* export: $1 :export */')
  135. define(`EXPORTED', `EXPORT(`$1')
  136. $1')
  137. define(`EXPORTED_FUNCTION', `EXPORT(`$1;')
  138. $1')
  139. dnl }}}
  140. dnl {{{ EXPORT_PROCESSOR(1:type, 2:processor)
  141. define(`EXPORT_PROCESSOR', `define(`EXPORT_$1_$2', 1)')
  142. dnl }}}
  143. dnl {{{ RELOCATE(1:type, 2:ele)
  144. define(`RELOCATE', `RELOCATE_EX(`$1', `DST(`$2')')')
  145. dnl }}}
  146. dnl {{{ RELOCATE_EX(1:type, 2:dst)
  147. define(`RELOCATE_EX', `')
  148. dnl }}}
  149. dnl {{{ IFNOTMEMCPY
  150. define(`IFNOTMEMCPY', `ifdef(`USEMEMCPY', `', `$1')')
  151. dnl }}}
  152. dnl {{{ COPY
  153. define(`COPY', `IFNOTMEMCPY(`IFCOPY(`DST(`$1') = SRC(`$1');')')DONE(`$1')')
  154. dnl }}}
  155. dnl {{{ COPY_N_EX(1:count, 2:type, 3:dst)
  156. define(`COPY_N_EX', `
  157. ALLOC(`DST(`$3')', `$2', `SRC(`$1')')
  158. IFCOPY(`
  159. memcpy(DST(`$3'), SRC(`$3'), sizeof(DST(`$3[0]')) * SRC(`$1'));
  160. ')
  161. ')
  162. dnl }}}
  163. dnl {{{ COPYPOINTER
  164. define(`COPYPOINTER', `COPY(`$1')')
  165. dnl }}}
  166. dnl {{{ SETNULL_EX
  167. define(`SETNULL_EX', `
  168. IFDASM(`
  169. ifelse(`$2', `[]', `
  170. add_next_index_null(DST());
  171. ', `
  172. add_assoc_null_ex(DST(), XCACHE_STRS("ifelse(`$2', `', `$1', `$2')"));
  173. ')
  174. ')
  175. IFCOPY(`$1 = NULL;')
  176. ')
  177. define(`SETNULL', `SETNULL_EX(`DST(`$1')')DONE(`$1')')
  178. dnl }}}
  179. dnl {{{ COPYNULL_EX(1:dst, 2:elm-name)
  180. define(`COPYNULL_EX', `
  181. IFDASM(`
  182. ifelse(`$2', `[]', `
  183. add_next_index_null(DST());
  184. ', `
  185. add_assoc_null_ex(DST(), XCACHE_STRS("ifelse(`$2', `', `$1', `$2')"));
  186. ')
  187. ')
  188. IFNOTMEMCPY(`IFCOPY(`$1 = NULL;')')
  189. assert(patsubst($1, DST(), SRC()) == NULL);
  190. ')
  191. dnl }}}
  192. dnl {{{ COPYNULL(1:elm)
  193. define(`COPYNULL', `
  194. COPYNULL_EX(`DST(`$1')', `$1')DONE(`$1')
  195. ')
  196. dnl }}}
  197. dnl {{{ COPYZERO_EX(1:dst, 2:elm-name)
  198. define(`COPYZERO_EX', `
  199. IFDASM(`add_assoc_long_ex(DST(), XCACHE_STRS("$2"), 0);')
  200. IFNOTMEMCPY(`IFCOPY(`$1 = 0;')')
  201. assert(patsubst($1, DST(), SRC()) == 0);
  202. ')
  203. dnl }}}
  204. dnl {{{ COPYZERO(1:elm)
  205. define(`COPYZERO', `
  206. COPYZERO_EX(`DST(`$1')', `$1')DONE(`$1')
  207. ')
  208. dnl }}}
  209. dnl {{{ LIST_DIFF(1:left-list, 2:right-list)
  210. define(`foreach',
  211. `pushdef(`$1')_foreach(`$1', `$2', `$3')popdef(`$1')')
  212. define(`_arg1', `$1')
  213. define(`_foreach',
  214. `ifelse(`$2', `()', ,
  215. `define(`$1', _arg1$2)$3`'_foreach(`$1',
  216. (shift$2),
  217. `$3')')')
  218. define(`LIST_DIFF', `dnl
  219. foreach(`i', `($1)', `pushdef(`item_'defn(`i'))')dnl allocate variable for items in $1
  220. foreach(`i', `($2)', `pushdef(`item_'defn(`i'))undefine(`item_'defn(`i'))')dnl allocate variable for items in $2, and undefine it
  221. foreach(`i', `($1)', `ifdef(`item_'defn(`i'), `defn(`i') ')')dnl see what is still defined
  222. foreach(`i', `($2)', `define(`item_'defn(`i'))popdef(`item_'defn(`i'))')dnl
  223. foreach(`i', `($1)', `popdef(`item_'defn(`i'))')dnl
  224. ')
  225. dnl }}}
  226. dnl {{{ DONE_*
  227. define(`DONE_SIZE', `IFAUTOCHECK(`dnl
  228. xc_autocheck_done_size += (int) $1`';
  229. xc_autocheck_done_count ++;
  230. ')')
  231. define(`DONE', `
  232. define(`ELEMENTS_DONE', defn(`ELEMENTS_DONE')`,"$1"')
  233. IFAUTOCHECK(`dnl
  234. if (zend_u_hash_exists(&xc_autocheck_done_names, IS_STRING, "$1", sizeof("$1"))) {
  235. fprintf(stderr
  236. , "duplicate field at %s `#'%d FUNC_NAME`' : %s\n"
  237. , __FILE__, __LINE__
  238. , "$1"
  239. );
  240. }
  241. else {
  242. zend_uchar b = 1;
  243. zend_hash_add(&xc_autocheck_done_names, "$1", sizeof("$1"), (void*)&b, sizeof(b), NULL);
  244. }
  245. ')
  246. DONE_SIZE(`sizeof(SRC(`$1'))')
  247. ')
  248. define(`DISABLECHECK', `
  249. pushdef(`DONE_SIZE')
  250. pushdef(`DONE')
  251. $1
  252. popdef(`DONE_SIZE')
  253. popdef(`DONE')
  254. ')
  255. dnl }}}
  256. dnl {{{ IF**
  257. define(`IFCALC', `ifelse(PROCESSOR_TYPE, `calc', `$1', `$2')')
  258. define(`IFSTORE', `ifelse(PROCESSOR_TYPE, `store', `$1', `$2')')
  259. define(`IFCALCSTORE', `IFSTORE(`$1', `IFCALC(`$1', `$2')')')
  260. define(`IFRESTORE', `ifelse(PROCESSOR_TYPE, `restore', `$1', `$2')')
  261. define(`IFCOPY', `IFSTORE(`$1', `IFRESTORE(`$1', `$2')')')
  262. define(`IFCALCCOPY', `IFCALC(`$1', `IFCOPY(`$1', `$2')')')
  263. define(`PROCRELOCATE', `ifelse(PROCESSOR_TYPE, `relocate', `$1', `$2')')
  264. define(`IFRELOCATE', `ifelse(defn(`RELOCATE_EX'), `', `$2', `$1')')
  265. define(`IFDPRINT', `ifelse(PROCESSOR_TYPE, `dprint', `$1', `$2')')
  266. define(`IFDASM', `ifelse(PROCESSOR_TYPE, `dasm', `$1', `$2')')
  267. dnl }}}
  268. EXPORT_PROCESSOR(`dasm', `zend_op_array')
  269. EXPORT_PROCESSOR(`dasm', `zend_function')
  270. EXPORT_PROCESSOR(`dasm', `zend_class_entry')
  271. EXPORT_PROCESSOR(`dasm', `zend_ast')
  272. EXPORT_PROCESSOR(`dprint', `zval')
  273. include(__dir__`/hashtable.m4')
  274. include(__dir__`/string.m4')
  275. include(__dir__`/struct.m4')
  276. include(__dir__`/process.m4')
  277. include(__dir__`/head.m4')
  278. dnl ==== calc ====
  279. REDEF(`PROCESSOR_TYPE', `calc')
  280. include(__dir__`/processor.m4')
  281. dnl ==== store ====
  282. pushdef(`RELOCATE_EX', `$2 = ptradd($1 *, notnullable($2), processor->relocatediff);')
  283. REDEF(`PROCESSOR_TYPE', `store')
  284. include(__dir__`/processor.m4')
  285. popdef(`RELOCATE_EX')
  286. dnl ==== restore ====
  287. REDEF(`PROCESSOR_TYPE', `restore')
  288. include(__dir__`/processor.m4')
  289. dnl ==== relocate ====
  290. pushdef(`PTR_FROM_VIRTUAL_EX', `ptradd($1 *, notnullable($2), ptrdiff)')
  291. pushdef(`RELOCATE_EX', `$2 = ptradd($1 *, notnullable($2), relocatediff);')
  292. pushdef(`SRC', defn(`DST'))
  293. REDEF(`PROCESSOR_TYPE', `relocate')
  294. include(__dir__`/processor.m4')
  295. popdef(`SRC')
  296. popdef(`RELOCATE_EX')
  297. popdef(`PTR_FROM_VIRTUAL_EX')
  298. dnl ==== dprint ====
  299. #ifdef HAVE_XCACHE_DPRINT
  300. REDEF(`PROCESSOR_TYPE', `dprint') include(__dir__`/processor.m4')
  301. #endif /* HAVE_XCACHE_DPRINT */
  302. dnl ==== dasm ====
  303. #ifdef HAVE_XCACHE_DISASSEMBLER
  304. REDEF(`PROCESSOR_TYPE', `dasm') include(__dir__`/processor.m4')
  305. #endif /* HAVE_XCACHE_DISASSEMBLER */
  306. undefine(`PROCESSOR_TYPE')
  307. include(__dir__`/foot.m4')
  308. ifdef(`EXIT_PENDING', `m4exit(EXIT_PENDING)')