2026-03-29 10:40:09 -06:00
|
|
|
#ifndef ARENA_H
|
|
|
|
|
#define ARENA_H
|
|
|
|
|
|
2026-03-29 17:30:01 -06:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2026-03-29 19:06:49 -06:00
|
|
|
uint8_t *buffer;
|
2026-03-30 11:26:05 -06:00
|
|
|
size_t capacity;
|
2026-03-29 17:30:01 -06:00
|
|
|
size_t offset;
|
|
|
|
|
} Arena;
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
2026-03-29 19:06:49 -06:00
|
|
|
ARENA_OK = 0,
|
2026-03-29 17:30:01 -06:00
|
|
|
ARENA_BAD_ALLOC,
|
|
|
|
|
ARENA_OUT_OF_SPACE,
|
|
|
|
|
ARENA_NULL_ARG,
|
2026-03-29 17:53:10 -06:00
|
|
|
ARENA_INVALID_SIZE,
|
2026-03-29 21:08:34 -06:00
|
|
|
ARENA_INVALID_ALIGN,
|
2026-03-30 11:26:05 -06:00
|
|
|
ARENA_CAPACITY_OVERFLOW,
|
2026-03-29 17:30:01 -06:00
|
|
|
} ArenaErr;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
bool is_valid;
|
|
|
|
|
union {
|
|
|
|
|
ArenaErr err;
|
2026-03-29 19:06:49 -06:00
|
|
|
uint8_t *address;
|
2026-03-29 17:30:01 -06:00
|
|
|
};
|
|
|
|
|
} ArenaPointer;
|
|
|
|
|
|
2026-03-29 17:53:10 -06:00
|
|
|
typedef struct {
|
|
|
|
|
bool is_valid;
|
|
|
|
|
union {
|
|
|
|
|
ArenaErr err;
|
|
|
|
|
Arena arena;
|
|
|
|
|
};
|
|
|
|
|
} ArenaResult;
|
|
|
|
|
|
2026-03-29 20:59:32 -06:00
|
|
|
typedef struct {
|
|
|
|
|
bool is_valid;
|
|
|
|
|
union {
|
|
|
|
|
ArenaErr err;
|
|
|
|
|
size_t val;
|
|
|
|
|
};
|
|
|
|
|
} SizeResult;
|
|
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
ArenaResult arena_init(size_t capacity);
|
2026-03-29 17:30:01 -06:00
|
|
|
void arena_destroy(Arena *arena);
|
|
|
|
|
|
|
|
|
|
ArenaPointer arena_alloc(Arena *arena, size_t size, size_t alignment);
|
test and rework: Added push test and cahnged arena_push return
So added a little test that just adds 3 ints to an arena, nothing much
but the basics work, also changed arena_push because it only pushed and
didn't returned a ponter (wich is fucking useless) so also fixed that,
everything seems fine now, need more tests.
# Tipos:
# feat, fix, refactor, docs, style, test, chore
2026-03-31 19:15:37 -06:00
|
|
|
ArenaPointer arena_push(Arena *arena, void *data, size_t size, size_t alignment);
|
2026-03-30 11:26:05 -06:00
|
|
|
ArenaErr arena_realloc(Arena *arena, size_t new_capacity);
|
2026-03-29 17:30:01 -06:00
|
|
|
|
2026-03-29 20:59:32 -06:00
|
|
|
SizeResult align_arena_offset(Arena *arena, size_t alignment);
|
2026-03-30 11:26:05 -06:00
|
|
|
|
|
|
|
|
// Should be moved to something like general utilities,
|
|
|
|
|
// i should make one for all my c projects
|
|
|
|
|
bool mul_size_t_safe(size_t a, size_t b, size_t *out);
|
2026-03-29 17:30:01 -06:00
|
|
|
|
2026-03-29 10:40:09 -06:00
|
|
|
#endif // !ARENA_H
|