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.

170 lines
5.4 KiB

  1. dnl {{{ xc_store_target
  2. EXPORT(`typedef void *(*xc_processor_storage_allocator_t)(void *data, size_t size);')
  3. EXPORT(`typedef struct { xc_processor_storage_allocator_t allocator; void *allocator_data; ptrdiff_t relocatediff; } xc_processor_storage_t;')
  4. dnl }}}
  5. define(`DEFINE_STORE_API', `
  6. EXPORTED_FUNCTION(`$1 *xc_processor_store_$1(const xc_processor_storage_t *storage, $1 *src TSRMLS_DC)') dnl {{{
  7. {
  8. $1 *dst;
  9. xc_processor_t processor;
  10. memset(&processor, 0, sizeof(processor));
  11. processor.handle_reference = 1;
  12. processor.relocatediff = storage->relocatediff;
  13. IFAUTOCHECK(`xc_vector_init(unsigned long, &processor.allocsizes, 0);')
  14. /* calc size */ {
  15. zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
  16. if (processor.handle_reference) {
  17. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  18. }
  19. processor.size = 0;
  20. /* allocate */
  21. processor.size = ALIGN(processor.size + sizeof(src[0]));
  22. xc_calc_$1(&processor, src TSRMLS_CC);
  23. if (processor.handle_reference) {
  24. zend_hash_destroy(&processor.zvalptrs);
  25. }
  26. zend_hash_destroy(&processor.strings);
  27. }
  28. ifelse(
  29. `$1', `xc_entry_data_php_t', `SRC(`size')',
  30. `', `', SRC(`entry.size')) = processor.size;
  31. ifelse(
  32. `$1', `xc_entry_var_t', `SRC(`have_references') = processor.have_references;',
  33. `$1', `xc_entry_data_php_t', `SRC(`have_references') = processor.have_references;'
  34. )
  35. IFAUTOCHECK(`xc_vector_reverse(&processor.allocsizes);')
  36. /* store {{{ */
  37. {
  38. IFAUTOCHECK(`char *oldp;')
  39. zend_hash_init(&processor.strings, 0, NULL, NULL, 0);
  40. if (processor.handle_reference) {
  41. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  42. }
  43. /* allocator :) */
  44. processor.p = (char *) storage->allocator(storage->allocator_data, processor.size);
  45. if (processor.p == NULL) {
  46. dst = NULL;
  47. goto err_alloc;
  48. }
  49. IFAUTOCHECK(`oldp = processor.p;')
  50. assert(processor.p == (char *) ALIGN(processor.p));
  51. /* allocate */
  52. dst = ($1 *) processor.p;
  53. processor.p = (char *) ALIGN(processor.p + sizeof(dst[0]));
  54. xc_store_$1(&processor, dst, src TSRMLS_CC);
  55. IFAUTOCHECK(` {
  56. size_t unexpected = processor.p - oldp;
  57. size_t expected = processor.size;
  58. if (unexpected != processor.size) {
  59. fprintf(stderr, "unexpected:%lu - expected:%lu = %ld != 0\n", (unsigned long) unexpected, (unsigned long) expected, (long) unexpected - expected);
  60. abort();
  61. }
  62. }')
  63. err_alloc:
  64. if (processor.handle_reference) {
  65. zend_hash_destroy(&processor.zvalptrs);
  66. }
  67. zend_hash_destroy(&processor.strings);
  68. }
  69. /* }}} */
  70. IFAUTOCHECK(`xc_vector_destroy(&processor.allocsizes);')
  71. return dst;
  72. }
  73. dnl }}}
  74. ')
  75. DEFINE_STORE_API(`xc_entry_var_t')
  76. DEFINE_STORE_API(`xc_entry_php_t')
  77. DEFINE_STORE_API(`xc_entry_data_php_t')
  78. EXPORTED_FUNCTION(`xc_entry_php_t *xc_processor_restore_xc_entry_php_t(xc_entry_php_t *dst, const xc_entry_php_t *src TSRMLS_DC)') dnl {{{
  79. {
  80. xc_processor_t processor;
  81. memset(&processor, 0, sizeof(processor));
  82. xc_restore_xc_entry_php_t(&processor, dst, src TSRMLS_CC);
  83. return dst;
  84. }
  85. dnl }}}
  86. EXPORTED_FUNCTION(`xc_entry_data_php_t *xc_processor_restore_xc_entry_data_php_t(const xc_entry_php_t *entry_php, xc_entry_data_php_t *dst, const xc_entry_data_php_t *src, zend_bool readonly_protection TSRMLS_DC)') dnl {{{
  87. {
  88. xc_processor_t processor;
  89. memset(&processor, 0, sizeof(processor));
  90. processor.readonly_protection = readonly_protection;
  91. /* this function is used for php data only */
  92. if (SRC(`have_references')) {
  93. processor.handle_reference = 1;
  94. }
  95. processor.entry_php_src = entry_php;
  96. if (processor.handle_reference) {
  97. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  98. }
  99. xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
  100. if (processor.handle_reference) {
  101. zend_hash_destroy(&processor.zvalptrs);
  102. }
  103. return dst;
  104. }
  105. dnl }}}
  106. EXPORTED_FUNCTION(`zval *xc_processor_restore_zval(zval *dst, const zval *src, zend_bool have_references TSRMLS_DC)') dnl {{{
  107. {
  108. xc_processor_t processor;
  109. memset(&processor, 0, sizeof(processor));
  110. processor.handle_reference = have_references;
  111. if (processor.handle_reference) {
  112. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  113. dnl fprintf(stderr, "mark[%p] = %p\n", src, dst);
  114. zend_hash_add(&processor.zvalptrs, (char *)src, sizeof(src), (void*)&dst, sizeof(dst), NULL);
  115. }
  116. xc_restore_zval(&processor, dst, src TSRMLS_CC);
  117. if (processor.handle_reference) {
  118. zend_hash_destroy(&processor.zvalptrs);
  119. }
  120. return dst;
  121. }
  122. dnl }}}
  123. define(`DEFINE_RELOCATE_API', `
  124. /* src = readable element, before memcpy if any
  125. * dst = writable element, after memcpy if any
  126. * virtual_src = brother pointers relatived to this address, before relocation
  127. * virtual_dst = brother pointers relatived to this address, after relocation
  128. */
  129. EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
  130. {
  131. char *old_address = 0; /* unkown X used later */
  132. ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
  133. /* diff to new_ptr */
  134. ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
  135. ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
  136. xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
  137. }
  138. dnl }}}
  139. ')
  140. DEFINE_RELOCATE_API(`xc_entry_var_t')
  141. DEFINE_RELOCATE_API(`xc_entry_php_t')
  142. DEFINE_RELOCATE_API(`xc_entry_data_php_t')
  143. EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
  144. EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
  145. {
  146. IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
  147. xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
  148. }
  149. dnl }}}
  150. EXPORTED(`#endif')