source: trunk/xcache/xc_allocator.c @ 1391

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

fix xtest

  • Property svn:eol-style set to native
File size: 2.7 KB
RevLine 
[1391]1#ifdef TEST
2#   ifdef HAVE_CONFIG_H
3#       include <config.h>
4#   endif
5#else
6#   include "xcache.h"
7#endif
[1135]8#include "xc_allocator.h"
[49]9#include <string.h>
[1135]10#include <stdio.h>
[1]11
[1135]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];
[49]17
[1135]18int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable) /* {{{ */
[1]19{
[1135]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        }
[1]27    }
[1135]28    return 0;
[1]29}
30/* }}} */
[1135]31const xc_allocator_vtable_t *xc_allocator_find(const char *name) /* {{{ */
[1]32{
[1135]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;
[1]37        }
38    }
[1135]39    return NULL;
[1]40}
41/* }}} */
[1135]42void xc_allocator_init() /* {{{ */
[1]43{
[1135]44    extern void xc_allocator_bestfit_register();
45#ifdef HAVE_XCACHE_TEST
46    extern void xc_allocator_malloc_register();
47#endif
[1]48
[1135]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
[1]54}
55/* }}} */
[49]56#ifdef TEST
[989]57/* {{{ testing */
[49]58#undef CHECK
[998]59#define CHECK(a, msg) do { \
60    if (!(a)) { \
61        fprintf(stderr, "%s\n", msg); return -1; \
62    } \
63} while (0)
64
[49]65#include <time.h>
66
[1135]67int testAllocator(const xc_allocator_vtable_t *allocator_vtable)
[49]68{
69    int count = 0;
70    void *p;
[1135]71    xc_allocator_t *allocator;
[49]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
[999]80    size = 1024;
[49]81#endif
82    CHECK(memory = malloc(size), "OOM");
[998]83    CHECK(ptrs   = malloc(size * sizeof(void *)), "OOM");
[1135]84    allocator = (xc_allocator_t *) memory;
85    allocator->vtable = allocator_vtable;
86    CHECK(allocator = allocator->vtable->init(NULL, allocator, size), "Failed init memory allocator");
[49]87
[1135]88    while ((p = allocator->vtable->malloc(allocator, 1))) {
[49]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);
[1135]96        allocator->vtable->free(allocator, ptrs[i]);
[49]97        ptrs[i] = ptrs[count - 1];
98        count --;
99    }
100
[54]101    free(ptrs);
[49]102    free(memory);
103    return 0;
104}
105/* }}} */
[1135]106int main() /* {{{ */
[148]107{
108    int i;
109
[1135]110    xc_allocator_init();
[148]111
[1135]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);
[148]115    }
[1135]116    return 0;
[148]117}
118/* }}} */
[1135]119#endif
Note: See TracBrowser for help on using the repository browser.