source: trunk/xcache/xc_allocator.c @ 1378

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

use config.h correctly wherever needed

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