1#define NONSTD_IMPLEMENTATION
2#include "../nonstd.h"
3
4#include <stdio.h>
5
6typedef struct {
7 int id;
8 char *name;
9} User;
10
11int main(void) {
12 // Initialize the arena
13 Arena a = arena_make();
14
15 // Allocate some structs
16 User *u1 = arena_alloc(&a, sizeof(User));
17 u1->id = 1;
18 // Use string builder for name, but we can't easily alloc string builder
19 // internal buffer on arena yet unless we make a custom allocator for it.
20 // For now, let's just use arena_alloc for a string.
21 char *name1 = arena_alloc(&a, 10);
22 strcpy(name1, "Alice");
23 u1->name = name1;
24
25 User *u2 = arena_alloc(&a, sizeof(User));
26 u2->id = 2;
27 char *name2 = arena_alloc(&a, 10);
28 strcpy(name2, "Bob");
29 u2->name = name2;
30
31 printf("User 1: %d, %s\n", u1->id, u1->name);
32 printf("User 2: %d, %s\n", u2->id, u2->name);
33
34 // Creating a linked list using arena
35 struct Node {
36 int val;
37 struct Node *next;
38 };
39
40 struct Node *head = NULL;
41 for (int i = 0; i < 5; i++) {
42 struct Node *node = arena_alloc(&a, sizeof(struct Node));
43 node->val = i;
44 node->next = head;
45 head = node;
46 }
47
48 printf("List: ");
49 for (struct Node *n = head; n; n = n->next) {
50 printf("%d ", n->val);
51 }
52 printf("\n");
53
54 // Free everything at once
55 arena_free(&a);
56 printf("Arena freed.\n");
57
58 return 0;
59}