20 fprintf(
stream,
"example_data@%p {\n", (
void*)self);
29#define VALUE struct example_data
30#define VALUE_DELETE example_data_delete
31#define VALUE_DEBUG example_data_debug
32#define NAME unstable_arena
37 DC_SCOPED(unstable_arena) arena = unstable_arena_new_with_capacity_for(3, stdalloc_get_ref());
39 unstable_arena_insert(&arena,
40 (
struct example_data){.description = strdup(
"First"), .value = 1});
41 unstable_arena_index_t idx2 = unstable_arena_insert(
42 &arena, (
struct example_data){.description = strdup(
"Second"), .value = 2});
43 unstable_arena_insert(&arena,
44 (
struct example_data){.description = strdup(
"Third"), .value = 3});
46 DC_FOR(unstable_arena, &arena, iter, entry) {
47 printf(
"Entry at index %u: %s = %d\n", entry.index.index, entry.value->description,
53 struct example_data removed = unstable_arena_remove(&arena, idx2);
66#define VALUE struct example_data
67#define VALUE_DELETE example_data_delete
68#define VALUE_DEBUG example_data_debug
70#define NAME custom_arena
75 DC_SCOPED(dbg) debug_alloc = dbg_new(
"custom_arena", stdout, stdalloc_get_ref());
77 DC_SCOPED(hybrid) hybrid_alloc = hybrid_new(&buf, &debug_alloc);
78 DC_SCOPED(custom_arena) arena = custom_arena_new_with_capacity_for(3, &hybrid_alloc);
80 custom_arena_insert(&arena, (
struct example_data){.description = strdup(
"A"), .value = 1});
81 custom_arena_insert(&arena, (
struct example_data){.description = strdup(
"B"), .value = 2});
82 custom_arena_insert(&arena, (
struct example_data){.description = strdup(
"C"), .value = 3});
86#define VALUE struct example_data
87#define VALUE_DELETE example_data_delete
88#define VALUE_DEBUG example_data_debug
89#define NAME geometric_arena
97 geometric_arena_index_t
const* b) {
98 return a->index == b->index;
101#define KEY geometric_arena_index_t
102#define KEY_HASH geometric_arena_index_hash
103#define KEY_EQ geometric_arena_index_eq
104#define VALUE struct example_data const*
105#define NAME index_to_data_map
110 DC_SCOPED(geometric_arena) arena = geometric_arena_new(stdalloc_get_ref());
112 geometric_arena_index_t idx1 = geometric_arena_insert(
113 &arena, (
struct example_data){.description = strdup(
"Alpha"), .value = 10});
114 geometric_arena_index_t idx2 = geometric_arena_insert(
115 &arena, (
struct example_data){.description = strdup(
"Beta"), .value = 20});
116 geometric_arena_index_t idx3 = geometric_arena_insert(
117 &arena, (
struct example_data){.description = strdup(
"Gamma"), .value = 30});
119 DC_SCOPED(index_to_data_map) map = index_to_data_map_new(stdalloc_get_ref());
120 index_to_data_map_insert(&map, idx1, geometric_arena_read(&arena, idx1));
121 index_to_data_map_insert(&map, idx2, geometric_arena_read(&arena, idx2));
122 index_to_data_map_insert(&map, idx3, geometric_arena_read(&arena, idx3));
128#define BLOCK_INDEX_BITS 2
129#define VALUE struct example_data
130#define VALUE_DELETE example_data_delete
131#define VALUE_DEBUG example_data_debug
132#define NAME chunked_arena
137 DC_STATIC_ASSERT(
sizeof(chunked_arena_index_t) ==
sizeof(uint8_t),
"Index size must be 8 bits");
139 DC_SCOPED(chunked_arena) arena = chunked_arena_new(stdalloc_get_ref());
141 chunked_arena_insert(&arena, (
struct example_data){.description = strdup(
"X"), .value = 100});
142 chunked_arena_insert(&arena, (
struct example_data){.description = strdup(
"Y"), .value = 200});
static void example_geometric_arena()
static void example_data_debug(struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
static size_t geometric_arena_index_hash(geometric_arena_index_t const *idx)
static void example_chunked_arena()
static void example_unstable_arena()
static void example_custom_allocator_arena()
static void example_data_delete(struct example_data *self)
static bool geometric_arena_index_eq(geometric_arena_index_t const *a, geometric_arena_index_t const *b)
#define DC_DEFAULT_HASH(obj)
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
#define DC_FOR(TYPE, INSTANCE, ITER, ITEM)
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Debug format helpers for debug printin data structures.
static DC_PUBLIC FILE * stream(SELF *self)