Derive-C
Loading...
Searching...
No Matches
arena.c
Go to the documentation of this file.
1
4
7#include <derive-c/prelude.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
14 int value;
15};
16
17static void example_data_delete(struct example_data* self) { free(self->description); }
18
19static void example_data_debug(struct example_data const* self, dc_debug_fmt fmt, FILE* stream) {
20 fprintf(stream, "example_data@%p {\n", (void*)self);
21 fmt = dc_debug_fmt_scope_begin(fmt);
22 dc_debug_fmt_print(fmt, stream, "description: %s,\n", self->description);
23 dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
24 fmt = dc_debug_fmt_scope_end(fmt);
25 dc_debug_fmt_print(fmt, stream, "}");
26}
27
28#define INDEX_BITS 16
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
34
35static void example_unstable_arena(DC_LOGGER* parent) {
36 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
37 DC_SCOPED(unstable_arena) arena = unstable_arena_new_with_capacity_for(3, stdalloc_get_ref());
38
39 DC_LOG(log, DC_INFO, "inserting three items");
40 unstable_arena_insert(&arena,
41 (struct example_data){.description = strdup("First"), .value = 1});
42 unstable_arena_index_t idx2 = unstable_arena_insert(
43 &arena, (struct example_data){.description = strdup("Second"), .value = 2});
44 unstable_arena_insert(&arena,
45 (struct example_data){.description = strdup("Third"), .value = 3});
46
47 DC_FOR(unstable_arena, &arena, iter, entry) {
48 DC_LOG(log, DC_INFO, "entry at index %u: %s = %d", entry.index.index,
49 entry.value->description, entry.value->value);
50 }
51
52 DC_LOG(log, DC_INFO, "arena: %s", DC_DEBUG(unstable_arena_debug, &arena));
53
54 DC_LOG(log, DC_INFO, "removing second item");
55 struct example_data removed = unstable_arena_remove(&arena, idx2);
56 example_data_delete(&removed);
57}
58
59#define NAME dbg
61
62#define CAPACITY 512
63#define ALLOC dbg
64#define NAME hybrid
66
67#define INDEX_BITS 16
68#define VALUE struct example_data
69#define VALUE_DELETE example_data_delete
70#define VALUE_DEBUG example_data_debug
71#define ALLOC hybrid
72#define NAME custom_arena
74
76 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
77 DC_SCOPED(dbg) debug_alloc = dbg_new("custom_arena", stdout, stdalloc_get_ref());
78 hybrid_buffer buf;
79 DC_SCOPED(hybrid) hybrid_alloc = hybrid_new(&buf, &debug_alloc);
80 DC_SCOPED(custom_arena) arena = custom_arena_new_with_capacity_for(3, &hybrid_alloc);
81
82 DC_LOG(log, DC_INFO, "inserting items A, B, C");
83 custom_arena_insert(&arena, (struct example_data){.description = strdup("A"), .value = 1});
84 custom_arena_insert(&arena, (struct example_data){.description = strdup("B"), .value = 2});
85 custom_arena_insert(&arena, (struct example_data){.description = strdup("C"), .value = 3});
86}
87
88#define INDEX_BITS 32
89#define VALUE struct example_data
90#define VALUE_DELETE example_data_delete
91#define VALUE_DEBUG example_data_debug
92#define NAME geometric_arena
94
95static size_t geometric_arena_index_hash(geometric_arena_index_t const* idx) {
96 return DC_DEFAULT_HASH(&idx->index);
97}
98
99static bool geometric_arena_index_eq(geometric_arena_index_t const* a,
100 geometric_arena_index_t const* b) {
101 return a->index == b->index;
102}
103
104#define KEY geometric_arena_index_t
105#define KEY_HASH geometric_arena_index_hash
106#define KEY_EQ geometric_arena_index_eq
107#define VALUE struct example_data const*
108#define NAME index_to_data_map
110
111static void example_geometric_arena(DC_LOGGER* parent) {
112 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
113 DC_SCOPED(geometric_arena) arena = geometric_arena_new(stdalloc_get_ref());
114
115 DC_LOG(log, DC_INFO, "inserting Alpha, Beta, Gamma");
116 geometric_arena_index_t idx1 = geometric_arena_insert(
117 &arena, (struct example_data){.description = strdup("Alpha"), .value = 10});
118 geometric_arena_index_t idx2 = geometric_arena_insert(
119 &arena, (struct example_data){.description = strdup("Beta"), .value = 20});
120 geometric_arena_index_t idx3 = geometric_arena_insert(
121 &arena, (struct example_data){.description = strdup("Gamma"), .value = 30});
122
123 DC_SCOPED(index_to_data_map) map = index_to_data_map_new(stdalloc_get_ref());
124 index_to_data_map_insert(&map, idx1, geometric_arena_read(&arena, idx1));
125 index_to_data_map_insert(&map, idx2, geometric_arena_read(&arena, idx2));
126 index_to_data_map_insert(&map, idx3, geometric_arena_read(&arena, idx3));
127
128 DC_LOG(log, DC_INFO, "arena: %s", DC_DEBUG(geometric_arena_debug, &arena));
129}
130
131#define INDEX_BITS 8
132#define BLOCK_INDEX_BITS 2
133#define VALUE struct example_data
134#define VALUE_DELETE example_data_delete
135#define VALUE_DEBUG example_data_debug
136#define NAME chunked_arena
138
139static void example_chunked_arena(DC_LOGGER* parent) {
140 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
141 DC_STATIC_ASSERT(sizeof(chunked_arena_index_t) == sizeof(uint8_t), "Index size must be 8 bits");
142
143 DC_SCOPED(chunked_arena) arena = chunked_arena_new(stdalloc_get_ref());
144
145 DC_LOG(log, DC_INFO, "inserting X and Y");
146 chunked_arena_insert(&arena, (struct example_data){.description = strdup("X"), .value = 100});
147 chunked_arena_insert(&arena, (struct example_data){.description = strdup("Y"), .value = 200});
148}
149
150int main() {
152 root = NS(DC_LOGGER,
153 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
154 (dc_log_id){"arena"});
155
160 return 0;
161}
static void example_data_debug(struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition arena.c:19
static size_t geometric_arena_index_hash(geometric_arena_index_t const *idx)
Definition arena.c:95
static void example_custom_allocator_arena(DC_LOGGER *parent)
Definition arena.c:75
static void example_data_delete(struct example_data *self)
Definition arena.c:17
static bool geometric_arena_index_eq(geometric_arena_index_t const *a, geometric_arena_index_t const *b)
Definition arena.c:99
static void example_geometric_arena(DC_LOGGER *parent)
Definition arena.c:111
static void example_unstable_arena(DC_LOGGER *parent)
Definition arena.c:35
static void example_chunked_arena(DC_LOGGER *parent)
Definition arena.c:139
int main()
Definition arena.c:150
#define DC_DEFAULT_HASH(obj)
Definition default.h:15
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_FOR(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:13
#define NS(pre, post)
Definition namespace.h:14
#define DC_STATIC_ASSERT
Definition panic.h:22
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
Debug format helpers for debug printin data structures.
Definition fmt.h:11
int value
Definition arena.c:14
char * description
Definition arena.c:13
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108