Files
sx/examples/0838-memory-helpers.sx
agra d8076b9333 lang: rename signed integer types sN -> iN
Surface rename of the signed integer family: s1..s64 become i1..i64
(u1..u64, usize, isize unchanged). 'string' keeps the s-prefix arm in
name classification; width parsing moves to the i-prefix arm next to
isize.

Internal TypeId tags follow the surface (.s8/.s16/.s32/.s64 ->
.i8/.i16/.i32/.i64), as do mono-key mangle fragments (ptr_i64,
tu_i64_bool) and all display/diagnostic formatting (i{d}).

Migrated in the same sweep: stdlib + examples + issue repros + FFI C
companions (shared symbol names like ffi_id_i64), expected
stdout/stderr/ir snapshots, specs.md, readme.md, CLAUDE.md/AGENTS.md,
implementation_plan.md, docs/, issue writeups. Vendored stb_image and
historical flow state left untouched.

zig build test: 426/426; examples suite: 595/595.
2026-06-12 09:31:53 +03:00

69 lines
2.0 KiB
Plaintext

// Typed allocation helpers over the Allocator protocol (std/mem.sx):
// create/destroy (one T), alloc/free (slices), clone, resize, and the
// bytes-level mem_realloc. Declared `ufcs` — the dot spelling
// (`context.allocator.create(Session)`), the pipe spelling, and the
// direct call all hit the same generic machinery. Contents are
// UNINITIALISED by design
// (Zig-aligned): assign before reading. TrackingAllocator balances to
// zero across every pair.
#import "modules/std.sx";
#import "modules/std/mem.sx";
Session :: struct { id: i64; score: i64; }
main :: () {
gpa := GPA.init();
tracker := TrackingAllocator.init(xx gpa);
a : Allocator = xx tracker;
// create / destroy — direct spelling
s := create(a, Session);
s.id = 7; s.score = 42;
print("create: {} {}\n", s.id, s.score);
destroy(a, s);
// create — fluent pipe spelling
p := a |> create(Session);
p.id = 1;
print("pipe-create: {}\n", p.id);
a |> destroy(p);
// create — canonical dot spelling on context.allocator
q := context.allocator.create(Session);
q.id = 2;
print("dot-create: {}\n", q.id);
context.allocator.destroy(q);
// alloc / free — typed slice
xs := a |> alloc(i64, 4);
xs[0] = 10; xs[1] = 20; xs[2] = 30; xs[3] = 40;
print("alloc: {} {} len={}\n", xs[0], xs[3], xs.len);
// clone — independent copy (canonical dot spelling)
ys := xs.clone(a);
xs[0] = 99;
print("clone: {} (orig {})\n", ys[0], xs[0]);
a.free(ys);
// resize — grow (copies, old backing freed)
zs := xs |> resize(a, 6);
zs[5] = 60;
print("resize: {} {} len={}\n", zs[1], zs[5], zs.len);
// resize — shrink
ws := zs |> resize(a, 2);
print("shrink: {} {} len={}\n", ws[0], ws[1], ws.len);
a |> free(ws);
// mem_realloc — bytes level
raw := a.alloc_bytes(8);
q : *i64 = xx raw;
q.* = 1234;
raw2 := mem_realloc(a, raw, 8, 16, 8);
q2 : *i64 = xx raw2;
print("realloc: {}\n", q2.*);
a.dealloc_bytes(raw2);
tracker.report();
}