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

pull/12298/head
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
1 changed files with 7 additions and 3 deletions

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_
} }