source: trunk/xcache/xc_allocator.c @ 1135

Last change on this file since 1135 was 1135, checked in by moo, 2 years ago

refactor: s/mem/allocator/

  • Property svn:eol-style set to native
File size: 2.6 KB
RevLine 
[1135]1#include "xc_allocator.h"
[49]2#include <string.h>
[1135]3#include <stdio.h>
[1]4
[1135]5typedef struct {
6    const char *name;
7    const xc_allocator_vtable_t *allocator_vtable;
8} xc_allocator_info_t;
9static xc_allocator_info_t xc_allocator_infos[10];
[49]10
[1135]11int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable) /* {{{ */
[1]12{
[1135]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        }
[1]20    }
[1135]21    return 0;
[1]22}
23/* }}} */
[1135]24const xc_allocator_vtable_t *xc_allocator_find(const char *name) /* {{{ */
[1]25{
[1135]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;
[1]30        }
31    }
[1135]32    return NULL;
[1]33}
34/* }}} */
[1135]35void xc_allocator_init() /* {{{ */
[1]36{
[1135]37    extern void xc_allocator_bestfit_register();
38#ifdef HAVE_XCACHE_TEST
39    extern void xc_allocator_malloc_register();
40#endif
[1]41
[1135]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
[1]47}
48/* }}} */
[49]49#ifdef TEST
[989]50/* {{{ testing */
[49]51#undef CHECK
[998]52#define CHECK(a, msg) do { \
53    if (!(a)) { \
54        fprintf(stderr, "%s\n", msg); return -1; \
55    } \
56} while (0)
57
[49]58#include <time.h>
59
[1135]60int testAllocator(const xc_allocator_vtable_t *allocator_vtable)
[49]61{
62    int count = 0;
63    void *p;
[1135]64    xc_allocator_t *allocator;
[49]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
[999]73    size = 1024;
[49]74#endif
75    CHECK(memory = malloc(size), "OOM");
[998]76    CHECK(ptrs   = malloc(size * sizeof(void *)), "OOM");
[1135]77    allocator = (xc_allocator_t *) memory;
78    allocator->vtable = allocator_vtable;
79    CHECK(allocator = allocator->vtable->init(NULL, allocator, size), "Failed init memory allocator");
[49]80
[1135]81    while ((p = allocator->vtable->malloc(allocator, 1))) {
[49]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);
[1135]89        allocator->vtable->free(allocator, ptrs[i]);
[49]90        ptrs[i] = ptrs[count - 1];
91        count --;
92    }
93
[54]94    free(ptrs);
[49]95    free(memory);
96    return 0;
97}
98/* }}} */
[1135]99int main() /* {{{ */
[148]100{
101    int i;
102
[1135]103    xc_allocator_init();
[148]104
[1135]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);
[148]108    }
[1135]109    return 0;
[148]110}
111/* }}} */
[1135]112#endif
Note: See TracBrowser for help on using the repository browser.