This commit is contained in:
agra
2026-02-20 23:18:17 +02:00
parent efa60fa670
commit be99b26c1d
5 changed files with 127 additions and 27 deletions

View File

@@ -1414,11 +1414,11 @@ END;
{
gpa_state : GPA = .{ alloc_count = 0 };
gpa := gpa_create(@gpa_state);
p1 := gpa.alloc(gpa.ctx, 64);
p2 := gpa.alloc(gpa.ctx, 128);
p1 := gpa.alloc(64);
p2 := gpa.alloc(128);
print("gpa allocs: {}\n", gpa_state.alloc_count); // gpa allocs: 2
gpa.free(gpa.ctx, p1);
gpa.free(gpa.ctx, p2);
gpa.dealloc(p1);
gpa.dealloc(p2);
print("gpa final: {}\n", gpa_state.alloc_count); // gpa final: 0
}
@@ -1429,11 +1429,11 @@ END;
arena_state : Arena = ---;
arena := arena_create(@arena_state, gpa3, 32);
// First chunk fits 80 usable bytes
a1 := arena.alloc(arena.ctx, 40);
a2 := arena.alloc(arena.ctx, 40);
a1 := arena.alloc(40);
a2 := arena.alloc(40);
print("arena chunks: {}\n", gpa_state3.alloc_count); // arena chunks: 1
// Overflow → new chunk
a3 := arena.alloc(arena.ctx, 16);
a3 := arena.alloc(16);
print("arena overflow: {}\n", gpa_state3.alloc_count); // arena overflow: 2
// Verify memory works across chunks
p1 : [*]u8 = xx a1;
@@ -1456,15 +1456,29 @@ END;
stack_buf : [128]u8 = ---;
buf_state : BufAlloc = ---;
bufalloc := buf_create(@buf_state, @stack_buf[0], 128);
b1 := bufalloc.alloc(bufalloc.ctx, 24);
b2 := bufalloc.alloc(bufalloc.ctx, 24);
b1 := bufalloc.alloc(24);
b2 := bufalloc.alloc(24);
print("buf pos: {}\n", buf_state.pos); // buf pos: 48
b3 := bufalloc.alloc(bufalloc.ctx, 200);
b3 := bufalloc.alloc(200);
b3_i : s64 = xx b3;
print("buf overflow: {}\n", b3_i); // buf overflow: 0
buf_reset(@buf_state);
print("buf reset: {}\n", buf_state.pos); // buf reset: 0
}
{
if 1 == (1,) {
print("1 == (1)\n");
}
if (1,) == (1) {
print("(1) == 1\n");
}
if (1,) == 1 {
print("1 == 1\n");
}
}
print("=== DONE ===\n");
}

View File

@@ -4,10 +4,21 @@
Allocator :: struct {
ctx: *void;
alloc: (*void, s64) -> *void;
free: (*void, *void) -> void;
alloc_fn: (*void, s64) -> *void;
free_fn: (*void, *void) -> void;
}
allocator_alloc :: (a: Allocator, size: s64) -> *void {
a.alloc_fn(a.ctx, size);
}
allocator_dealloc :: (a: Allocator, ptr: *void) {
a.free_fn(a.ctx, ptr);
}
alloc :: ufcs allocator_alloc;
dealloc :: ufcs allocator_dealloc;
// --- GPA: general purpose allocator (malloc/free wrapper) ---
GPA :: struct {
@@ -27,7 +38,7 @@ gpa_free :: (ctx: *void, ptr: *void) {
}
gpa_create :: (gpa: *GPA) -> Allocator {
Allocator.{ ctx = gpa, alloc = gpa_alloc, free = gpa_free };
Allocator.{ ctx = gpa, alloc_fn = gpa_alloc, free_fn = gpa_free };
}
// --- Arena: multi-chunk bump allocator (Zig-inspired) ---
@@ -47,7 +58,7 @@ arena_add_chunk :: (a: *Arena, min_size: s64) {
prev_cap := if a.first != null then a.first.cap else 0;
needed := min_size + 16 + 16;
len := (prev_cap + needed) * 3 / 2;
raw := a.parent.alloc(a.parent.ctx, len);
raw := a.parent.alloc(len);
chunk : *ArenaChunk = xx raw;
chunk.next = a.first;
chunk.cap = len;
@@ -82,7 +93,7 @@ arena_create :: (a: *Arena, parent: Allocator, size: s64) -> Allocator {
a.end_index = 0;
a.parent = parent;
arena_add_chunk(a, size);
Allocator.{ ctx = a, alloc = arena_alloc, free = arena_free };
Allocator.{ ctx = a, alloc_fn = arena_alloc, free_fn = arena_free };
}
arena_reset :: (a: *Arena) {
@@ -91,7 +102,7 @@ arena_reset :: (a: *Arena) {
it := a.first.next;
while it != null {
next := it.next;
a.parent.free(a.parent.ctx, it);
a.parent.dealloc(it);
it = next;
}
a.first.next = null;
@@ -103,7 +114,7 @@ arena_deinit :: (a: *Arena) {
it := a.first;
while it != null {
next := it.next;
a.parent.free(a.parent.ctx, it);
a.parent.dealloc(it);
it = next;
}
a.first = null;
@@ -136,7 +147,7 @@ buf_create :: (b: *BufAlloc, buf: [*]u8, len: s64) -> Allocator {
b.buf = buf;
b.len = len;
b.pos = 0;
Allocator.{ ctx = b, alloc = buf_alloc, free = buf_free };
Allocator.{ ctx = b, alloc_fn = buf_alloc, free_fn = buf_free };
}
buf_reset :: (b: *BufAlloc) {

View File

@@ -31,8 +31,12 @@ context : Context = ---;
// --- Slice & string allocation ---
context_alloc :: (size: s64) -> *void {
if context.allocator.ctx != null then context.allocator.alloc(size) else malloc(size);
}
cstring :: (size: s64) -> string {
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, size + 1) else malloc(size + 1);
raw := context_alloc(size + 1);
memset(raw, 0, size + 1);
s : string = ---;
s.ptr = xx raw;
@@ -41,7 +45,7 @@ cstring :: (size: s64) -> string {
}
alloc_slice :: ($T: Type, count: s64) -> []T {
raw := if context.allocator.ctx != null then context.allocator.alloc(context.allocator.ctx, count * size_of(T)) else malloc(count * size_of(T));
raw := context_alloc(count * size_of(T));
memset(raw, 0, count * size_of(T));
s : []T = ---;
s.ptr = xx raw;