Browse Source

Merge pull request #3815 from laytan/tlsf-fixes

tlsf: destroy first pool & properly zero memory
Jeroen van Rijn 1 year ago
parent
commit
9f8b84c212
2 changed files with 11 additions and 6 deletions
  1. 9 4
      core/mem/tlsf/tlsf.odin
  2. 2 2
      core/mem/tlsf/tlsf_internal.odin

+ 9 - 4
core/mem/tlsf/tlsf.odin

@@ -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)
-		return nil, nil
+		// NOTE: this doesn't work right at the moment, Jeroen has it on his to-do list :)
+		// 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
 
 
@@ -153,4 +158,4 @@ allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
 	}
 	}
 
 
 	return nil, nil
 	return nil, nil
-}
+}

+ 2 - 2
core/mem/tlsf/tlsf_internal.odin

@@ -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
@@ -735,4 +735,4 @@ resize_non_zeroed :: proc(control: ^Allocator, ptr: rawptr, old_size, new_size:
 	block_trim_used(control, block, adjust)
 	block_trim_used(control, block, adjust)
 	res = ([^]byte)(ptr)[:new_size]
 	res = ([^]byte)(ptr)[:new_size]
 	return
 	return
-}
+}