26 lines
769 B
Plaintext
26 lines
769 B
Plaintext
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 = func(_ ?*mut anyopaque, size usize, _ usize) ?*mut u8 {
|
|
return ptr_cast(u8, c.malloc(size))
|
|
},
|
|
free = func(_ ?*mut anyopaque, memory ?*mut u8, _ usize, _ 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)
|
|
}
|