Merge pull request #3815 from laytan/tlsf-fixes

tlsf: destroy first pool & properly zero memory
This commit is contained in:
Jeroen van Rijn
2024-06-27 19:50:24 +02:00
committed by GitHub
2 changed files with 11 additions and 6 deletions
+8 -3
View File
@@ -97,6 +97,10 @@ init :: proc{init_from_buffer, init_from_allocator}
destroy :: proc(control: ^Allocator) { destroy :: proc(control: ^Allocator) {
if control == nil { return } if control == nil { return }
if control.pool.allocator.procedure != nil {
runtime.delete(control.pool.data, control.pool.allocator)
}
// No need to call `pool_remove` or anything, as they're they're embedded in the backing memory. // No need to call `pool_remove` or anything, as they're they're embedded in the backing memory.
// We do however need to free the `Pool` tracking entities and the backing memory itself. // We do however need to free the `Pool` tracking entities and the backing memory itself.
// As `Allocator` is embedded in the first backing slice, the `control` pointer will be // As `Allocator` is embedded in the first backing slice, the `control` pointer will be
@@ -132,8 +136,9 @@ allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
return nil, nil return nil, nil
case .Free_All: case .Free_All:
clear(control) // NOTE: this doesn't work right at the moment, Jeroen has it on his to-do list :)
return nil, nil // clear(control)
return nil, .Mode_Not_Implemented
case .Resize: case .Resize:
return resize(control, old_memory, uint(old_size), uint(size), uint(alignment)) return resize(control, old_memory, uint(old_size), uint(size), uint(alignment))
@@ -144,7 +149,7 @@ allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
case .Query_Features: case .Query_Features:
set := (^runtime.Allocator_Mode_Set)(old_memory) set := (^runtime.Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Resize_Non_Zeroed, .Query_Features} set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, /* .Free_All, */ .Resize, .Resize_Non_Zeroed, .Query_Features}
} }
return nil, nil return nil, nil
+1 -1
View File
@@ -630,7 +630,7 @@ alloc_bytes_non_zeroed :: proc(control: ^Allocator, size: uint, align: uint) ->
@(require_results) @(require_results)
alloc_bytes :: proc(control: ^Allocator, size: uint, align: uint) -> (res: []byte, err: runtime.Allocator_Error) { alloc_bytes :: proc(control: ^Allocator, size: uint, align: uint) -> (res: []byte, err: runtime.Allocator_Error) {
res, err = alloc_bytes_non_zeroed(control, size, align) res, err = alloc_bytes_non_zeroed(control, size, align)
if err != nil { if err == nil {
intrinsics.mem_zero(raw_data(res), len(res)) intrinsics.mem_zero(raw_data(res), len(res))
} }
return return