items is now a []T slice whose .len IS the live element count (cap = allocated
capacity), so a List iterates directly: `for xs.items (e) { ... }`. A
`len :: (self) -> i64 #get => items.len` accessor keeps `xs.len` reads working;
`.len` WRITES become `.items.len`. List stays 24 bytes (`[]T`=16 + cap=8).
- list.sx: append/ensure_capacity/deinit rewritten for the slice backing. deinit
guards the free on `cap > 0` (true ownership) and resets via explicit
ptr=null/len=0 (a `.{}` slice assignment yields a garbage len; `.[]` is the
empty-slice literal but can't be assigned to a generic []T — both worked around).
- Compiler coupling updated: comptime_vm makeStringList/readStringList write/read
items as a {ptr,len} fat pointer at field 0 + cap at field 1; control_flow
listView views an `items: []T` slice (keeps the legacy {[*]T,len} shape too).
- Migrated List `.len` writes to `.items.len` in sched.sx + ui/{render,pipeline,
glyph_cache} + platform/{sdl3,android,uikit}.
- Snapshots: List's type-table layout changed → ~40 .ir + memory/0800 (items now
prints as a slice) regenerated; diagnostics/1183 retargeted to a genuine
many-pointer (xs.items is a slice now). Example memory/0840 locks for-each.
60 lines
2.7 KiB
Plaintext
60 lines
2.7 KiB
Plaintext
// The growable container of the prelude. Consumers never import this file
|
|
// directly — std.sx re-exports `List`.
|
|
#import "modules/std/core.sx";
|
|
|
|
// `items` is a `[]T` slice whose `.len` IS the live element count, so a `List`
|
|
// is directly iterable: `for xs.items (e) { ... }`. `cap` is the allocated
|
|
// capacity (`>= items.len`); the buffer spans `cap` elements, the slice spans
|
|
// the live `items.len`. The `len` `#get` accessor exposes the live count as
|
|
// `xs.len` (read), delegating to `items.len`; writes use `xs.items.len`.
|
|
List :: struct ($T: Type) {
|
|
items: []T = .[]; // empty slice ({ptr, len=0}); `.[]` is the empty-slice
|
|
// literal — `.{}` would init the slice's underlying
|
|
// {ptr,len} struct (and currently yields a garbage len).
|
|
cap: i64 = 0;
|
|
|
|
// No-paren read accessor: `xs.len` → the live element count.
|
|
len :: (self: *List(T)) -> i64 #get => self.items.len;
|
|
|
|
append :: (list: *List(T), item: T, alloc: Allocator = context.allocator) {
|
|
if list.items.len >= list.cap {
|
|
new_cap := if list.cap == 0 then 4 else list.cap * 2;
|
|
new_ptr : [*]T = xx alloc.alloc_bytes(new_cap * size_of(T));
|
|
if list.items.len > 0 {
|
|
memcpy(new_ptr, list.items.ptr, list.items.len * size_of(T));
|
|
alloc.dealloc_bytes(list.items.ptr);
|
|
}
|
|
list.items.ptr = new_ptr; // keep the live len; only the buffer moves
|
|
list.cap = new_cap;
|
|
}
|
|
list.items.ptr[list.items.len] = item; // write at the live index (within cap)
|
|
list.items.len += 1;
|
|
}
|
|
|
|
ensure_capacity :: (list: *List(T), n: i64, alloc: Allocator = context.allocator) {
|
|
if list.cap >= n { return; }
|
|
new_cap := if list.cap == 0 then 4 else list.cap;
|
|
while new_cap < n { new_cap = new_cap * 2; }
|
|
new_ptr : [*]T = xx alloc.alloc_bytes(new_cap * size_of(T));
|
|
if list.items.len > 0 {
|
|
memcpy(new_ptr, list.items.ptr, list.items.len * size_of(T));
|
|
alloc.dealloc_bytes(list.items.ptr);
|
|
}
|
|
list.items.ptr = new_ptr;
|
|
list.cap = new_cap;
|
|
}
|
|
|
|
deinit :: (list: *List(T), alloc: Allocator = context.allocator) {
|
|
// `cap > 0` is the ownership signal: a List holds an allocated buffer
|
|
// ONLY after a growth set `cap`. Guarding on `cap` (not `items.ptr`)
|
|
// makes deinit idempotent and safe on a never-grown / borrowed-items
|
|
// list — and `.[]` leaves a len-0 slice whose ptr need not be null.
|
|
if list.cap > 0 {
|
|
alloc.dealloc_bytes(list.items.ptr);
|
|
}
|
|
list.items.ptr = null;
|
|
list.items.len = 0;
|
|
list.cap = 0;
|
|
}
|
|
}
|