1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: use malloc instead of calloc for []Type{cap: size}

This commit is contained in:
Delyan Angelov 2021-10-28 17:18:03 +03:00
parent 2c3e44eb20
commit 4d0f6767b1
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -34,10 +34,14 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
cap_ := if cap < mylen { mylen } else { cap } cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{ mut arr := array{
element_size: elm_size element_size: elm_size
data: vcalloc(cap_ * elm_size)
len: mylen len: mylen
cap: cap_ cap: cap_
} }
if cap_ > 0 && mylen == 0 {
arr.data = unsafe { malloc(cap_ * elm_size) }
} else {
arr.data = vcalloc(cap_ * elm_size)
}
if val != 0 { if val != 0 {
for i in 0 .. arr.len { for i in 0 .. arr.len {
unsafe { arr.set_unsafe(i, val) } unsafe { arr.set_unsafe(i, val) }
@ -50,7 +54,7 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) a
cap_ := if cap < mylen { mylen } else { cap } cap_ := if cap < mylen { mylen } else { cap }
mut arr := array{ mut arr := array{
element_size: elm_size element_size: elm_size
data: vcalloc(cap_ * elm_size) data: unsafe { malloc(cap_ * elm_size) }
len: mylen len: mylen
cap: cap_ cap: cap_
} }
@ -66,7 +70,7 @@ fn new_array_from_c_array(len int, cap int, elm_size int, c_array voidptr) array
cap_ := if cap < len { len } else { cap } cap_ := if cap < len { len } else { cap }
arr := array{ arr := array{
element_size: elm_size element_size: elm_size
data: vcalloc(cap_ * elm_size) data: unsafe { malloc(cap_ * elm_size) }
len: len len: len
cap: cap_ cap: cap_
} }