Derive-C
Loading...
Searching...
No Matches
arena.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <derive-c/allocs/std.h>
#include <derive-c/structures/arena/template.h>
Include dependency graph for arena.c:

Go to the source code of this file.

Classes

struct  foo

Macros

#define INDEX_BITS   32
#define V   uint32_t
#define SELF   ints
#define INDEX_BITS   8
#define V   struct foo
#define V_DELETE   my_foo_delete
#define SELF   foo_arena

Functions

void int_example ()
void my_foo_delete (struct foo *self)
void foo_example ()
int main ()

Macro Definition Documentation

◆ INDEX_BITS [1/2]

#define INDEX_BITS   32

Definition at line 11 of file arena.c.

◆ INDEX_BITS [2/2]

#define INDEX_BITS   8

Definition at line 11 of file arena.c.

◆ SELF [1/2]

#define SELF   ints

Definition at line 13 of file arena.c.

◆ SELF [2/2]

#define SELF   foo_arena

Definition at line 13 of file arena.c.

◆ V [1/2]

#define V   uint32_t

Definition at line 12 of file arena.c.

◆ V [2/2]

#define V   struct foo

Definition at line 12 of file arena.c.

◆ V_DELETE

#define V_DELETE   my_foo_delete

Definition at line 55 of file arena.c.

Function Documentation

◆ foo_example()

void foo_example ( )
Examples
structures/arena.c.

Definition at line 59 of file arena.c.

59 {
60 foo_arena arena = foo_arena_new_with_capacity_for(12, stdalloc_get());
61 foo_arena_index index_a = foo_arena_insert(
62 &arena, (struct foo){.x = 42, .y = "A", .owned_data = (int*)malloc(sizeof(int))});
63 foo_arena_index index_b = foo_arena_insert(
64 &arena, (struct foo){.x = 41, .y = "B", .owned_data = (int*)malloc(sizeof(int))});
65
66 assert(foo_arena_size(&arena) == 2);
67 assert(foo_arena_full(&arena) == false);
68 assert(foo_arena_read(&arena, index_a)->x == 42);
69 assert(foo_arena_read(&arena, index_b)->x == 41);
70
71 foo_arena_write(&arena, index_b)->x = 100;
72 assert(foo_arena_read(&arena, index_b)->x == 100);
73
74 // we remove the entry, improtantly - we now own this data
75 struct foo entry_a = foo_arena_remove(&arena, index_a);
76
77 // entry_b was deleted
78 foo_arena_delete(&arena);
79
80 // entry a has not yet been deleted, so we can still access and then delete it
81 assert(entry_a.x == 42);
82
83 my_foo_delete(&entry_a);
84}
static void * malloc(SELF *self, size_t size)
Definition template.h:29
void my_foo_delete(struct foo *self)
Definition arena.c:51
Definition arena.c:45
int x
Definition arena.c:46
Here is the call graph for this function:
Here is the caller graph for this function:

◆ int_example()

void int_example ( )
Examples
structures/arena.c.

Definition at line 16 of file arena.c.

16 {
17 ints arena = ints_new_with_capacity_for(12, stdalloc_get());
18 ints_insert(&arena, 23);
19 ints_insert(&arena, 42);
20 ints_insert(&arena, 1000);
21 ints_insert(&arena, 1001);
22
23 assert(ints_size(&arena) == 4);
24 {
25 ints_iter_const print_ints = ints_get_iter_const(&arena);
26 ints_iv_const const* entry = NULL;
27 while ((entry = ints_iter_const_next(&print_ints))) {
28 printf("entry for %d at %d\n", *entry->value, entry->index.index);
29 }
30 }
31
32 {
33 ints_iter inc_ints = ints_get_iter(&arena);
34 ints_iv const* entry = NULL;
35 while ((entry = ints_iter_next(&inc_ints))) {
36 printf("incrementing for %d = %d + 1 at %d\n", *entry->value, *entry->value,
37 entry->index.index);
38 *entry->value += 1;
39 }
40 }
41
42 ints_delete(&arena);
43}
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 86 of file arena.c.

86 {
89}
void foo_example()
Definition arena.c:59
void int_example()
Definition arena.c:16
Here is the call graph for this function:

◆ my_foo_delete()

void my_foo_delete ( struct foo * self)
Examples
structures/arena.c.

Definition at line 51 of file arena.c.

51{ free(self->owned_data); }
static void free(SELF *self, void *ptr)
Definition template.h:62
int * owned_data
Definition arena.c:48
Here is the call graph for this function:
Here is the caller graph for this function: