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.
 
 
 
 
 
 

120 lines
2.7 KiB

  1. #ifdef TEST
  2. # ifdef HAVE_CONFIG_H
  3. # include <config.h>
  4. # endif
  5. #else
  6. # include "xcache.h"
  7. #endif
  8. #include "xc_allocator.h"
  9. #include <string.h>
  10. #include <stdio.h>
  11. typedef struct {
  12. const char *name;
  13. const xc_allocator_vtable_t *allocator_vtable;
  14. } xc_allocator_info_t;
  15. static xc_allocator_info_t xc_allocator_infos[10];
  16. int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable) /* {{{ */
  17. {
  18. size_t i;
  19. for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]); i ++) {
  20. if (!xc_allocator_infos[i].name) {
  21. xc_allocator_infos[i].name = name;
  22. xc_allocator_infos[i].allocator_vtable = allocator_vtable;
  23. return 1;
  24. }
  25. }
  26. return 0;
  27. }
  28. /* }}} */
  29. const xc_allocator_vtable_t *xc_allocator_find(const char *name) /* {{{ */
  30. {
  31. size_t i;
  32. for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]) && xc_allocator_infos[i].name; i ++) {
  33. if (strcmp(xc_allocator_infos[i].name, name) == 0) {
  34. return xc_allocator_infos[i].allocator_vtable;
  35. }
  36. }
  37. return NULL;
  38. }
  39. /* }}} */
  40. void xc_allocator_init() /* {{{ */
  41. {
  42. extern void xc_allocator_bestfit_register();
  43. #ifdef HAVE_XCACHE_TEST
  44. extern void xc_allocator_malloc_register();
  45. #endif
  46. memset(xc_allocator_infos, 0, sizeof(xc_allocator_infos));
  47. xc_allocator_bestfit_register();
  48. #ifdef HAVE_XCACHE_TEST
  49. xc_allocator_malloc_register();
  50. #endif
  51. }
  52. /* }}} */
  53. #ifdef TEST
  54. /* {{{ testing */
  55. #undef CHECK
  56. #define CHECK(a, msg) do { \
  57. if (!(a)) { \
  58. fprintf(stderr, "%s\n", msg); return -1; \
  59. } \
  60. } while (0)
  61. #include <time.h>
  62. int testAllocator(const xc_allocator_vtable_t *allocator_vtable)
  63. {
  64. int count = 0;
  65. void *p;
  66. xc_allocator_t *allocator;
  67. void *memory;
  68. void **ptrs;
  69. int size, i;
  70. #if 0
  71. fprintf(stderr, "%s", "Input test size: ");
  72. scanf("%d", &size);
  73. #else
  74. size = 1024;
  75. #endif
  76. CHECK(memory = malloc(size), "OOM");
  77. CHECK(ptrs = malloc(size * sizeof(void *)), "OOM");
  78. allocator = (xc_allocator_t *) memory;
  79. allocator->vtable = allocator_vtable;
  80. CHECK(allocator = allocator->vtable->init(NULL, allocator, size), "Failed init memory allocator");
  81. while ((p = allocator->vtable->malloc(allocator, 1))) {
  82. ptrs[count ++] = p;
  83. }
  84. fprintf(stderr, "count=%d, random freeing\n", count);
  85. srandom(time(NULL));
  86. while (count) {
  87. i = (random() % count);
  88. fprintf(stderr, "freeing %d: ", i);
  89. allocator->vtable->free(allocator, ptrs[i]);
  90. ptrs[i] = ptrs[count - 1];
  91. count --;
  92. }
  93. free(ptrs);
  94. free(memory);
  95. return 0;
  96. }
  97. /* }}} */
  98. int main() /* {{{ */
  99. {
  100. int i;
  101. xc_allocator_init();
  102. for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]) && xc_allocator_infos[i].name; i ++) {
  103. fprintf(stderr, "testing %s...\n", xc_allocator_infos[i].name);
  104. testAllocator(xc_allocator_infos[i].allocator_vtable);
  105. }
  106. return 0;
  107. }
  108. /* }}} */
  109. #endif