mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
arrays: replace C.memcpy with vmemcpy calls, to make v -shared -Wimpure-v vlib/arrays/
pass (#17357)
This commit is contained in:
parent
8a1e90fff1
commit
79b2c34f97
@ -509,17 +509,18 @@ pub fn rotate_right[T](mut array []T, k int) {
|
||||
|
||||
[unsafe]
|
||||
fn ptr_rotate[T](left_ int, mid &T, right_ int) {
|
||||
sz := usize(sizeof(T))
|
||||
mut left := usize(left_)
|
||||
mut right := usize(right_)
|
||||
limit := raw_array_cap[T]()
|
||||
for {
|
||||
delta := if left < right { left } else { right }
|
||||
|
||||
if delta <= raw_array_cap[T]() {
|
||||
if delta <= usize(limit) {
|
||||
break
|
||||
}
|
||||
unsafe {
|
||||
swap_nonoverlapping[T](&T(usize(voidptr(mid)) - left * usize(sizeof(T))),
|
||||
&T(usize(voidptr(mid)) + usize(right - delta) * usize(sizeof(T))), int(delta))
|
||||
swap_nonoverlapping[T](&T(usize(voidptr(mid)) - left * sz), &T(usize(voidptr(mid)) +
|
||||
usize(right - delta) * sz), int(delta))
|
||||
}
|
||||
if left <= right {
|
||||
right -= delta
|
||||
@ -527,21 +528,21 @@ fn ptr_rotate[T](left_ int, mid &T, right_ int) {
|
||||
left -= delta
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
sz := usize(sizeof(T))
|
||||
rawarray := C.malloc(raw_array_malloc_size[T]())
|
||||
rawarray := malloc(raw_array_malloc_size[T]())
|
||||
defer {
|
||||
free(rawarray)
|
||||
}
|
||||
dim := &T(usize(voidptr(mid)) - left * sz + right * sz)
|
||||
if left <= right {
|
||||
C.memcpy(rawarray, voidptr(usize(voidptr(mid)) - left * sz), left * sz)
|
||||
C.memmove(voidptr(usize(voidptr(mid)) - left * sz), voidptr(mid), right * sz)
|
||||
C.memcpy(voidptr(dim), rawarray, left * sz)
|
||||
vmemcpy(rawarray, voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz))
|
||||
vmemmove(voidptr(usize(voidptr(mid)) - left * sz), voidptr(mid), isize(right * sz))
|
||||
vmemcpy(voidptr(dim), rawarray, isize(left * sz))
|
||||
} else {
|
||||
C.memcpy(rawarray, voidptr(mid), right * sz)
|
||||
C.memmove(voidptr(dim), voidptr(usize(voidptr(mid)) - left * sz), left * sz)
|
||||
C.memcpy(voidptr(usize(voidptr(mid)) - left * sz), rawarray, right * sz)
|
||||
vmemcpy(rawarray, voidptr(mid), isize(right * sz))
|
||||
vmemmove(voidptr(dim), voidptr(usize(voidptr(mid)) - left * sz), isize(left * sz))
|
||||
vmemcpy(voidptr(usize(voidptr(mid)) - left * sz), rawarray, isize(right * sz))
|
||||
}
|
||||
C.free(rawarray)
|
||||
}
|
||||
}
|
||||
|
||||
@ -561,54 +562,54 @@ mut:
|
||||
w u64
|
||||
}
|
||||
|
||||
const (
|
||||
extra_size = 32 * sizeof(usize)
|
||||
)
|
||||
const extra_size = 32 * isize(sizeof(usize))
|
||||
|
||||
fn raw_array_cap[T]() usize {
|
||||
if sizeof(T) > arrays.extra_size {
|
||||
fn raw_array_cap[T]() isize {
|
||||
size := isize(sizeof(T))
|
||||
if size > arrays.extra_size {
|
||||
return 1
|
||||
} else {
|
||||
return arrays.extra_size / sizeof(T)
|
||||
return arrays.extra_size / size
|
||||
}
|
||||
}
|
||||
|
||||
fn raw_array_malloc_size[T]() usize {
|
||||
if sizeof(T) > arrays.extra_size {
|
||||
return usize(sizeof(T)) * 2
|
||||
fn raw_array_malloc_size[T]() isize {
|
||||
size := isize(sizeof(T))
|
||||
if size > arrays.extra_size {
|
||||
return size * 2
|
||||
} else {
|
||||
return 32 * usize(sizeof(usize))
|
||||
return arrays.extra_size
|
||||
}
|
||||
}
|
||||
|
||||
[unsafe]
|
||||
fn memswap(x voidptr, y voidptr, len usize) {
|
||||
block_size := sizeof(Block)
|
||||
block_size := isize(sizeof(Block))
|
||||
|
||||
mut i := usize(0)
|
||||
for i + block_size <= len {
|
||||
for i + usize(block_size) <= len {
|
||||
mut t_ := Block{}
|
||||
t := voidptr(&t_)
|
||||
|
||||
xi := usize(x) + i
|
||||
yi := usize(y) + i
|
||||
unsafe {
|
||||
C.memcpy(t, voidptr(xi), block_size)
|
||||
C.memcpy(voidptr(xi), voidptr(yi), block_size)
|
||||
C.memcpy(t, voidptr(yi), block_size)
|
||||
vmemcpy(t, voidptr(xi), block_size)
|
||||
vmemcpy(voidptr(xi), voidptr(yi), block_size)
|
||||
vmemcpy(t, voidptr(yi), block_size)
|
||||
}
|
||||
i += block_size
|
||||
i += usize(block_size)
|
||||
}
|
||||
if i < len {
|
||||
mut t_ := UnalignedBlock{}
|
||||
t := voidptr(&t_)
|
||||
rem := len - i
|
||||
rem := isize(len - i)
|
||||
xi := usize(x) + i
|
||||
yi := usize(y) + i
|
||||
unsafe {
|
||||
C.memcpy(t, voidptr(xi), rem)
|
||||
C.memcpy(voidptr(xi), voidptr(yi), rem)
|
||||
C.memcpy(voidptr(yi), t, rem)
|
||||
vmemcpy(t, voidptr(xi), rem)
|
||||
vmemcpy(voidptr(xi), voidptr(yi), rem)
|
||||
vmemcpy(voidptr(yi), t, rem)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -633,7 +634,7 @@ pub fn copy[T](mut dst []T, src []T) int {
|
||||
return 0
|
||||
}
|
||||
if can_copy_bits[T]() {
|
||||
blen := min * int(sizeof(T))
|
||||
blen := min * isize(sizeof(T))
|
||||
unsafe { vmemmove(&T(dst.data), src.data, blen) }
|
||||
} else {
|
||||
for i in 0 .. min {
|
||||
@ -657,8 +658,9 @@ fn can_copy_bits[T]() bool {
|
||||
// carray_to_varray copies a C byte array into a V array of type `T`.
|
||||
// See also: `cstring_to_vstring`
|
||||
[unsafe]
|
||||
pub fn carray_to_varray[T](c_array voidptr, c_array_len int) []T {
|
||||
mut v_array := []T{len: c_array_len}
|
||||
unsafe { vmemcpy(v_array.data, c_array, c_array_len * int(sizeof(T))) }
|
||||
pub fn carray_to_varray[T](c_array_data voidptr, items int) []T {
|
||||
mut v_array := []T{len: items}
|
||||
total_size := items * isize(sizeof(T))
|
||||
unsafe { vmemcpy(v_array.data, c_array_data, total_size) }
|
||||
return v_array
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user