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