diff --git a/base/runtime/core_builtin_soa.odin b/base/runtime/core_builtin_soa.odin index e9a284a04..48ab7aebc 100644 --- a/base/runtime/core_builtin_soa.odin +++ b/base/runtime/core_builtin_soa.odin @@ -180,10 +180,17 @@ resize_soa :: proc(array: ^$T/#soa[dynamic]$E, #any_int length: int, loc := #cal } footer := raw_soa_footer(array) + old_len := footer.len + old_cap := footer.cap - if length > footer.cap { + if length > old_cap { reserve_soa(array, length, loc) or_return - } else if size_of(E) > 0 && length > footer.len { + } + + // reserve_soa has zeroed any newly allocated [old_cap, length) + // reused [old_len, min(old_cap, length)) still needs zeroing + to_zero_end := min(old_cap, length) + if size_of(E) > 0 && to_zero_end > old_len { ti := type_info_base(type_info_of(typeid_of(T))) si := &ti.variant.(Type_Info_Struct) @@ -197,13 +204,13 @@ resize_soa :: proc(array: ^$T/#soa[dynamic]$E, #any_int length: int, loc := #cal soa_offset = align_forward_int(soa_offset, align_of(E)) - mem_zero(rawptr(uintptr(data) + uintptr(soa_offset) + uintptr(type.size * footer.len)), type.size * (length - footer.len)) + mem_zero(rawptr(uintptr(data) + uintptr(soa_offset) + uintptr(type.size * old_len)), type.size * (to_zero_end - old_len)) soa_offset += type.size * footer.cap } } - footer.len = length + footer.len = max(length, 0) return nil } @@ -214,7 +221,7 @@ non_zero_resize_soa :: proc(array: ^$T/#soa[dynamic]$E, #any_int length: int, lo } non_zero_reserve_soa(array, length, loc) or_return footer := raw_soa_footer(array) - footer.len = length + footer.len = max(length, 0) return nil }