mem: BufAlloc.init returns the state by value — full buffer usable, no header carve
This commit is contained in:
@@ -220,9 +220,13 @@ impl Allocator for Arena {
|
||||
|
||||
// --- BufAlloc: bump allocator backed by a user-provided slice ---
|
||||
//
|
||||
// `init` returns the BufAlloc by value (the caller's local IS the
|
||||
// state, like every allocator); the FULL buffer is usable — no bytes
|
||||
// are carved off its head for the state struct.
|
||||
//
|
||||
// Usage:
|
||||
// stack_buf : [128]u8 = ---;
|
||||
// buf := BufAlloc.init(@stack_buf[0], 128); // *BufAlloc
|
||||
// buf := BufAlloc.init(@stack_buf[0], 128); // BufAlloc
|
||||
// push Context.{ allocator = xx buf, data = null } { ... }
|
||||
// buf.reset();
|
||||
|
||||
@@ -231,14 +235,8 @@ BufAlloc :: struct {
|
||||
len: s64;
|
||||
pos: s64;
|
||||
|
||||
init :: (buf: [*]u8, len: s64) -> *BufAlloc {
|
||||
self_size :: size_of(BufAlloc);
|
||||
if len < self_size { return null; }
|
||||
b : *BufAlloc = xx buf;
|
||||
b.buf = @buf[self_size];
|
||||
b.len = len - self_size;
|
||||
b.pos = 0;
|
||||
b
|
||||
init :: (buf: [*]u8, len: s64) -> BufAlloc {
|
||||
BufAlloc.{ buf = buf, len = len, pos = 0 }
|
||||
}
|
||||
|
||||
reset :: (b: *BufAlloc) {
|
||||
|
||||
Reference in New Issue
Block a user