-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharena.h
More file actions
36 lines (26 loc) · 898 Bytes
/
arena.h
File metadata and controls
36 lines (26 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef ARENA_H
#define ARENA_H
#include <stddef.h>
#include <stdint.h>
// ==========================================
// MEMORY SAFETY: ARENA ALLOCATOR
// ==========================================
typedef struct {
uint8_t *buffer;
size_t length;
size_t capacity;
size_t high_water; // peak usage across all resets
} Arena;
// Initialize the arena with a fixed size
Arena arena_init(size_t size);
// Allocate memory from the arena. Panics on overflow (Memory Safe).
void *arena_alloc(Arena *a, size_t size);
// Reset arena to beginning without freeing buffer
void arena_reset(Arena *a);
// Save current position (returns offset that can be passed to arena_restore)
size_t arena_save(Arena *a);
// Restore arena to a previously saved position
void arena_restore(Arena *a, size_t saved);
// Free arena memory (prints high-watermark stats)
void arena_free(Arena *a);
#endif