Derive-C
Loading...
Searching...
No Matches
arena.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <derive-c/macros/iterators.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 49 of file arena.c.

Function Documentation

◆ foo_example()

void foo_example ( )
Examples
structures/arena.c.

Definition at line 53 of file arena.c.

53 {
54 foo_arena arena = foo_arena_new_with_capacity_for(12);
55 foo_arena_index index_a = foo_arena_insert(
56 &arena, (struct foo){.x = 42, .y = "A", .owned_data = (int*)malloc(sizeof(int))});
57 foo_arena_index index_b = foo_arena_insert(
58 &arena, (struct foo){.x = 41, .y = "B", .owned_data = (int*)malloc(sizeof(int))});
59
60 assert(foo_arena_size(&arena) == 2);
61 assert(foo_arena_full(&arena) == false);
62 assert(foo_arena_read(&arena, index_a)->x == 42);
63 assert(foo_arena_read(&arena, index_b)->x == 41);
64
65 foo_arena_write(&arena, index_b)->x = 100;
66 assert(foo_arena_read(&arena, index_b)->x == 100);
67
68 // we remove the entry, improtantly - we now own this data
69 struct foo entry_a = foo_arena_remove(&arena, index_a);
70
71 // entry_b was deleted
72 foo_arena_delete(&arena);
73
74 // entry a has not yet been deleted, so we can still access and then delete it
75 assert(entry_a.x == 42);
76
77 my_foo_delete(&entry_a);
78}
void my_foo_delete(struct foo *self)
Definition arena.c:45
Definition arena.c:39
int x
Definition arena.c:40
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);
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 ITER_LOOP(ints_iter_const, print_ints, ints_iv_const, entry) {
27 printf("entry for %d at %d", *entry.value, entry.index.index);
28 }
29
30 ints_iter inc_ints = ints_get_iter(&arena);
31 ITER_LOOP(ints_iter, inc_ints, ints_iv, entry) {
32 printf("incrementing for %d = %d + 1 at %d", *entry.value, *entry.value, entry.index.index);
33 *entry.value += 1;
34 }
35
36 ints_delete(&arena);
37}
#define ITER_LOOP(ITER_TYPE, ITER_NAME, VALUE_TYPE, VALUE_NAME)
Definition iterators.h:2
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 80 of file arena.c.

80 {
83}
void foo_example()
Definition arena.c:53
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 45 of file arena.c.

45{ free(self->owned_data); }
int * owned_data
Definition arena.c:42
Here is the caller graph for this function: