allocator interface (first pass)

This commit is contained in:
2026-07-06 21:24:49 +02:00
parent 7ce3917c15
commit 95c61311ca
24 changed files with 833 additions and 161 deletions
+29
View File
@@ -0,0 +1,29 @@
c :: import "@ffi/c"
Allocator :: struct {
context ?*mut anyopaque
alloc @func(context ?*mut anyopaque, size usize, alignment usize) ?*mut u8
free @func(context ?*mut anyopaque, memory ?*mut u8, size usize, alignment usize) void
}
heap Allocator :: Allocator {
context = none,
alloc = heap_alloc,
free = heap_free,
}
heap_alloc func(context ?*mut anyopaque, size usize, alignment usize) ?*mut u8 {
return ptr_cast(u8, c.malloc(size))
}
heap_free func(context ?*mut anyopaque, memory ?*mut u8, size usize, alignment usize) void {
c.free(memory)
}
alloc func(allocator Allocator, size usize, alignment usize) ?*mut u8 {
return allocator.alloc(allocator.context, size, alignment)
}
free func(allocator Allocator, memory ?*mut u8, size usize, alignment usize) void {
allocator.free(allocator.context, memory, size, alignment)
}