Handle nil old data case

This commit is contained in:
Damian Tarnawski
2025-08-24 15:04:19 +02:00
parent ac4a89e765
commit 66f4c93420
+13 -6
View File
@@ -249,26 +249,27 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo
old_data := (^rawptr)(array)^ old_data := (^rawptr)(array)^
if old_data != nil {
new_bytes, resize_err := array.allocator.procedure( new_bytes, resize_err := array.allocator.procedure(
array.allocator.data, .Resize_Non_Zeroed, new_size, max_align, array.allocator.data, .Resize_Non_Zeroed, new_size, max_align,
old_data, old_size, loc, old_data, old_size, loc,
) )
new_data := raw_data(new_bytes) new_data := raw_data(new_bytes)
old_offset := 0
new_offset := 0
if resize_err == .None { if resize_err == .None {
footer.cap = capacity footer.cap = capacity
old_offset := 0
new_offset := 0
// Correct data memory // Correct data memory
// from: |x x y y z z _ _ _| // from: |x x y y z z _ _ _|
// to: |x x _ y y _ z z _| // to: |x x _ y y _ z z _|
// move old data to the end of the new allocation to avoid overlap // move old data to the end of the new allocation to avoid overlap
old_start := uintptr(new_data) + uintptr(new_size - old_size) old_start := uintptr(new_data) + uintptr(new_size - old_size)
mem_copy(rawptr(old_start), old_data, old_size) mem_copy(rawptr(old_start), new_data, old_size)
// now: |_ _ _ x x y y z z| // now: |_ _ _ x x y y z z|
@@ -302,15 +303,19 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo
if resize_err != .Mode_Not_Implemented { if resize_err != .Mode_Not_Implemented {
return resize_err return resize_err
} }
}
new_bytes = array.allocator.procedure( new_bytes := array.allocator.procedure(
array.allocator.data, .Alloc if zero_memory else .Alloc_Non_Zeroed, new_size, max_align, array.allocator.data, .Alloc if zero_memory else .Alloc_Non_Zeroed, new_size, max_align,
nil, old_size, loc, nil, old_size, loc,
) or_return ) or_return
new_data = raw_data(new_bytes) new_data := raw_data(new_bytes)
footer.cap = capacity footer.cap = capacity
old_offset := 0
new_offset := 0
// Correct data memory // Correct data memory
// from: |x x y y z z| ... |_ _ _ _ _ _ _ _ _| // from: |x x y y z z| ... |_ _ _ _ _ _ _ _ _|
// to: |x x _ y y _ z z _| // to: |x x _ y y _ z z _|
@@ -332,10 +337,12 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo
new_offset += type.size * capacity new_offset += type.size * capacity
} }
if old_data != nil {
array.allocator.procedure( array.allocator.procedure(
array.allocator.data, .Free, 0, max_align, array.allocator.data, .Free, 0, max_align,
old_data, old_size, loc, old_data, old_size, loc,
) or_return ) or_return
}
return nil return nil
} }