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-04-10 20:54:02 -06:00
|
|
|
struct {
|
|
|
|
|
Arena *arena;
|
|
|
|
|
size_t offset;
|
|
|
|
|
};
|
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-04-10 20:54:02 -06:00
|
|
|
void *arena_unwrap_pointer(ArenaPointer p);
|
|
|
|
|
|
2026-04-11 11:30:04 -06:00
|
|
|
SizeResult arena_get_align_padding(Arena *arena, size_t alignment);
|
2026-04-11 21:15:31 -06:00
|
|
|
// can take big sizes but of sourse there is a limit,
|
|
|
|
|
// its protected against SIZE_MAX but anything that
|
|
|
|
|
// is big may break it, still, is an edge case that
|
|
|
|
|
// I shouldn't cover.
|
refactor(all): Fixed alloc and moved growing logic to another function
OK, so evrything was a fucknig mess, it still kinda is, AI helped
me to clean my own mess, it really sucks but the fucking debugger
and address sanitizer were not working, also, there were small
small errors so no big deal but still, a bit sad, also changed a
the way i cast from uint8_t to double and int in the tests, may
change that i guess, verything else i might say is good :)
2026-04-03 12:44:01 -06:00
|
|
|
ArenaErr arena_ensure_capacity(Arena *arena, size_t size, size_t alignment);
|
2026-04-11 21:15:31 -06:00
|
|
|
|
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
|
2026-04-11 21:15:31 -06:00
|
|
|
static inline bool mul_size_t_safe(size_t a, size_t b, size_t *out);
|
|
|
|
|
static inline bool add_size_t_safe(size_t a, size_t b, size_t *out);
|
|
|
|
|
static inline bool is_power_of_two(size_t x);
|
refactor(all): Fixed alloc and moved growing logic to another function
OK, so evrything was a fucknig mess, it still kinda is, AI helped
me to clean my own mess, it really sucks but the fucking debugger
and address sanitizer were not working, also, there were small
small errors so no big deal but still, a bit sad, also changed a
the way i cast from uint8_t to double and int in the tests, may
change that i guess, verything else i might say is good :)
2026-04-03 12:44:01 -06:00
|
|
|
|
2026-03-29 10:40:09 -06:00
|
|
|
#endif // !ARENA_H
|