diff options
| author | nasr <nsrddyn@gmail.com> | 2026-01-28 13:13:40 +0100 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-01-28 13:13:40 +0100 |
| commit | 3913d1778318cd0c6bfb871148d38abb33ec7fd3 (patch) | |
| tree | 917728adbf32c877ad591ad9d42f727cc7540b9b /library/arena.h | |
| parent | 7dead79e05e03a71a502ca4e75d05d126ff9f25c (diff) | |
checkpoint
Diffstat (limited to 'library/arena.h')
| -rw-r--r-- | library/arena.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/library/arena.h b/library/arena.h new file mode 100644 index 0000000..330023e --- /dev/null +++ b/library/arena.h | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #ifndef ARENA_H | ||
| 2 | #define ARENA_H | ||
| 3 | |||
| 4 | #include "base.h" | ||
| 5 | |||
| 6 | /** | ||
| 7 | * Arena Helper macro's | ||
| 8 | * */ | ||
| 9 | |||
| 10 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 11 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
| 12 | #define ALIGN_UP_POW2(n, p) (((u64)(n) + ((u64)(p) - 1)) & (~((u64)(p) - 1))) | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | /* | ||
| 17 | * Represents a disk partition with major/minor device numbers and block count. | ||
| 18 | */ | ||
| 19 | |||
| 20 | /** | ||
| 21 | * replacing malloc/free with arena allocaters | ||
| 22 | * | ||
| 23 | * */ | ||
| 24 | |||
| 25 | #define ARENA_BASE_POS (sizeof(mem_arena)) | ||
| 26 | // void * for the size of a pointer on the machine, 64/32bit comp | ||
| 27 | #define ARENA_ALIGN (sizeof(void *)) | ||
| 28 | |||
| 29 | |||
| 30 | static inline u64 KiB(u64 n) { return n << 10; } | ||
| 31 | static inline u64 MiB(u64 n) { return n << 20; } | ||
| 32 | static inline u64 GiB(u64 n) { return n << 30; } | ||
| 33 | |||
| 34 | typedef struct mem_arena mem_arena; | ||
| 35 | |||
| 36 | |||
| 37 | struct mem_arena | ||
| 38 | { | ||
| 39 | u64 capacity; | ||
| 40 | u64 pos; | ||
| 41 | } ; | ||
| 42 | |||
| 43 | // arena prototypes | ||
| 44 | mem_arena * | ||
| 45 | arena_create(u64 capacity); | ||
| 46 | // make it a void pointer to allow implicit conversion | ||
| 47 | void | ||
| 48 | arena_destroy(mem_arena *arena); | ||
| 49 | |||
| 50 | void * | ||
| 51 | arena_push(mem_arena *arena, u64 size, b32 non_zero); | ||
| 52 | |||
| 53 | void | ||
| 54 | arena_pop(mem_arena *arena, u64 size); | ||
| 55 | |||
| 56 | void | ||
| 57 | arena_pop_to(mem_arena *arena, u64 pos); | ||
| 58 | |||
| 59 | void | ||
| 60 | arena_clear(mem_arena *arena); | ||
| 61 | |||
| 62 | |||
| 63 | #define PUSH_STRUCT(arena, T) (T *)arena_push((arena), sizeof(T), 0) | ||
| 64 | #define PUSH_STRUCT_NZ(arena, T) (T *)arena_push((arena), sizeof(T), 1) | ||
| 65 | #define PUSH_ARRAY(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 0) | ||
| 66 | #define PUSH_ARRAY_NZ(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 1) | ||
| 67 | |||
| 68 | |||
| 69 | #endif | ||
