Files
sx/examples/0808-memory-xx-value-routes-through-context-allocator.sx
agra 59f0aa7716 std: restructure — std/ modules, namespace tail, std/xml.sx
allocators/fs/process/socket/log/trace/test move under modules/std/
(allocators.sx becomes std/mem.sx; the Allocator protocol moves into
the std.sx prelude, impls stay in mem.sx). New std/xml.sx holds
xml_escape as xml.escape. std.sx gains the carried namespace tail —
flat-importing std.sx now also provides mem./xml./log. — with the
remaining modules (fs/process/socket/json/cli/hash/test) deferred from
the tail until the global last-wins maps are fully own-wins (pulling
them into every closure collides bare names corpus-wide; they stay
direct imports: modules/std/fs.sx etc.). log.sx's internal emit
renamed log_emit (it clobbered consumer fns named emit program-wide).
bundle.sx uses xml.escape via the carried alias. Consumer import paths
swept mechanically; .ir snapshots recaptured for the larger std
closure. m3te + game build unchanged.
2026-06-11 06:10:59 +03:00

46 lines
1.4 KiB
Plaintext

// Phase 1.1 — the compiler-internal heap-copy that backs `xx <rvalue>`
// protocol erasure must dispatch through `context.allocator`, not call
// libc malloc directly. So when a `push Context.{ allocator = tracer }`
// block is active, a `xx StructLiteral.{}` inside it MUST be allocated
// by the tracker.
//
// Note: `xx` only heap-copies for RVALUES (struct literals, call results).
// `xx <lvalue>` (an identifier, field access, index, or deref) borrows
// the operand's storage, so it never allocates and never reaches this
// path. See specs.md §3 — Protocol value ownership and lifetime.
#import "modules/std.sx";
#import "modules/std/mem.sx"; // `Allocator` is non-transitive: name it, import it.
Tracer :: struct {
count: s64;
init :: () -> *Tracer {
t : *Tracer = xx malloc(size_of(Tracer));
t.count = 0;
t
}
}
impl Allocator for Tracer {
alloc :: (self: *Tracer, size: s64) -> *void {
self.count += 1;
return malloc(size);
}
dealloc :: (self: *Tracer, ptr: *void) {
free(ptr);
}
}
ByValue :: struct { x: s64; y: s64; }
main :: () -> s32 {
tracer := Tracer.init();
push Context.{ allocator = xx tracer, data = null } {
// Struct-literal operand: rvalue → heap-copy through context.allocator.
ignore : Allocator = xx ByValue.{ x = 1, y = 2 };
_ = ignore;
}
print("Tracer.count = {}\n", tracer.count);
0
}