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.

197 lines
6.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);')
  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_php_t')
  76. DEFINE_STORE_API(`xc_entry_data_php_t')
  77. DEFINE_STORE_API(`xc_entry_var_t')
  78. EXPORTED_FUNCTION(`void 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. }
  84. dnl }}}
  85. EXPORTED_FUNCTION(`void 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 {{{
  86. {
  87. xc_processor_t processor;
  88. memset(&processor, 0, sizeof(processor));
  89. processor.readonly_protection = readonly_protection;
  90. /* this function is used for php data only */
  91. if (SRC(`have_references')) {
  92. processor.handle_reference = 1;
  93. }
  94. processor.entry_php_src = entry_php;
  95. if (processor.handle_reference) {
  96. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  97. }
  98. xc_restore_xc_entry_data_php_t(&processor, dst, src TSRMLS_CC);
  99. if (processor.handle_reference) {
  100. zend_hash_destroy(&processor.zvalptrs);
  101. }
  102. }
  103. dnl }}}
  104. EXPORTED_FUNCTION(`void xc_processor_restore_var(zval *dst, zval **dst_ptr, const xc_entry_var_t *src, zend_class_entry **index_to_ce TSRMLS_DC)') dnl {{{
  105. {
  106. xc_processor_t processor;
  107. memset(&processor, 0, sizeof(processor));
  108. processor.handle_reference = src->have_references;
  109. if (processor.handle_reference) {
  110. zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
  111. if (dst_ptr) {
  112. zval_ptr_dtor(dst_ptr);
  113. }
  114. }
  115. processor.index_to_ce = index_to_ce;
  116. #ifdef ZEND_ENGINE_2
  117. if (src->objects_count) {
  118. int i;
  119. size_t size = sizeof(processor.object_handles[0]) * src->objects_count;
  120. processor.object_handles = emalloc(size);
  121. IFAUTOCHECK(`xc_memsetptr(processor.object_handles, (void *) -1, size);')
  122. /* other objects in $object->properties after $object, reverse order is necessary */
  123. for (i = src->objects_count - 1; i >= 0; --i) {
  124. zend_object *object = emalloc(sizeof(*object));
  125. processor.object_handles[i] = zend_objects_store_put(object, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
  126. xc_restore_zend_object(&processor, object, &src->objects[i] TSRMLS_CC);
  127. }
  128. }
  129. #endif
  130. if (dst_ptr) {
  131. xc_restore_zval_ptr(&processor, dst_ptr, &src->value TSRMLS_CC);
  132. }
  133. else {
  134. xc_restore_zval(&processor, dst, src->value TSRMLS_CC);
  135. }
  136. if (processor.handle_reference) {
  137. zend_hash_destroy(&processor.zvalptrs);
  138. }
  139. #ifdef ZEND_ENGINE_2
  140. if (src->objects_count) {
  141. zend_uint i;
  142. for (i = 0; i < src->objects_count; ++i) {
  143. zend_objects_store_del_ref_by_handle(processor.object_handles[i] TSRMLS_CC);
  144. }
  145. efree(processor.object_handles);
  146. }
  147. #endif
  148. }
  149. dnl }}}
  150. define(`DEFINE_RELOCATE_API', `
  151. /* src = readable element, before memcpy if any
  152. * dst = writable element, after memcpy if any
  153. * virtual_src = brother pointers relatived to this address, before relocation
  154. * virtual_dst = brother pointers relatived to this address, after relocation
  155. */
  156. EXPORTED_FUNCTION(`void xc_processor_relocate_$1($1 *dst, $1 *virtual_dst, $1 *src, $1 *virtual_src TSRMLS_DC)') dnl {{{
  157. {
  158. char *old_address = 0; /* unkown X used later */
  159. ptrdiff_t offset = ptrsub(old_address, (ptrdiff_t) virtual_src);
  160. /* diff to new_ptr */
  161. ptrdiff_t ptrdiff = ptrsub(dst, virtual_src);
  162. ptrdiff_t relocatediff = (ptrdiff_t) ptradd($1 *, virtual_dst, offset);
  163. xc_relocate_$1(dst, ptrdiff, relocatediff TSRMLS_CC);
  164. }
  165. dnl }}}
  166. ')
  167. DEFINE_RELOCATE_API(`xc_entry_var_t')
  168. DEFINE_RELOCATE_API(`xc_entry_php_t')
  169. DEFINE_RELOCATE_API(`xc_entry_data_php_t')
  170. EXPORTED(`#ifdef HAVE_XCACHE_DPRINT')
  171. EXPORTED_FUNCTION(`void xc_dprint(xc_entry_php_t *src, int indent TSRMLS_DC)') dnl {{{
  172. {
  173. IFDPRINT(`INDENT()`'fprintf(stderr, "xc_entry_php_t:src");')
  174. xc_dprint_xc_entry_php_t(src, indent TSRMLS_CC);
  175. }
  176. dnl }}}
  177. EXPORTED(`#endif')