source: trunk/xcache/xc_allocator.c @ 1391

Last change on this file since 1391 was 1391, checked in by moo, 13 months ago

fix xtest

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