2026-03-29 10:40:09 -06:00
|
|
|
#include "arena.h"
|
2026-03-29 17:53:10 -06:00
|
|
|
#include <stdbool.h>
|
2026-03-30 11:26:05 -06:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
2026-03-29 17:53:10 -06:00
|
|
|
#include <stdlib.h>
|
2026-03-29 18:35:13 -06:00
|
|
|
#include <string.h>
|
2026-03-29 17:53:10 -06:00
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
ArenaResult arena_init(size_t capacity) {
|
2026-03-29 18:00:57 -06:00
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
void *buffer = malloc(capacity);
|
2026-03-29 17:53:10 -06:00
|
|
|
if (buffer == NULL) {
|
|
|
|
|
ArenaResult err = {
|
|
|
|
|
.is_valid = false,
|
2026-03-29 18:00:57 -06:00
|
|
|
.err = ARENA_BAD_ALLOC,
|
2026-03-29 17:53:10 -06:00
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
if (capacity < 1) {
|
2026-03-29 17:53:10 -06:00
|
|
|
ArenaResult err = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_INVALID_SIZE,
|
|
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Arena new_arena = {
|
2026-03-29 19:06:49 -06:00
|
|
|
.buffer = buffer,
|
2026-03-30 11:26:05 -06:00
|
|
|
.capacity = capacity,
|
2026-03-29 17:53:10 -06:00
|
|
|
.offset = 0,
|
|
|
|
|
};
|
|
|
|
|
ArenaResult val = {
|
|
|
|
|
.is_valid = true,
|
|
|
|
|
.arena = new_arena,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2026-03-29 18:00:57 -06:00
|
|
|
|
|
|
|
|
void arena_destroy(Arena *arena) {
|
|
|
|
|
if (arena == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arena->offset = 0;
|
2026-03-30 11:26:05 -06:00
|
|
|
arena->capacity = 0;
|
2026-03-29 19:06:49 -06:00
|
|
|
free(arena->buffer);
|
2026-03-29 18:00:57 -06:00
|
|
|
}
|
2026-03-29 18:35:13 -06:00
|
|
|
|
|
|
|
|
ArenaPointer arena_alloc(Arena *arena, size_t size, size_t alignment) {
|
|
|
|
|
if (arena == NULL) {
|
|
|
|
|
ArenaPointer err = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_NULL_ARG,
|
|
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 20:59:32 -06:00
|
|
|
SizeResult new_offset = align_arena_offset(arena, alignment);
|
|
|
|
|
if (!new_offset.is_valid) {
|
|
|
|
|
ArenaPointer err = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = new_offset.err,
|
|
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
if (new_offset.val + size >= arena->capacity) {
|
2026-03-29 18:35:13 -06:00
|
|
|
ArenaPointer err = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_OUT_OF_SPACE,
|
|
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 20:59:32 -06:00
|
|
|
arena->offset = new_offset.val;
|
2026-03-29 18:35:13 -06:00
|
|
|
|
|
|
|
|
ArenaPointer val = {
|
|
|
|
|
.is_valid = true,
|
2026-03-29 19:06:49 -06:00
|
|
|
.address = arena->buffer + arena->offset,
|
2026-03-29 18:35:13 -06:00
|
|
|
};
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2026-03-29 19:06:49 -06:00
|
|
|
|
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-29 19:06:49 -06:00
|
|
|
if (arena == NULL || data == NULL) {
|
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 bad_pointer = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_NULL_ARG,
|
|
|
|
|
};
|
|
|
|
|
return bad_pointer;
|
2026-03-29 19:06:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArenaPointer pointer = arena_alloc(arena, size, alignment);
|
|
|
|
|
|
|
|
|
|
if (!pointer.is_valid) {
|
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
|
|
|
return pointer;
|
2026-03-30 11:26:05 -06:00
|
|
|
}
|
|
|
|
|
while (!pointer.is_valid && pointer.err == ARENA_OUT_OF_SPACE) {
|
|
|
|
|
size_t new_capacity;
|
|
|
|
|
|
|
|
|
|
bool is_overflow = mul_size_t_safe(arena->capacity, 2, &new_capacity);
|
|
|
|
|
if (is_overflow) {
|
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 bad_pointer = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_CAPACITY_OVERFLOW,
|
|
|
|
|
};
|
|
|
|
|
return bad_pointer;
|
2026-03-29 20:59:32 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
ArenaErr err = arena_realloc(arena, new_capacity);
|
2026-03-29 19:06:49 -06:00
|
|
|
if (err != ARENA_OK) {
|
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 bad_pointer = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = err,
|
|
|
|
|
};
|
|
|
|
|
return bad_pointer;
|
2026-03-29 19:06:49 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 20:59:32 -06:00
|
|
|
pointer = arena_alloc(arena, size, alignment);
|
|
|
|
|
}
|
2026-03-29 19:06:49 -06:00
|
|
|
|
|
|
|
|
memcpy(
|
|
|
|
|
pointer.address,
|
|
|
|
|
data,
|
|
|
|
|
size);
|
|
|
|
|
|
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
|
|
|
return pointer;
|
2026-03-29 19:06:49 -06:00
|
|
|
}
|
|
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
ArenaErr arena_realloc(Arena *arena, size_t new_capacity) {
|
2026-03-29 19:06:49 -06:00
|
|
|
if (arena == NULL) {
|
|
|
|
|
return ARENA_NULL_ARG;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
uint8_t *tmp = realloc(arena->buffer, new_capacity);
|
2026-03-29 19:06:49 -06:00
|
|
|
if (tmp == NULL) {
|
|
|
|
|
return ARENA_BAD_ALLOC;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arena->buffer = tmp;
|
2026-03-30 11:26:05 -06:00
|
|
|
arena->capacity = new_capacity;
|
2026-03-29 19:06:49 -06:00
|
|
|
return ARENA_OK;
|
|
|
|
|
}
|
2026-03-29 20:59:32 -06:00
|
|
|
|
2026-03-29 21:08:34 -06:00
|
|
|
SizeResult align_arena_offset(Arena *arena, size_t alignment) {
|
|
|
|
|
if (arena == NULL) {
|
|
|
|
|
SizeResult err = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_NULL_ARG,
|
|
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (alignment < 1) {
|
|
|
|
|
SizeResult err = {
|
|
|
|
|
.is_valid = false,
|
|
|
|
|
.err = ARENA_INVALID_ALIGN,
|
|
|
|
|
};
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SizeResult val = {
|
|
|
|
|
.is_valid = true,
|
|
|
|
|
.val = ((arena->offset + (alignment - 1)) & ~(alignment - 1)),
|
|
|
|
|
// Formula for alignment, found at stackoverflow, i came up with one
|
|
|
|
|
// but it involved 3 divisions, this one is way faster :)
|
|
|
|
|
};
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2026-03-29 20:59:32 -06:00
|
|
|
|
2026-03-30 11:26:05 -06:00
|
|
|
bool mul_size_t_safe(size_t a, size_t b, size_t *out) {
|
|
|
|
|
if (out == NULL) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (a != 0 && b > SIZE_MAX / a) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*out = a * b;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|