builtin: make array.ensure_cap/1 more robust

pull/7416/head
Delyan Angelov 2020-12-19 21:35:53 +02:00
parent d782db8878
commit cd30dcc287
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 7 additions and 4 deletions

View File

@ -91,15 +91,18 @@ fn (mut a array) ensure_cap(required int) {
if required <= a.cap {
return
}
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
mut cap := if a.cap > 0 { a.cap } else { 2 }
for required > cap {
cap *= 2
}
if a.cap == 0 {
a.data = vcalloc(cap * a.element_size)
new_size := cap * a.element_size
mut new_data := byteptr(0)
if a.cap > 0 {
new_data = v_realloc(a.data, new_size)
} else {
a.data = v_realloc(a.data, cap * a.element_size)
new_data = vcalloc(new_size)
}
a.data = new_data
a.cap = cap
}