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.
 
 
 
 
 
 

321 lines
7.7 KiB

  1. #define _xc_allocator_t _xc_allocator_bestfit_t
  2. #define _xc_allocator_block_t _xc_allocator_bestfit_block_t
  3. #include "xc_allocator.h"
  4. #undef _xc_allocator_t
  5. #undef _xc_allocator_block_t
  6. #ifdef TEST
  7. # include <limits.h>
  8. # include <stdio.h>
  9. # define XCACHE_DEBUG
  10. typedef int zend_bool;
  11. # define ZEND_ATTRIBUTE_PTR_FORMAT(a, b, c)
  12. # define zend_error(type, error) fprintf(stderr, "%s", error)
  13. #else
  14. # include <php.h>
  15. #endif
  16. #ifdef XCACHE_DEBUG
  17. # define ALLOC_DEBUG_BLOCK_CHECK
  18. #endif
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "xc_shm.h"
  23. #include "util/xc_align.h"
  24. #include "util/xc_trace.h"
  25. #if 0
  26. #undef ALLOC_DEBUG_BLOCK_CHECK
  27. #endif
  28. #define CHAR_PTR(p) ((char *) (p))
  29. #define PADD(p, a) (CHAR_PTR(p) + a)
  30. #define PSUB(p1, p2) (CHAR_PTR(p1) - CHAR_PTR(p2))
  31. /* {{{ allocator */
  32. typedef struct _xc_allocator_bestfit_block_t xc_allocator_bestfit_block_t;
  33. struct _xc_allocator_bestfit_block_t {
  34. #ifdef ALLOC_DEBUG_BLOCK_CHECK
  35. unsigned int magic;
  36. #endif
  37. xc_memsize_t size; /* reserved even after alloc */
  38. xc_allocator_bestfit_block_t *next; /* not used after alloc */
  39. };
  40. typedef struct _xc_allocator_bestfit_t {
  41. const xc_allocator_vtable_t *vtable;
  42. xc_shm_t *shm;
  43. xc_memsize_t size;
  44. xc_memsize_t avail; /* total free */
  45. xc_allocator_bestfit_block_t headblock[1]; /* just as a pointer to first block*/
  46. } xc_allocator_bestfit_t;
  47. #include <stddef.h>
  48. #define SizeOf(type, field) sizeof( ((type *) 0)->field )
  49. #define BLOCK_HEADER_SIZE() (ALIGN( offsetof(xc_allocator_bestfit_block_t, size) + SizeOf(xc_allocator_bestfit_block_t, size) ))
  50. #define BLOCK_MAGIC ((unsigned int) 0x87655678)
  51. /* }}} */
  52. static inline void xc_block_setup(xc_allocator_bestfit_block_t *b, xc_memsize_t size, xc_allocator_bestfit_block_t *next) /* {{{ */
  53. {
  54. #ifdef ALLOC_DEBUG_BLOCK_CHECK
  55. b->magic = BLOCK_MAGIC;
  56. #endif
  57. b->size = size;
  58. b->next = next;
  59. }
  60. /* }}} */
  61. #ifdef ALLOC_DEBUG_BLOCK_CHECK
  62. static void xc_block_check(xc_allocator_bestfit_block_t *b) /* {{{ */
  63. {
  64. if (b->magic != BLOCK_MAGIC) {
  65. fprintf(stderr, "0x%X != 0x%X magic wrong \n", b->magic, BLOCK_MAGIC);
  66. }
  67. }
  68. /* }}} */
  69. #else
  70. # define xc_block_check(b) do { } while(0)
  71. #endif
  72. static XC_ALLOCATOR_MALLOC(xc_allocator_bestfit_malloc) /* {{{ */
  73. {
  74. xc_allocator_bestfit_block_t *prev, *cur;
  75. xc_allocator_bestfit_block_t *newb, *b;
  76. xc_memsize_t realsize;
  77. xc_memsize_t minsize;
  78. void *p;
  79. /* [xc_allocator_bestfit_block_t:size|size] */
  80. realsize = BLOCK_HEADER_SIZE() + size;
  81. /* realsize is ALIGNed so next block start at ALIGNed address */
  82. realsize = ALIGN(realsize);
  83. TRACE("avail: %lu (%luKB). Allocate size: %lu realsize: %lu (%luKB)"
  84. , allocator->avail, allocator->avail / 1024
  85. , size
  86. , realsize, realsize / 1024
  87. );
  88. do {
  89. p = NULL;
  90. if (allocator->avail < realsize) {
  91. TRACE("%s", " oom");
  92. break;
  93. }
  94. b = NULL;
  95. minsize = ULONG_MAX;
  96. /* prev|cur */
  97. for (prev = allocator->headblock; prev->next; prev = cur) {
  98. /* while (prev->next != 0) { */
  99. cur = prev->next;
  100. xc_block_check(cur);
  101. if (cur->size == realsize) {
  102. /* found a perfect fit, stop searching */
  103. b = prev;
  104. break;
  105. }
  106. /* make sure we can split on the block */
  107. else if (cur->size > (sizeof(xc_allocator_bestfit_block_t) + realsize) &&
  108. cur->size < minsize) {
  109. /* cur is acceptable and memller */
  110. b = prev;
  111. minsize = cur->size;
  112. }
  113. prev = cur;
  114. }
  115. if (b == NULL) {
  116. TRACE("%s", " no fit chunk");
  117. break;
  118. }
  119. prev = b;
  120. cur = prev->next;
  121. p = PADD(cur, BLOCK_HEADER_SIZE());
  122. /* update the block header */
  123. allocator->avail -= realsize;
  124. /* perfect fit, just unlink */
  125. if (cur->size == realsize) {
  126. prev->next = cur->next;
  127. TRACE(" perfect fit. Got: %p", p);
  128. break;
  129. }
  130. /* make new free block after alloced space */
  131. /* save, as it might be overwrited by newb (cur->size is ok) */
  132. b = cur->next;
  133. /* prev|cur |next=b */
  134. newb = (xc_allocator_bestfit_block_t *)PADD(cur, realsize);
  135. xc_block_setup(newb, cur->size - realsize, b);
  136. cur->size = realsize;
  137. /* prev|cur|newb|next
  138. * `--^
  139. */
  140. TRACE(" -> avail: %lu (%luKB). new next: %p offset: %lu %luKB. Got: %p"
  141. , allocator->avail, allocator->avail / 1024
  142. , newb
  143. , PSUB(newb, allocator), PSUB(newb, allocator) / 1024
  144. , p
  145. );
  146. prev->next = newb;
  147. /* prev|cur|newb|next
  148. * `-----^
  149. */
  150. } while (0);
  151. return p;
  152. }
  153. /* }}} */
  154. static XC_ALLOCATOR_FREE(xc_allocator_bestfit_free) /* {{{ return block size freed */
  155. {
  156. xc_allocator_bestfit_block_t *cur, *b;
  157. int size;
  158. cur = (xc_allocator_bestfit_block_t *) (CHAR_PTR(p) - BLOCK_HEADER_SIZE());
  159. TRACE("freeing: %p, size=%lu", p, cur->size);
  160. xc_block_check(cur);
  161. assert((char*)allocator < (char*)cur);
  162. assert((char*)cur < (char*)allocator + allocator->size);
  163. /* find free block right before the p */
  164. b = allocator->headblock;
  165. while (b->next != 0 && b->next < cur) {
  166. b = b->next;
  167. }
  168. /* restore block */
  169. cur->next = b->next;
  170. b->next = cur;
  171. size = cur->size;
  172. TRACE(" avail %lu (%luKB)", allocator->avail, allocator->avail / 1024);
  173. allocator->avail += size;
  174. /* combine prev|cur */
  175. if (PADD(b, b->size) == (char *)cur) {
  176. b->size += cur->size;
  177. b->next = cur->next;
  178. cur = b;
  179. TRACE("%s", " combine prev");
  180. }
  181. /* combine cur|next */
  182. b = cur->next;
  183. if (PADD(cur, cur->size) == (char *)b) {
  184. cur->size += b->size;
  185. cur->next = b->next;
  186. TRACE("%s", " combine next");
  187. }
  188. TRACE(" -> avail %lu (%luKB)", allocator->avail, allocator->avail / 1024);
  189. return size;
  190. }
  191. /* }}} */
  192. static XC_ALLOCATOR_CALLOC(xc_allocator_bestfit_calloc) /* {{{ */
  193. {
  194. xc_memsize_t realsize = memb * size;
  195. void *p = xc_allocator_bestfit_malloc(allocator, realsize);
  196. if (p) {
  197. memset(p, 0, realsize);
  198. }
  199. return p;
  200. }
  201. /* }}} */
  202. static XC_ALLOCATOR_REALLOC(xc_allocator_bestfit_realloc) /* {{{ */
  203. {
  204. void *newp = xc_allocator_bestfit_malloc(allocator, size);
  205. if (p && newp) {
  206. memcpy(newp, p, size);
  207. xc_allocator_bestfit_free(allocator, p);
  208. }
  209. return newp;
  210. }
  211. /* }}} */
  212. static XC_ALLOCATOR_AVAIL(xc_allocator_bestfit_avail) /* {{{ */
  213. {
  214. return allocator->avail;
  215. }
  216. /* }}} */
  217. static XC_ALLOCATOR_SIZE(xc_allocator_bestfit_size) /* {{{ */
  218. {
  219. return allocator->size;
  220. }
  221. /* }}} */
  222. static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_bestfit_freeblock_first) /* {{{ */
  223. {
  224. return allocator->headblock->next;
  225. }
  226. /* }}} */
  227. static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_bestfit_freeblock_next) /* {{{ */
  228. {
  229. return block->next;
  230. }
  231. /* }}} */
  232. static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_bestfit_block_size) /* {{{ */
  233. {
  234. return block->size;
  235. }
  236. /* }}} */
  237. static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_bestfit_block_offset) /* {{{ */
  238. {
  239. return ((char *) block) - ((char *) allocator);
  240. }
  241. /* }}} */
  242. static XC_ALLOCATOR_INIT(xc_allocator_bestfit_init) /* {{{ */
  243. {
  244. xc_allocator_bestfit_block_t *b;
  245. #define MINSIZE (ALIGN(sizeof(xc_allocator_bestfit_t)) + sizeof(xc_allocator_bestfit_block_t))
  246. /* requires at least the header and 1 tail block */
  247. if (size < MINSIZE) {
  248. fprintf(stderr, "xc_allocator_bestfit_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
  249. return NULL;
  250. }
  251. TRACE("size=%lu", size);
  252. allocator->shm = shm;
  253. allocator->size = size;
  254. allocator->avail = size - MINSIZE;
  255. /* pointer to first block, right after ALIGNed header */
  256. b = allocator->headblock;
  257. xc_block_setup(b, 0, (xc_allocator_bestfit_block_t *) PADD(allocator, ALIGN(sizeof(xc_allocator_bestfit_t))));
  258. /* first block*/
  259. b = b->next;
  260. xc_block_setup(b, allocator->avail, 0);
  261. #undef MINSIZE
  262. return allocator;
  263. }
  264. /* }}} */
  265. static XC_ALLOCATOR_DESTROY(xc_allocator_bestfit_destroy) /* {{{ */
  266. {
  267. }
  268. /* }}} */
  269. static xc_allocator_vtable_t xc_allocator_bestfit = XC_ALLOCATOR_VTABLE(allocator_bestfit);
  270. void xc_allocator_bestfit_register() /* {{{ */
  271. {
  272. if (xc_allocator_register("bestfit", &xc_allocator_bestfit) == 0) {
  273. zend_error(E_ERROR, "XCache: failed to register allocator 'bestfit'");
  274. }
  275. }
  276. /* }}} */