source: trunk/xcache/xc_allocator.c @ 1378

Last change on this file since 1378 was 1378, checked in by moo, 14 months ago

use config.h correctly wherever needed

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1#include "xcache.h"
2#include "xc_allocator.h"
3#include <string.h>
4#include <stdio.h>
5
6typedef struct {
7    const char *name;
8    const xc_allocator_vtable_t *allocator_vtable;
9} xc_allocator_info_t;
10static xc_allocator_info_t xc_allocator_infos[10];
11
12int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable) /* {{{ */
13{
14    size_t i;
15    for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]); i ++) {
16        if (!xc_allocator_infos[i].name) {
17            xc_allocator_infos[i].name = name;
18            xc_allocator_infos[i].allocator_vtable = allocator_vtable;
19            return 1;
20        }
21    }
22    return 0;
23}
24/* }}} */
25const xc_allocator_vtable_t *xc_allocator_find(const char *name) /* {{{ */
26{
27    size_t i;
28    for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]) && xc_allocator_infos[i].name; i ++) {
29        if (strcmp(xc_allocator_infos[i].name, name) == 0) {
30            return xc_allocator_infos[i].allocator_vtable;
31        }
32    }
33    return NULL;
34}
35/* }}} */
36void xc_allocator_init() /* {{{ */
37{
38    extern void xc_allocator_bestfit_register();
39#ifdef HAVE_XCACHE_TEST
40    extern void xc_allocator_malloc_register();
41#endif
42
43    memset(xc_allocator_infos, 0, sizeof(xc_allocator_infos));
44    xc_allocator_bestfit_register();
45#ifdef HAVE_XCACHE_TEST
46    xc_allocator_malloc_register();
47#endif
48}
49/* }}} */
50#ifdef TEST
51/* {{{ testing */
52#undef CHECK
53#define CHECK(a, msg) do { \
54    if (!(a)) { \
55        fprintf(stderr, "%s\n", msg); return -1; \
56    } \
57} while (0)
58
59#include <time.h>
60
61int testAllocator(const xc_allocator_vtable_t *allocator_vtable)
62{
63    int count = 0;
64    void *p;
65    xc_allocator_t *allocator;
66    void *memory;
67    void **ptrs;
68    int size, i;
69
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
82    while ((p = allocator->vtable->malloc(allocator, 1))) {
83        ptrs[count ++] = p;
84    }
85    fprintf(stderr, "count=%d, random freeing\n", count);
86    srandom(time(NULL));
87    while (count) {
88        i = (random() % count);
89        fprintf(stderr, "freeing %d: ", i);
90        allocator->vtable->free(allocator, ptrs[i]);
91        ptrs[i] = ptrs[count - 1];
92        count --;
93    }
94
95    free(ptrs);
96    free(memory);
97    return 0;
98}
99/* }}} */
100int main() /* {{{ */
101{
102    int i;
103
104    xc_allocator_init();
105
106    for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]) && xc_allocator_infos[i].name; i ++) {
107        fprintf(stderr, "testing %s...\n", xc_allocator_infos[i].name);
108        testAllocator(xc_allocator_infos[i].allocator_vtable);
109    }
110    return 0;
111}
112/* }}} */
113#endif
Note: See TracBrowser for help on using the repository browser.