1 #include "xbt/mallocator.h"
4 #include <stdio.h> /* snprintf */
6 typedef struct element {
9 typedef struct element *element_t;
11 element_t base_mallocator;
13 static void *element_mallocator_new_f(void)
15 element_t elem = xbt_new(s_element_t, 1);
20 static void element_mallocator_free_f(void *elem)
25 #define element_mallocator_reset_f ((void_f_pvoid_t)NULL)
27 static void pprint_elems(xbt_dynar_t elems) {
31 xbt_dynar_foreach(elems, iter, elem) {
32 printf(" (%d,%d)", elem->value, (int)(base_mallocator-elem)/(int)sizeof(int));
37 int main(int argc, char**argv)
39 xbt_mallocator_initialization_is_done(1);
41 xbt_mallocator_t mallocator =
42 xbt_mallocator_new(65536, element_mallocator_new_f, element_mallocator_free_f, element_mallocator_reset_f);
43 xbt_dynar_t elems = xbt_dynar_new(sizeof(element_t), NULL);
44 element_t elem = NULL;
45 base_mallocator = xbt_mallocator_get(mallocator);
47 for (i=0; i<=10; i++) {
48 elem = xbt_mallocator_get(mallocator);
50 xbt_dynar_push(elems, &elem);
54 for (i=0; i<=5; i++) {
55 xbt_dynar_pop(elems, &elem);
56 xbt_mallocator_release(mallocator, elem);
60 xbt_dynar_remove_at(elems, 2, &elem);
61 xbt_mallocator_release(mallocator, elem);
64 for (i=11; i<=15; i++) {
65 elem = xbt_mallocator_get(mallocator);
67 xbt_dynar_push(elems, &elem);