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

@@ -1377,5 +1377,86 @@ END;
print("{}\n", "piped" |> concat(" ok") |> concat("!")); // piped ok!
}
// ── alloc_slice ──────────────────────────────────────────
{
items := alloc_slice(s64, 5);
items[0] = 10;
items[1] = 20;
items[2] = 30;
items[3] = 40;
items[4] = 50;
print("alloc len: {}\n", items.len); // alloc len: 5
print("alloc[0]: {}\n", items[0]); // alloc[0]: 10
print("alloc[4]: {}\n", items[4]); // alloc[4]: 50
// alloc_slice with u8
bytes := alloc_slice(u8, 3);
bytes[0] = 65;
bytes[1] = 66;
bytes[2] = 67;
print("bytes len: {}\n", bytes.len); // bytes len: 3
}
// ========================================================
// ALLOCATORS
// ========================================================
print("--- allocators ---\n");
// ── GPA ─────────────────────────────────────────────────
{
gpa_state : GPA = .{ alloc_count = 0 };
gpa := gpa_create(@gpa_state);
p1 := gpa.alloc(gpa.ctx, 64);
p2 := gpa.alloc(gpa.ctx, 128);
print("gpa allocs: {}\n", gpa_state.alloc_count); // gpa allocs: 2
gpa.free(gpa.ctx, p1);
gpa.free(gpa.ctx, p2);
print("gpa final: {}\n", gpa_state.alloc_count); // gpa final: 0
}
// ── Arena backed by GPA (multi-chunk) ───────────────────
{
gpa_state3 : GPA = .{ alloc_count = 0 };
gpa3 := gpa_create(@gpa_state3);
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);
print("arena chunks: {}\n", gpa_state3.alloc_count); // arena chunks: 1
// Overflow → new chunk
a3 := arena.alloc(arena.ctx, 16);
print("arena overflow: {}\n", gpa_state3.alloc_count); // arena overflow: 2
// Verify memory works across chunks
p1 : [*]u8 = xx a1;
p3 : [*]u8 = xx a3;
p1[0] = 42;
p3[0] = 99;
print("arena a1: {}\n", p1[0]); // arena a1: 42
print("arena a3: {}\n", p3[0]); // arena a3: 99
// Reset retains newest chunk
arena_reset(@arena_state);
print("arena reset idx: {}\n", arena_state.end_index); // arena reset idx: 0
print("arena reset gpa: {}\n", gpa_state3.alloc_count);// arena reset gpa: 1
// Deinit frees all
arena_deinit(@arena_state);
print("arena deinit: {}\n", gpa_state3.alloc_count); // arena deinit: 0
}
// ── BufAlloc from stack array ───────────────────────────
{
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);
print("buf pos: {}\n", buf_state.pos); // buf pos: 48
b3 := bufalloc.alloc(bufalloc.ctx, 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
}
print("=== DONE ===\n");
}