Derive-C
Loading...
Searching...
No Matches
container/arena.c

Examples for using arena containers.

Examples for using arena containers.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct example_data {
char* description;
int value;
};
static void example_data_delete(struct example_data* self) { free(self->description); }
static void example_data_debug(struct example_data const* self, dc_debug_fmt fmt, FILE* stream) {
fprintf(stream, "example_data@%p {\n", (void*)self);
dc_debug_fmt_print(fmt, stream, "description: %s,\n", self->description);
dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
}
#define INDEX_BITS 16
#define VALUE struct example_data
#define VALUE_DELETE example_data_delete
#define VALUE_DEBUG example_data_debug
#define NAME unstable_arena
static void example_unstable_arena() {
DC_SCOPED(unstable_arena) arena = unstable_arena_new_with_capacity_for(3, stdalloc_get_ref());
unstable_arena_insert(&arena,
(struct example_data){.description = strdup("First"), .value = 1});
unstable_arena_index_t idx2 = unstable_arena_insert(
&arena, (struct example_data){.description = strdup("Second"), .value = 2});
unstable_arena_insert(&arena,
(struct example_data){.description = strdup("Third"), .value = 3});
DC_FOR(unstable_arena, &arena, iter, entry) {
printf("Entry at index %u: %s = %d\n", entry.index.index, entry.value->description,
entry.value->value);
}
unstable_arena_debug(&arena, dc_debug_fmt_new(), stdout);
struct example_data removed = unstable_arena_remove(&arena, idx2);
}
#define NAME dbg
#define CAPACITY 512
#define ALLOC dbg
#define NAME hybrid
#define INDEX_BITS 16
#define VALUE struct example_data
#define VALUE_DELETE example_data_delete
#define VALUE_DEBUG example_data_debug
#define ALLOC hybrid
#define NAME custom_arena
DC_SCOPED(dbg) debug_alloc = dbg_new("custom_arena", stdout, stdalloc_get_ref());
hybrid_buffer buf;
DC_SCOPED(hybrid) hybrid_alloc = hybrid_new(&buf, &debug_alloc);
DC_SCOPED(custom_arena) arena = custom_arena_new_with_capacity_for(3, &hybrid_alloc);
custom_arena_insert(&arena, (struct example_data){.description = strdup("A"), .value = 1});
custom_arena_insert(&arena, (struct example_data){.description = strdup("B"), .value = 2});
custom_arena_insert(&arena, (struct example_data){.description = strdup("C"), .value = 3});
}
#define INDEX_BITS 32
#define VALUE struct example_data
#define VALUE_DELETE example_data_delete
#define VALUE_DEBUG example_data_debug
#define NAME geometric_arena
static size_t geometric_arena_index_hash(geometric_arena_index_t const* idx) {
return DC_DEFAULT_HASH(&idx->index);
}
static bool geometric_arena_index_eq(geometric_arena_index_t const* a,
geometric_arena_index_t const* b) {
return a->index == b->index;
}
#define KEY geometric_arena_index_t
#define KEY_HASH geometric_arena_index_hash
#define KEY_EQ geometric_arena_index_eq
#define VALUE struct example_data const*
#define NAME index_to_data_map
static void example_geometric_arena() {
DC_SCOPED(geometric_arena) arena = geometric_arena_new(stdalloc_get_ref());
geometric_arena_index_t idx1 = geometric_arena_insert(
&arena, (struct example_data){.description = strdup("Alpha"), .value = 10});
geometric_arena_index_t idx2 = geometric_arena_insert(
&arena, (struct example_data){.description = strdup("Beta"), .value = 20});
geometric_arena_index_t idx3 = geometric_arena_insert(
&arena, (struct example_data){.description = strdup("Gamma"), .value = 30});
DC_SCOPED(index_to_data_map) map = index_to_data_map_new(stdalloc_get_ref());
index_to_data_map_insert(&map, idx1, geometric_arena_read(&arena, idx1));
index_to_data_map_insert(&map, idx2, geometric_arena_read(&arena, idx2));
index_to_data_map_insert(&map, idx3, geometric_arena_read(&arena, idx3));
geometric_arena_debug(&arena, dc_debug_fmt_new(), stdout);
}
#define INDEX_BITS 8
#define BLOCK_INDEX_BITS 2
#define VALUE struct example_data
#define VALUE_DELETE example_data_delete
#define VALUE_DEBUG example_data_debug
#define NAME chunked_arena
static void example_chunked_arena() {
DC_STATIC_ASSERT(sizeof(chunked_arena_index_t) == sizeof(uint8_t), "Index size must be 8 bits");
DC_SCOPED(chunked_arena) arena = chunked_arena_new(stdalloc_get_ref());
chunked_arena_insert(&arena, (struct example_data){.description = strdup("X"), .value = 100});
chunked_arena_insert(&arena, (struct example_data){.description = strdup("Y"), .value = 200});
}
int main() {
return 0;
}
int main()
Definition alloc.c:109
static void example_geometric_arena()
Definition arena.c:108
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:92
static void example_chunked_arena()
Definition arena.c:135
static void example_unstable_arena()
Definition arena.c:35
static void example_custom_allocator_arena()
Definition arena.c:73
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:96
#define DC_DEFAULT_HASH(obj)
Definition default.h:15
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_new()
Definition fmt.h:15
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 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_DEBUG_TRACE
Definition debug.h:17
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108