[mem]: Add free_all for buddy allocator

This commit is contained in:
flysand7
2024-09-07 13:27:17 +11:00
parent c0e17808d4
commit c0112d1c70
+9 -9
View File
@@ -1304,6 +1304,14 @@ buddy_allocator_free :: proc(b: ^Buddy_Allocator, ptr: rawptr) -> Allocator_Erro
return nil return nil
} }
buddy_allocator_free_all :: proc(b: ^Buddy_Allocator) {
alignment := b.alignment
head := ([^]byte)(b.head)
tail := ([^]byte)(b.tail)
data := head[:ptr_sub(tail, head)]
buddy_allocator_init(b, data, alignment)
}
buddy_allocator_proc :: proc( buddy_allocator_proc :: proc(
allocator_data: rawptr, allocator_data: rawptr,
mode: Allocator_Mode, mode: Allocator_Mode,
@@ -1312,7 +1320,6 @@ buddy_allocator_proc :: proc(
old_size: int, old_size: int,
loc := #caller_location, loc := #caller_location,
) -> ([]byte, Allocator_Error) { ) -> ([]byte, Allocator_Error) {
b := (^Buddy_Allocator)(allocator_data) b := (^Buddy_Allocator)(allocator_data)
switch mode { switch mode {
case .Alloc: case .Alloc:
@@ -1326,18 +1333,13 @@ buddy_allocator_proc :: proc(
case .Free: case .Free:
return nil, buddy_allocator_free(b, old_memory) return nil, buddy_allocator_free(b, old_memory)
case .Free_All: case .Free_All:
alignment := b.alignment buddy_allocator_free_all(b)
head := ([^]byte)(b.head)
tail := ([^]byte)(b.tail)
data := head[:ptr_sub(tail, head)]
buddy_allocator_init(b, data, alignment)
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Query_Features, .Alloc, .Alloc_Non_Zeroed, .Resize, .Resize_Non_Zeroed, .Free, .Free_All, .Query_Info} set^ = {.Query_Features, .Alloc, .Alloc_Non_Zeroed, .Resize, .Resize_Non_Zeroed, .Free, .Free_All, .Query_Info}
} }
return nil, nil return nil, nil
case .Query_Info: case .Query_Info:
info := (^Allocator_Query_Info)(old_memory) info := (^Allocator_Query_Info)(old_memory)
if info != nil && info.pointer != nil { if info != nil && info.pointer != nil {
@@ -1345,7 +1347,6 @@ buddy_allocator_proc :: proc(
if !(b.head <= ptr && ptr <= b.tail) { if !(b.head <= ptr && ptr <= b.tail) {
return nil, .Invalid_Pointer return nil, .Invalid_Pointer
} }
block := (^Buddy_Block)(([^]byte)(ptr)[-b.alignment:]) block := (^Buddy_Block)(([^]byte)(ptr)[-b.alignment:])
info.size = int(block.size) info.size = int(block.size)
info.alignment = int(b.alignment) info.alignment = int(b.alignment)
@@ -1353,6 +1354,5 @@ buddy_allocator_proc :: proc(
} }
return nil, nil return nil, nil
} }
return nil, nil return nil, nil
} }