Alias heap calls from base:runtime is core:os

This commit is contained in:
gingerBill
2024-01-28 22:47:55 +00:00
parent 9a16bc5fc5
commit 038086d1d9
13 changed files with 50 additions and 316 deletions
-57
View File
@@ -49,60 +49,3 @@ _os_write :: proc "contextless" (data: []byte) -> (n: int, err: _OS_Errno) #no_b
n = int(total_write)
return
}
//
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
// Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
// padding. We also store the original pointer returned by heap_alloc right before
// the pointer we return to the user.
//
_windows_default_alloc_or_resize :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil, zero_memory := true) -> ([]byte, Allocator_Error) {
if size == 0 {
_windows_default_free(old_ptr)
return nil, nil
}
a := max(alignment, align_of(rawptr))
space := size + a - 1
allocated_mem: rawptr
if old_ptr != nil {
original_old_ptr := ([^]rawptr)(old_ptr)[-1]
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
} else {
allocated_mem = heap_alloc(space+size_of(rawptr), zero_memory)
}
aligned_mem := ([^]u8)(allocated_mem)[size_of(rawptr):]
ptr := uintptr(aligned_mem)
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a)
diff := int(aligned_ptr - ptr)
if (size + diff) > space || allocated_mem == nil {
return nil, .Out_Of_Memory
}
aligned_mem = ([^]byte)(aligned_ptr)
([^]rawptr)(aligned_mem)[-1] = allocated_mem
return aligned_mem[:size], nil
}
_windows_default_alloc :: proc "contextless" (size, alignment: int, zero_memory := true) -> ([]byte, Allocator_Error) {
return _windows_default_alloc_or_resize(size, alignment, nil, zero_memory)
}
_windows_default_free :: proc "contextless" (ptr: rawptr) {
if ptr != nil {
heap_free(([^]rawptr)(ptr)[-1])
}
}
_windows_default_resize :: proc "contextless" (p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, Allocator_Error) {
return _windows_default_alloc_or_resize(new_size, new_alignment, p)
}