allocator interface (first pass)
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
malloc c_func(size usize) ?*mut u8
|
||||
free c_func(ptr ?*mut u8) void
|
||||
mem :: import ".."
|
||||
|
||||
alloc func(size usize) ?*mut u8 {
|
||||
return malloc(size)
|
||||
return mem.alloc(mem.heap, size, 1)
|
||||
}
|
||||
|
||||
free func(memory ?*mut u8) void {
|
||||
mem.free(mem.heap, memory, 0, 1)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user