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.
 
 
 
 
 
 

251 lines
5.3 KiB

  1. #define XC_SHM_IMPL _xc_malloc_shm_t
  2. #define XC_MEM_IMPL _xc_malloc_mem_t
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include "xc_shm.h"
  10. #include "php.h"
  11. #include "xc_utils.h"
  12. #include "util/xc_align.h"
  13. struct _xc_malloc_mem_t {
  14. const xc_mem_handlers_t *handlers;
  15. xc_shm_t *shm;
  16. xc_memsize_t size;
  17. xc_memsize_t avail; /* total free */
  18. };
  19. /* {{{ _xc_malloc_shm_t */
  20. struct _xc_malloc_shm_t {
  21. xc_shm_handlers_t *handlers;
  22. xc_shmsize_t size;
  23. xc_shmsize_t memoffset;
  24. #ifdef HAVE_XCACHE_TEST
  25. HashTable blocks;
  26. #endif
  27. };
  28. /* }}} */
  29. #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
  30. static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */
  31. {
  32. #ifdef HAVE_XCACHE_TEST
  33. if (p) {
  34. zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
  35. }
  36. #endif
  37. return p;
  38. }
  39. /* }}} */
  40. static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */
  41. {
  42. #ifdef HAVE_XCACHE_TEST
  43. zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p));
  44. #endif
  45. }
  46. /* }}} */
  47. static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
  48. {
  49. return xc_add_to_blocks(mem, malloc(size), size);
  50. }
  51. /* }}} */
  52. static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
  53. {
  54. xc_del_from_blocks(mem, (void *) p);
  55. free((void *) p);
  56. return 0;
  57. }
  58. /* }}} */
  59. static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
  60. {
  61. return xc_add_to_blocks(mem, calloc(memb, size), size);
  62. }
  63. /* }}} */
  64. static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
  65. {
  66. return xc_add_to_blocks(mem, realloc((void *) p, size), size);
  67. }
  68. /* }}} */
  69. static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
  70. {
  71. char *p = xc_add_to_blocks(mem, malloc(len), len);
  72. if (!p) {
  73. return NULL;
  74. }
  75. return memcpy(p, str, len);
  76. }
  77. /* }}} */
  78. static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
  79. {
  80. return xc_malloc_strndup(mem, str, strlen(str) + 1);
  81. }
  82. /* }}} */
  83. static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
  84. {
  85. return mem->avail;
  86. }
  87. /* }}} */
  88. static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
  89. {
  90. return mem->size;
  91. }
  92. /* }}} */
  93. static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
  94. {
  95. return (void *) -1;
  96. }
  97. /* }}} */
  98. XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
  99. {
  100. return NULL;
  101. }
  102. /* }}} */
  103. XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
  104. {
  105. return 0;
  106. }
  107. /* }}} */
  108. XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
  109. {
  110. return 0;
  111. }
  112. /* }}} */
  113. static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
  114. {
  115. #define MINSIZE (ALIGN(sizeof(xc_mem_t)))
  116. /* requires at least the header and 1 tail block */
  117. if (size < MINSIZE) {
  118. fprintf(stderr, "xc_mem_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
  119. return NULL;
  120. }
  121. mem->shm = shm;
  122. mem->size = size;
  123. mem->avail = size - MINSIZE;
  124. #undef MINSIZE
  125. return mem;
  126. }
  127. /* }}} */
  128. static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
  129. {
  130. }
  131. /* }}} */
  132. static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
  133. {
  134. return 0;
  135. }
  136. /* }}} */
  137. static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
  138. {
  139. #ifdef HAVE_XCACHE_TEST
  140. HashPosition pos;
  141. size_t *psize;
  142. char **ptr;
  143. zend_hash_internal_pointer_reset_ex(&shm->blocks, &pos);
  144. while (zend_hash_get_current_data_ex(&shm->blocks, (void **) &psize, &pos) == SUCCESS) {
  145. zend_hash_get_current_key_ex(&shm->blocks, (void *) &ptr, NULL, NULL, 0, &pos);
  146. if ((char *) p >= *ptr && (char *) p < *ptr + *psize) {
  147. return 1;
  148. }
  149. zend_hash_move_forward_ex(&shm->blocks, &pos);
  150. }
  151. #endif
  152. return 0;
  153. }
  154. /* }}} */
  155. static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
  156. {
  157. return 0;
  158. }
  159. /* }}} */
  160. static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */
  161. {
  162. return p;
  163. }
  164. /* }}} */
  165. static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
  166. {
  167. return p;
  168. }
  169. /* }}} */
  170. static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
  171. {
  172. #ifdef HAVE_XCACHE_TEST
  173. zend_hash_destroy(&shm->blocks);
  174. #endif
  175. free(shm);
  176. return;
  177. }
  178. /* }}} */
  179. static XC_SHM_INIT(xc_malloc_init) /* {{{ */
  180. {
  181. xc_shm_t *shm;
  182. CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
  183. shm->size = size;
  184. #ifdef HAVE_XCACHE_TEST
  185. zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
  186. #endif
  187. return shm;
  188. err:
  189. return NULL;
  190. }
  191. /* }}} */
  192. static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
  193. {
  194. xc_mem_t *mem;
  195. if (shm->memoffset + size > shm->size) {
  196. zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
  197. return NULL;
  198. }
  199. shm->memoffset += size;
  200. CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
  201. mem->handlers = shm->handlers->memhandlers;
  202. mem->handlers->init(shm, mem, size);
  203. return mem;
  204. err:
  205. return NULL;
  206. }
  207. /* }}} */
  208. static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
  209. {
  210. mem->handlers->destroy(mem);
  211. free(mem);
  212. }
  213. /* }}} */
  214. #define xc_malloc_destroy xc_mem_malloc_destroy
  215. #define xc_malloc_init xc_mem_malloc_init
  216. static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
  217. #undef xc_malloc_init
  218. #undef xc_malloc_destroy
  219. static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
  220. void xc_shm_malloc_register() /* {{{ */
  221. {
  222. if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
  223. zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
  224. }
  225. CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
  226. if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
  227. zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
  228. }
  229. err:
  230. return;
  231. }
  232. /* }}} */