typed alloc
This commit is contained in:
+50
-10
@@ -1,22 +1,58 @@
|
||||
c :: import "@ffi/c"
|
||||
|
||||
Allocator :: struct {
|
||||
context ?*mut anyopaque
|
||||
AllocatorVTable :: struct {
|
||||
alloc @func(context ?*mut anyopaque, size usize, alignment usize) ?*mut u8
|
||||
realloc @func(context ?*mut anyopaque, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8
|
||||
free @func(context ?*mut anyopaque, memory ?*mut u8, size usize, alignment usize) void
|
||||
}
|
||||
|
||||
alloc func(allocator Allocator, size usize, alignment usize) ?*mut u8 {
|
||||
return allocator.alloc(allocator.context, size, alignment)
|
||||
Allocator :: struct {
|
||||
context ?*mut anyopaque
|
||||
vtable @AllocatorVTable
|
||||
}
|
||||
|
||||
realloc func(allocator Allocator, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
|
||||
return allocator.realloc(allocator.context, memory, old_size, new_size, alignment)
|
||||
AllocError :: enum {
|
||||
out_of_memory
|
||||
}
|
||||
|
||||
free func(allocator Allocator, memory ?*mut u8, size usize, alignment usize) void {
|
||||
allocator.free(allocator.context, memory, size, alignment)
|
||||
raw_alloc func(allocator Allocator, size usize, alignment usize) ?*mut u8 {
|
||||
return allocator.vtable.alloc(allocator.context, size, alignment)
|
||||
}
|
||||
|
||||
raw_realloc func(allocator Allocator, memory ?*mut u8, old_size usize, new_size usize, alignment usize) ?*mut u8 {
|
||||
return allocator.vtable.realloc(allocator.context, memory, old_size, new_size, alignment)
|
||||
}
|
||||
|
||||
raw_free func(allocator Allocator, memory ?*mut u8, size usize, alignment usize) void {
|
||||
allocator.vtable.free(allocator.context, memory, size, alignment)
|
||||
}
|
||||
|
||||
_empty_storage [1]mut u64 = [0]
|
||||
|
||||
_empty_slice func($T type, count usize) []mut T {
|
||||
pointer *mut T :: ptr_cast(T, (&_empty_storage).ptr)
|
||||
return pointer[..count]
|
||||
}
|
||||
|
||||
alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
|
||||
if count == 0 {
|
||||
return _empty_slice(T, 0)
|
||||
}
|
||||
|
||||
element_size usize :: size_of(T)
|
||||
if element_size == 0 {
|
||||
return _empty_slice(T, count)
|
||||
}
|
||||
if count > max_value(usize) / element_size {
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
memory ?*mut u8 = raw_alloc(allocator, count * element_size, align_of(T))
|
||||
if memory |bytes| {
|
||||
pointer *mut T :: ptr_cast(T, bytes)
|
||||
return pointer[..count]
|
||||
}
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
_malloc_alignment usize :: 16 # ponytail: aarch64-macos libc malloc alignment assumption.
|
||||
@@ -93,9 +129,13 @@ _c_free func(_ ?*mut anyopaque, memory ?*mut u8, _ usize, _ usize) void {
|
||||
c.free(memory)
|
||||
}
|
||||
|
||||
c_allocator Allocator :: Allocator {
|
||||
context = none,
|
||||
_c_vtable AllocatorVTable :: AllocatorVTable {
|
||||
alloc = _c_alloc,
|
||||
realloc = _c_realloc,
|
||||
free = _c_free,
|
||||
}
|
||||
|
||||
c_allocator Allocator :: Allocator {
|
||||
context = none,
|
||||
vtable = &_c_vtable,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user