mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Merge branch 'master' into soa-resize-zero-memory
This commit is contained in:
@@ -273,17 +273,77 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo
|
|||||||
|
|
||||||
old_data := (^rawptr)(array)^
|
old_data := (^rawptr)(array)^
|
||||||
|
|
||||||
|
resize: if old_data != nil {
|
||||||
|
|
||||||
|
new_bytes, resize_err := array.allocator.procedure(
|
||||||
|
array.allocator.data, .Resize_Non_Zeroed, new_size, max_align,
|
||||||
|
old_data, old_size, loc,
|
||||||
|
)
|
||||||
|
new_data := raw_data(new_bytes)
|
||||||
|
|
||||||
|
#partial switch resize_err {
|
||||||
|
case .Mode_Not_Implemented: break resize
|
||||||
|
case .None: // continue resizing
|
||||||
|
case: return resize_err
|
||||||
|
}
|
||||||
|
|
||||||
|
footer.cap = capacity
|
||||||
|
|
||||||
|
old_offset := 0
|
||||||
|
new_offset := 0
|
||||||
|
|
||||||
|
// Correct data memory
|
||||||
|
// 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_data = rawptr(uintptr(new_data) + uintptr(new_size - old_size))
|
||||||
|
mem_copy(old_data, new_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
|
||||||
|
|
||||||
|
old_offset = align_forward_int(old_offset, max_align)
|
||||||
|
new_offset = align_forward_int(new_offset, max_align)
|
||||||
|
|
||||||
|
new_data_elem := rawptr(uintptr(new_data) + uintptr(new_offset))
|
||||||
|
old_data_elem := rawptr(uintptr(old_data) + uintptr(old_offset))
|
||||||
|
|
||||||
|
old_size_elem := type.size * old_cap
|
||||||
|
new_size_elem := type.size * capacity
|
||||||
|
|
||||||
|
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(rawptr(uintptr(new_data_elem) + uintptr(old_size_elem)), new_size_elem - old_size_elem)
|
||||||
|
}
|
||||||
|
|
||||||
|
old_offset += old_size_elem
|
||||||
|
new_offset += new_size_elem
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
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
|
old_offset := 0
|
||||||
new_offset := 0
|
new_offset := 0
|
||||||
|
|
||||||
|
// Correct data memory
|
||||||
|
// from: |x x y y z z| ... |_ _ _ _ _ _ _ _ _|
|
||||||
|
// to: |x x _ y y _ z z _|
|
||||||
|
|
||||||
for i in 0..<field_count {
|
for i in 0..<field_count {
|
||||||
type := si.types[i].variant.(Type_Info_Multi_Pointer).elem
|
type := si.types[i].variant.(Type_Info_Multi_Pointer).elem
|
||||||
|
|
||||||
@@ -301,10 +361,12 @@ _reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, zero_memory: boo
|
|||||||
new_offset += type.size * capacity
|
new_offset += type.size * capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
array.allocator.procedure(
|
if old_data != nil {
|
||||||
array.allocator.data, .Free, 0, max_align,
|
array.allocator.procedure(
|
||||||
old_data, old_size, loc,
|
array.allocator.data, .Free, 0, max_align,
|
||||||
) or_return
|
old_data, old_size, loc,
|
||||||
|
) or_return
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -213,6 +213,84 @@ test_soa_array_resize :: proc(t: ^testing.T) {
|
|||||||
testing.expect_value(t, array[2], V{0, 0})
|
testing.expect_value(t, array[2], V{0, 0})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_soa_make_len :: proc(t: ^testing.T) {
|
||||||
|
|
||||||
|
array, err := make(#soa[dynamic][2]int, 2)
|
||||||
|
defer delete(array)
|
||||||
|
array[0] = [2]int{1, 2}
|
||||||
|
array[1] = [2]int{3, 4}
|
||||||
|
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
testing.expect_value(t, len(array), 2)
|
||||||
|
testing.expect_value(t, cap(array), 2)
|
||||||
|
|
||||||
|
testing.expect_value(t, array[0], [2]int{1, 2})
|
||||||
|
testing.expect_value(t, array[1], [2]int{3, 4})
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_soa_array_allocator_resize :: proc(t: ^testing.T) {
|
||||||
|
|
||||||
|
arena: runtime.Arena
|
||||||
|
context.allocator = runtime.arena_allocator(&arena)
|
||||||
|
defer runtime.arena_destroy(&arena)
|
||||||
|
|
||||||
|
// |1 3 _ 2 4 _|
|
||||||
|
// |1 3 _ _ 2 4 _ _|
|
||||||
|
|
||||||
|
array, err := make(#soa[dynamic][2]int, 2, 3)
|
||||||
|
defer delete(array)
|
||||||
|
array[0] = [2]int{1, 2}
|
||||||
|
array[1] = [2]int{3, 4}
|
||||||
|
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
testing.expect_value(t, len(array), 2)
|
||||||
|
testing.expect_value(t, cap(array), 3)
|
||||||
|
|
||||||
|
err = resize(&array, 4)
|
||||||
|
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
testing.expect_value(t, len(array), 4)
|
||||||
|
testing.expect_value(t, cap(array), 4)
|
||||||
|
|
||||||
|
testing.expect_value(t, array[0], [2]int{1, 2})
|
||||||
|
testing.expect_value(t, array[1], [2]int{3, 4})
|
||||||
|
testing.expect_value(t, array[2], [2]int{0, 0})
|
||||||
|
testing.expect_value(t, array[3], [2]int{0, 0})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_soa_array_allocator_resize_overlapping :: proc(t: ^testing.T) {
|
||||||
|
|
||||||
|
arena: runtime.Arena
|
||||||
|
context.allocator = runtime.arena_allocator(&arena)
|
||||||
|
defer runtime.arena_destroy(&arena)
|
||||||
|
|
||||||
|
// |1 4 2 5 3 6|
|
||||||
|
// |1 4 _ _ 2 5 _ _ 3 6 _ _|
|
||||||
|
|
||||||
|
array, err := make(#soa[dynamic][3]int, 2, 2)
|
||||||
|
defer delete(array)
|
||||||
|
array[0] = [3]int{1, 2, 3}
|
||||||
|
array[1] = [3]int{4, 5, 6}
|
||||||
|
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
testing.expect_value(t, len(array), 2)
|
||||||
|
testing.expect_value(t, cap(array), 2)
|
||||||
|
|
||||||
|
err = resize(&array, 4)
|
||||||
|
|
||||||
|
testing.expect_value(t, err, nil)
|
||||||
|
testing.expect_value(t, len(array), 4)
|
||||||
|
testing.expect_value(t, cap(array), 4)
|
||||||
|
|
||||||
|
testing.expect_value(t, array[0], [3]int{1, 2, 3})
|
||||||
|
testing.expect_value(t, array[1], [3]int{4, 5, 6})
|
||||||
|
testing.expect_value(t, array[2], [3]int{0, 0, 0})
|
||||||
|
testing.expect_value(t, array[3], [3]int{0, 0, 0})
|
||||||
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_memory_equal :: proc(t: ^testing.T) {
|
test_memory_equal :: proc(t: ^testing.T) {
|
||||||
data: [256]u8
|
data: [256]u8
|
||||||
|
|||||||
Reference in New Issue
Block a user