diff options
| author | Damian Tarnawski <gthetarnav@gmail.com> | 2025-08-23 16:55:12 +0200 |
|---|---|---|
| committer | Damian Tarnawski <gthetarnav@gmail.com> | 2025-08-23 16:55:12 +0200 |
| commit | 2a6dfd2545b78b91ddb690f30068742212862f81 (patch) | |
| tree | b0ea5f36277cefea21584794797dc99bdc642cdd /base | |
| parent | 9d651348b5c6772db26af79dd2ff077c3af670f2 (diff) | |
Avoid overlap issues when correcting memory after resize in _reserve_soa
Diffstat (limited to 'base')
| -rw-r--r-- | base/runtime/core_builtin_soa.odin | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/base/runtime/core_builtin_soa.odin b/base/runtime/core_builtin_soa.odin index 7548f6735..46c767751 100644 --- a/base/runtime/core_builtin_soa.odin +++ b/base/runtime/core_builtin_soa.odin @@ -266,6 +266,12 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo // from: |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 + old_start := uintptr(new_data) + uintptr(new_size - old_size) + mem_copy(rawptr(old_start), old_data, old_size) + + // now: |_ _ _ x x y y z z| + for i in 0..<field_count { type := si.types[i].variant.(Type_Info_Multi_Pointer).elem @@ -273,18 +279,21 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo new_offset = align_forward_int(new_offset, max_align) new_data_elem := rawptr(uintptr(new_data) + uintptr(new_offset)) - old_data_elem := rawptr(uintptr(new_data) + uintptr(old_offset)) + old_data_elem := rawptr(old_start + uintptr(old_offset)) + + old_size_elem := type.size * old_cap + new_size_elem := type.size * capacity - mem_copy(new_data_elem, old_data_elem, type.size * old_cap) + mem_copy(new_data_elem, old_data_elem, old_size_elem) (^rawptr)(uintptr(array) + i*size_of(rawptr))^ = new_data_elem if zero_memory { - mem_zero(old_data_elem, int(uintptr(new_data_elem) - uintptr(old_data_elem))) + mem_zero(rawptr(uintptr(new_data_elem) + uintptr(old_size_elem)), new_size_elem - old_size_elem) } - old_offset += type.size * old_cap - new_offset += type.size * capacity + old_offset += old_size_elem + new_offset += new_size_elem } return nil |