Derive-C
Loading...
Searching...
No Matches
arena.c File Reference

Go to the source code of this file.

Data Structures

struct  example_data

Macros

#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
#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
#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
#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
#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

Functions

static void example_data_delete (struct example_data *self)
static void example_data_debug (struct example_data const *self, dc_debug_fmt fmt, FILE *stream)
static void example_unstable_arena ()
static void example_custom_allocator_arena ()
static size_t geometric_arena_index_hash (geometric_arena_index_t const *idx)
static bool geometric_arena_index_eq (geometric_arena_index_t const *a, geometric_arena_index_t const *b)
static void example_geometric_arena ()
static void example_chunked_arena ()
int main ()

Macro Definition Documentation

◆ ALLOC [1/2]

#define ALLOC   hybrid

Definition at line 61 of file arena.c.

◆ ALLOC [2/2]

#define ALLOC   dbg

Definition at line 61 of file arena.c.

◆ BLOCK_INDEX_BITS

#define BLOCK_INDEX_BITS   2

Definition at line 128 of file arena.c.

◆ CAPACITY

#define CAPACITY   512

Definition at line 60 of file arena.c.

◆ INDEX_BITS [1/4]

#define INDEX_BITS   8

Definition at line 28 of file arena.c.

◆ INDEX_BITS [2/4]

#define INDEX_BITS   32

Definition at line 28 of file arena.c.

◆ INDEX_BITS [3/4]

#define INDEX_BITS   16

Definition at line 28 of file arena.c.

◆ INDEX_BITS [4/4]

#define INDEX_BITS   16

Definition at line 28 of file arena.c.

◆ KEY

#define KEY   geometric_arena_index_t

Definition at line 101 of file arena.c.

◆ KEY_EQ

#define KEY_EQ   geometric_arena_index_eq

Definition at line 103 of file arena.c.

◆ KEY_HASH

#define KEY_HASH   geometric_arena_index_hash

Definition at line 102 of file arena.c.

◆ NAME [1/7]

#define NAME   chunked_arena

Definition at line 32 of file arena.c.

◆ NAME [2/7]

#define NAME   index_to_data_map

Definition at line 32 of file arena.c.

◆ NAME [3/7]

#define NAME   geometric_arena

Definition at line 32 of file arena.c.

◆ NAME [4/7]

#define NAME   custom_arena

Definition at line 32 of file arena.c.

◆ NAME [5/7]

#define NAME   hybrid

Definition at line 32 of file arena.c.

◆ NAME [6/7]

#define NAME   dbg

Definition at line 32 of file arena.c.

◆ NAME [7/7]

#define NAME   unstable_arena

Definition at line 32 of file arena.c.

◆ VALUE [1/5]

#define VALUE   struct example_data

Definition at line 29 of file arena.c.

◆ VALUE [2/5]

#define VALUE   struct example_data const*

Definition at line 29 of file arena.c.

◆ VALUE [3/5]

#define VALUE   struct example_data

Definition at line 29 of file arena.c.

◆ VALUE [4/5]

#define VALUE   struct example_data

Definition at line 29 of file arena.c.

◆ VALUE [5/5]

#define VALUE   struct example_data

Definition at line 29 of file arena.c.

◆ VALUE_DEBUG [1/4]

#define VALUE_DEBUG   example_data_debug

Definition at line 31 of file arena.c.

◆ VALUE_DEBUG [2/4]

#define VALUE_DEBUG   example_data_debug

Definition at line 31 of file arena.c.

◆ VALUE_DEBUG [3/4]

#define VALUE_DEBUG   example_data_debug

Definition at line 31 of file arena.c.

◆ VALUE_DEBUG [4/4]

#define VALUE_DEBUG   example_data_debug

Definition at line 31 of file arena.c.

◆ VALUE_DELETE [1/4]

#define VALUE_DELETE   example_data_delete

Definition at line 30 of file arena.c.

◆ VALUE_DELETE [2/4]

#define VALUE_DELETE   example_data_delete

Definition at line 30 of file arena.c.

◆ VALUE_DELETE [3/4]

#define VALUE_DELETE   example_data_delete

Definition at line 30 of file arena.c.

◆ VALUE_DELETE [4/4]

#define VALUE_DELETE   example_data_delete

Definition at line 30 of file arena.c.

Function Documentation

◆ example_chunked_arena()

void example_chunked_arena ( )
static
Examples
container/arena.c.

Definition at line 135 of file arena.c.

135 {
137 DC_STATIC_ASSERT(sizeof(chunked_arena_index_t) == sizeof(uint8_t), "Index size must be 8 bits");
138
139 DC_SCOPED(chunked_arena) arena = chunked_arena_new(stdalloc_get_ref());
140
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});
143}
#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
#define DC_DEBUG_TRACE
Definition debug.h:17

◆ example_custom_allocator_arena()

void example_custom_allocator_arena ( )
static
Examples
container/arena.c.

Definition at line 73 of file arena.c.

73 {
75 DC_SCOPED(dbg) debug_alloc = dbg_new("custom_arena", stdout, stdalloc_get_ref());
76 hybrid_buffer buf;
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);
79
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});
83}

◆ example_data_debug()

void example_data_debug ( struct example_data const * self,
dc_debug_fmt fmt,
FILE * stream )
static
Examples
container/arena.c, utils/option.c, and utils/result.c.

Definition at line 19 of file arena.c.

19 {
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}
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
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ example_data_delete()

void example_data_delete ( struct example_data * self)
static
Examples
container/arena.c, utils/option.c, and utils/result.c.

Definition at line 17 of file arena.c.

17{ free(self->description); }
char * description
Definition arena.c:13

◆ example_geometric_arena()

void example_geometric_arena ( )
static
Examples
container/arena.c.

Definition at line 108 of file arena.c.

108 {
110 DC_SCOPED(geometric_arena) arena = geometric_arena_new(stdalloc_get_ref());
111
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});
118
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));
123
124 geometric_arena_debug(&arena, dc_debug_fmt_new(), stdout);
125}
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15

◆ example_unstable_arena()

void example_unstable_arena ( )
static
Examples
container/arena.c.

Definition at line 35 of file arena.c.

35 {
37 DC_SCOPED(unstable_arena) arena = unstable_arena_new_with_capacity_for(3, stdalloc_get_ref());
38
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});
45
46 DC_FOR(unstable_arena, &arena, iter, entry) {
47 printf("Entry at index %u: %s = %d\n", entry.index.index, entry.value->description,
48 entry.value->value);
49 }
50
51 unstable_arena_debug(&arena, dc_debug_fmt_new(), stdout);
52
53 struct example_data removed = unstable_arena_remove(&arena, idx2);
54 example_data_delete(&removed);
55}
static void example_data_delete(struct example_data *self)
Definition arena.c:17
#define DC_FOR(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:13

◆ geometric_arena_index_eq()

bool geometric_arena_index_eq ( geometric_arena_index_t const * a,
geometric_arena_index_t const * b )
static
Examples
container/arena.c.

Definition at line 96 of file arena.c.

97 {
98 return a->index == b->index;
99}

◆ geometric_arena_index_hash()

size_t geometric_arena_index_hash ( geometric_arena_index_t const * idx)
static
Examples
container/arena.c.

Definition at line 92 of file arena.c.

92 {
93 return DC_DEFAULT_HASH(&idx->index);
94}
#define DC_DEFAULT_HASH(obj)
Definition default.h:15

◆ main()

int main ( )

Definition at line 145 of file arena.c.

145 {
150 return 0;
151}
static void example_geometric_arena()
Definition arena.c:108
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