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.

210 lines
4.1 KiB

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