This commit is contained in:
alessandro bason 2025-06-24 17:47:08 +02:00
parent 95d74c2ef4
commit a66e58193f
25 changed files with 2600 additions and 93 deletions

37
arena.h
View file

@ -3,6 +3,10 @@
#include "core.h"
#if COLLA_DEBUG
#include "str.h"
#endif
#if COLLA_WIN && !COLLA_TCC
#define alignof __alignof
#endif
@ -21,16 +25,36 @@ typedef enum alloc_flags_e {
ALLOC_SOFT_FAIL = 1 << 1,
} alloc_flags_e;
#if COLLA_DEBUG
typedef struct alloc_header_t alloc_header_t;
struct alloc_header_t {
char type_name[16];
u32 size;
u32 count;
alloc_header_t *next;
};
void arena__print_crash(arena_t *arena);
#endif
typedef struct arena_t arena_t;
struct arena_t {
u8 *beg;
u8 *cur;
u8 *end;
arena_type_e type;
#if COLLA_DEBUG
strview_t file;
int line;
alloc_header_t *head;
#endif
};
typedef struct arena_desc_t arena_desc_t;
struct arena_desc_t {
#if COLLA_DEBUG
strview_t file;
int line;
#endif
arena_type_e type;
usize size;
u8 *static_buffer;
@ -38,6 +62,9 @@ struct arena_desc_t {
typedef struct arena_alloc_desc_t arena_alloc_desc_t;
struct arena_alloc_desc_t {
#if COLLA_DEBUG
strview_t type_name;
#endif
arena_t *arena;
usize count;
alloc_flags_e flags;
@ -46,10 +73,18 @@ struct arena_alloc_desc_t {
};
// arena_type_e type, usize allocation, [ byte *static_buffer ]
#if !COLLA_DEBUG
#define arena_make(...) arena_init(&(arena_desc_t){ __VA_ARGS__ })
#else
#define arena_make(...) arena_init(&(arena_desc_t){ strv(__FILE__), __LINE__, __VA_ARGS__ })
#endif
// arena_t *arena, T type, [ usize count, alloc_flags_e flags, usize align, usize size ]
#if !COLLA_DEBUG
#define alloc(arenaptr, type, ...) arena_alloc(&(arena_alloc_desc_t){ .size = sizeof(type), .count = 1, .align = alignof(type), .arena = arenaptr, __VA_ARGS__ })
#else
#define alloc(arenaptr, type, ...) arena_alloc(&(arena_alloc_desc_t){ .type_name = strv(#type), .size = sizeof(type), .count = 1, .align = alignof(type), .arena = arenaptr, __VA_ARGS__ })
#endif
// simple arena that always calls malloc internally, this is useful if you need
// malloc for some reason but want to still use the arena interface
@ -70,4 +105,4 @@ usize arena_capacity(arena_t *arena);
void arena_rewind(arena_t *arena, usize from_start);
void arena_pop(arena_t *arena, usize amount);
#endif
#endif