This commit is contained in:
agra
2026-02-20 18:22:42 +02:00
parent 6f927361aa
commit 2f95810f9d
10 changed files with 510 additions and 77 deletions

View File

@@ -18,57 +18,37 @@ field_value_int :: ($T: Type, idx: s64) -> s64 #builtin;
field_index :: ($T: Type, val: T) -> s64 #builtin;
string :: []u8 #builtin;
// --- Arena allocator & Context ---
#import "allocators.sx";
Arena :: struct {
buf: string;
pos: s64;
}
// --- Context ---
Context :: struct {
arena: *Arena;
allocator: Allocator;
data: *void;
}
context : Context = ---;
arena_create :: (size: s64) -> Arena {
Arena.{ buf = cstring(size), pos = 0 };
}
arena_alloc :: (arena: *Arena, size: s64) -> *void {
aligned := (size + 7) & (0 - 8);
if arena.pos + aligned > arena.buf.len {
return malloc(aligned);
}
ptr : *void = xx @arena.buf[arena.pos];
arena.pos = arena.pos + aligned;
ptr;
}
arena_reset :: (arena: *Arena) {
arena.pos = 0;
}
arena_destroy :: (arena: *Arena) {
free(arena.buf.ptr);
}
// --- String allocation ---
CString :: union {
s: string;
struct { ptr: *void; len: s64; };
}
// --- Slice & string allocation ---
cstring :: (size: s64) -> string {
p : s64 = xx context.arena;
raw := if p != 0 then arena_alloc(context.arena, size + 1) else malloc(size + 1);
p : s64 = xx context.allocator.ctx;
raw := if p != 0 then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
memset(raw, 0, size + 1);
rs : CString = ---;
rs.ptr = raw;
rs.len = size;
rs.s;
s : string = ---;
s.ptr = xx raw;
s.len = size;
s;
}
alloc_slice :: ($T: Type, count: s64) -> []T {
p : s64 = xx context.allocator.ctx;
raw := if p != 0 then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
memset(raw, 0, count * size_of(T));
s : []T = ---;
s.ptr = xx raw;
s.len = count;
s;
}
int_to_string :: (n: s64) -> string {