mem: rename Allocator primitives to alloc_bytes/dealloc_bytes (Phase 4 naming pulled forward, Agra-approved)

This commit is contained in:
agra
2026-06-11 15:33:35 +03:00
parent ca5bd52262
commit 88bae3c9f5
58 changed files with 1536 additions and 1534 deletions

View File

@@ -40,9 +40,11 @@ string :: []u8 #builtin;
// --- Allocator protocol (impls live in std/mem.sx) ---
// Bytes-level primitives carry the `_bytes` suffix so the typed
// helpers in std/mem.sx own the bare names (`alloc(T, n)`, `free(s)`).
Allocator :: protocol #inline {
alloc :: (size: s64) -> *void;
dealloc :: (ptr: *void);
alloc_bytes :: (size: s64) -> *void;
dealloc_bytes :: (ptr: *void);
}
// --- Context ---
@@ -55,7 +57,7 @@ Context :: struct {
// --- Slice & string allocation ---
cstring :: (size: s64) -> string {
raw := context.allocator.alloc(size + 1);
raw := context.allocator.alloc_bytes(size + 1);
memset(raw, 0, size + 1);
s : string = ---;
s.ptr = xx raw;
@@ -64,7 +66,7 @@ cstring :: (size: s64) -> string {
}
alloc_slice :: ($T: Type, count: s64) -> []T {
raw := context.allocator.alloc(count * size_of(T));
raw := context.allocator.alloc_bytes(count * size_of(T));
memset(raw, 0, count * size_of(T));
s : []T = ---;
s.ptr = xx raw;
@@ -456,10 +458,10 @@ List :: struct ($T: Type) {
append :: (list: *List(T), item: T, alloc: Allocator = context.allocator) {
if list.len >= list.cap {
new_cap := if list.cap == 0 then 4 else list.cap * 2;
new_items : [*]T = xx alloc.alloc(new_cap * size_of(T));
new_items : [*]T = xx alloc.alloc_bytes(new_cap * size_of(T));
if list.len > 0 {
memcpy(new_items, list.items, list.len * size_of(T));
alloc.dealloc(list.items);
alloc.dealloc_bytes(list.items);
}
list.items = new_items;
list.cap = new_cap;
@@ -472,10 +474,10 @@ List :: struct ($T: Type) {
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_items : [*]T = xx alloc.alloc(new_cap * size_of(T));
new_items : [*]T = xx alloc.alloc_bytes(new_cap * size_of(T));
if list.len > 0 {
memcpy(new_items, list.items, list.len * size_of(T));
alloc.dealloc(list.items);
alloc.dealloc_bytes(list.items);
}
list.items = new_items;
list.cap = new_cap;
@@ -483,7 +485,7 @@ List :: struct ($T: Type) {
deinit :: (list: *List(T), alloc: Allocator = context.allocator) {
if list.items != null {
alloc.dealloc(list.items);
alloc.dealloc_bytes(list.items);
}
list.items = null;
list.len = 0;

View File

@@ -108,10 +108,10 @@ Array :: struct {
add :: (self: *Array, v: Value, alloc: Allocator) {
if self.len >= self.cap {
new_cap := if self.cap == 0 then 4 else self.cap * 2;
new_items : [*]Value = xx alloc.alloc(new_cap * size_of(Value));
new_items : [*]Value = xx alloc.alloc_bytes(new_cap * size_of(Value));
if self.len > 0 {
memcpy(new_items, self.items, self.len * size_of(Value));
alloc.dealloc(self.items);
alloc.dealloc_bytes(self.items);
}
self.items = new_items;
self.cap = new_cap;
@@ -121,7 +121,7 @@ Array :: struct {
}
deinit :: (self: *Array, alloc: Allocator) {
if self.items != null { alloc.dealloc(self.items); }
if self.items != null { alloc.dealloc_bytes(self.items); }
self.items = null;
self.len = 0;
self.cap = 0;
@@ -142,10 +142,10 @@ Object :: struct {
put :: (self: *Object, key: string, v: Value, alloc: Allocator) {
if self.len >= self.cap {
new_cap := if self.cap == 0 then 4 else self.cap * 2;
new_items : [*]Member = xx alloc.alloc(new_cap * size_of(Member));
new_items : [*]Member = xx alloc.alloc_bytes(new_cap * size_of(Member));
if self.len > 0 {
memcpy(new_items, self.items, self.len * size_of(Member));
alloc.dealloc(self.items);
alloc.dealloc_bytes(self.items);
}
self.items = new_items;
self.cap = new_cap;
@@ -155,7 +155,7 @@ Object :: struct {
}
deinit :: (self: *Object, alloc: Allocator) {
if self.items != null { alloc.dealloc(self.items); }
if self.items != null { alloc.dealloc_bytes(self.items); }
self.items = null;
self.len = 0;
self.cap = 0;
@@ -538,7 +538,7 @@ Parser :: struct {
return string.{ ptr = @self.src[start], len = end - start };
}
raw_len := end - start; // decoded length <= raw_len (escapes shrink)
out : [*]u8 = xx self.alloc.alloc(raw_len);
out : [*]u8 = xx self.alloc.alloc_bytes(raw_len);
dlen := try self.decode_into(start, end, out);
self.pos = end + 1;
return string.{ ptr = out, len = dlen };

View File

@@ -11,10 +11,10 @@
CAllocator :: struct {}
impl Allocator for CAllocator {
alloc :: (self: *CAllocator, size: s64) -> *void {
alloc_bytes :: (self: *CAllocator, size: s64) -> *void {
return libc_malloc(size);
}
dealloc :: (self: *CAllocator, ptr: *void) {
dealloc_bytes :: (self: *CAllocator, ptr: *void) {
libc_free(ptr);
}
}
@@ -40,11 +40,11 @@ GPA :: struct {
}
impl Allocator for GPA {
alloc :: (self: *GPA, size: s64) -> *void {
alloc_bytes :: (self: *GPA, size: s64) -> *void {
self.alloc_count += 1;
return libc_malloc(size);
}
dealloc :: (self: *GPA, ptr: *void) {
dealloc_bytes :: (self: *GPA, ptr: *void) {
self.alloc_count -= 1;
libc_free(ptr);
}
@@ -78,7 +78,7 @@ Arena :: struct {
prev_cap := if a.first != null then a.first.cap else 0;
needed := min_size + 16 + 16;
len := (prev_cap + needed) * 3 / 2;
raw := a.parent.alloc(len);
raw := a.parent.alloc_bytes(len);
chunk : *ArenaChunk = xx raw;
chunk.next = a.first;
chunk.cap = len;
@@ -97,7 +97,7 @@ Arena :: struct {
it := a.first.next;
while it != null {
next := it.next;
a.parent.dealloc(it);
a.parent.dealloc_bytes(it);
it = next;
}
a.first.next = null;
@@ -109,7 +109,7 @@ Arena :: struct {
it := a.first;
while it != null {
next := it.next;
a.parent.dealloc(it);
a.parent.dealloc_bytes(it);
it = next;
}
a.first = null;
@@ -118,7 +118,7 @@ Arena :: struct {
}
impl Allocator for Arena {
alloc :: (self: *Arena, size: s64) -> *void {
alloc_bytes :: (self: *Arena, size: s64) -> *void {
aligned := (size + 7) & (0 - 8);
if self.first != null {
usable := self.first.cap - 16;
@@ -135,7 +135,7 @@ impl Allocator for Arena {
self.end_index = self.end_index + aligned;
ptr
}
dealloc :: (self: *Arena, ptr: *void) {}
dealloc_bytes :: (self: *Arena, ptr: *void) {}
}
// --- BufAlloc: bump allocator backed by a user-provided slice ---
@@ -167,7 +167,7 @@ BufAlloc :: struct {
}
impl Allocator for BufAlloc {
alloc :: (self: *BufAlloc, size: s64) -> *void {
alloc_bytes :: (self: *BufAlloc, size: s64) -> *void {
aligned := (size + 7) & (0 - 8);
if self.pos + aligned > self.len {
return null;
@@ -176,7 +176,7 @@ impl Allocator for BufAlloc {
self.pos = self.pos + aligned;
ptr
}
dealloc :: (self: *BufAlloc, ptr: *void) {}
dealloc_bytes :: (self: *BufAlloc, ptr: *void) {}
}
// --- TrackingAllocator: wraps any Allocator, counts allocs/deallocs ---
@@ -228,16 +228,16 @@ TrackingAllocator :: struct {
}
impl Allocator for TrackingAllocator {
alloc :: (self: *TrackingAllocator, size: s64) -> *void {
ptr := self.parent.alloc(size);
alloc_bytes :: (self: *TrackingAllocator, size: s64) -> *void {
ptr := self.parent.alloc_bytes(size);
if ptr != null {
self.alloc_count += 1;
self.total_alloc_bytes += size;
}
ptr
}
dealloc :: (self: *TrackingAllocator, ptr: *void) {
self.parent.dealloc(ptr);
dealloc_bytes :: (self: *TrackingAllocator, ptr: *void) {
self.parent.dealloc_bytes(ptr);
self.dealloc_count += 1;
}
}

View File

@@ -211,7 +211,7 @@ GlyphCache :: struct {
self.font_data_size = file_size;
// Init stbtt_fontinfo
self.font_info = self.parent_allocator.alloc(FONTINFO_SIZE);
self.font_info = self.parent_allocator.alloc_bytes(FONTINFO_SIZE);
memset(self.font_info, 0, FONTINFO_SIZE);
stbtt_InitFont(self.font_info, font_data, 0);
@@ -242,7 +242,7 @@ GlyphCache :: struct {
self.atlas_width = GLYPH_ATLAS_W;
self.atlas_height = GLYPH_ATLAS_H;
bitmap_size : s64 = xx self.atlas_width * xx self.atlas_height;
self.bitmap = xx self.parent_allocator.alloc(bitmap_size);
self.bitmap = xx self.parent_allocator.alloc_bytes(bitmap_size);
memset(self.bitmap, 0, bitmap_size);
// Shelf packer init
@@ -258,10 +258,10 @@ GlyphCache :: struct {
// Init hash table (256 slots)
self.hash_cap = 256;
hash_bytes : s64 = self.hash_cap * 4; // u32 per slot
self.hash_keys = xx self.parent_allocator.alloc(hash_bytes);
self.hash_keys = xx self.parent_allocator.alloc_bytes(hash_bytes);
memset(self.hash_keys, 0, hash_bytes);
val_bytes : s64 = self.hash_cap * 8; // s64 per slot (s32 would suffice but alignment)
self.hash_vals = xx self.parent_allocator.alloc(val_bytes);
self.hash_vals = xx self.parent_allocator.alloc_bytes(val_bytes);
// Create the atlas texture. In GPU-protocol mode we create empty and
// let the first `flush()` push the (zero-initialized) bitmap via
@@ -406,10 +406,10 @@ GlyphCache :: struct {
self.hash_cap = old_cap * 2;
hash_bytes : s64 = self.hash_cap * 4;
self.hash_keys = xx self.parent_allocator.alloc(hash_bytes);
self.hash_keys = xx self.parent_allocator.alloc_bytes(hash_bytes);
memset(self.hash_keys, 0, hash_bytes);
val_bytes : s64 = self.hash_cap * 8;
self.hash_vals = xx self.parent_allocator.alloc(val_bytes);
self.hash_vals = xx self.parent_allocator.alloc_bytes(val_bytes);
// Rehash
mask := self.hash_cap - 1;
@@ -427,8 +427,8 @@ GlyphCache :: struct {
i += 1;
}
self.parent_allocator.dealloc(old_keys);
self.parent_allocator.dealloc(old_vals);
self.parent_allocator.dealloc_bytes(old_keys);
self.parent_allocator.dealloc_bytes(old_vals);
}
// Upload dirty atlas to GPU. On the Metal path, defer the upload to
@@ -489,7 +489,7 @@ GlyphCache :: struct {
new_w := self.atlas_width * 2;
new_h := self.atlas_height * 2;
new_size : s64 = xx new_w * xx new_h;
new_bitmap : [*]u8 = xx self.parent_allocator.alloc(new_size);
new_bitmap : [*]u8 = xx self.parent_allocator.alloc_bytes(new_size);
memset(new_bitmap, 0, new_size);
// Copy old rows into new bitmap
@@ -501,7 +501,7 @@ GlyphCache :: struct {
y += 1;
}
self.parent_allocator.dealloc(self.bitmap);
self.parent_allocator.dealloc_bytes(self.bitmap);
self.bitmap = new_bitmap;
self.atlas_width = new_w;
self.atlas_height = new_h;

View File

@@ -50,7 +50,7 @@ UIRenderer :: struct {
init :: (self: *UIRenderer) {
// Allocate vertex scratch (CPU side) — same for both backends.
buf_size := MAX_UI_VERTICES * UI_VERTEX_BYTES;
self.vertices = xx context.allocator.alloc(buf_size);
self.vertices = xx context.allocator.alloc_bytes(buf_size);
memset(self.vertices, 0, buf_size);
self.vertex_count = 0;
self.dpi_scale = 1.0;

View File

@@ -45,7 +45,7 @@ StateStore :: struct {
}
// Create new entry
data : [*]u8 = xx self.parent_allocator.alloc(size_of(T));
data : [*]u8 = xx self.parent_allocator.alloc_bytes(size_of(T));
memcpy(data, @default, size_of(T));
self.entries.append(.{
id = id,