cgen: dont preallocate zero length with capacity of 1

pull/10215/head
Joe Conigliaro 2021-05-26 14:51:20 +10:00
parent cf07375d1b
commit 560301dbfe
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 4 additions and 6 deletions

View File

@ -95,13 +95,11 @@ fn (mut a array) ensure_cap(required int) {
cap *= 2 cap *= 2
} }
new_size := cap * a.element_size new_size := cap * a.element_size
mut new_data := &byte(0) a.data = if a.data != voidptr(0) {
if a.data != voidptr(0) { unsafe { realloc_data(a.data, a.cap * a.element_size, new_size) }
new_data = unsafe { realloc_data(a.data, a.cap * a.element_size, new_size) }
} else { } else {
new_data = vcalloc(new_size) vcalloc(new_size)
} }
a.data = new_data
a.cap = cap a.cap = cap
} }

View File

@ -5978,7 +5978,7 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
elem_type_str = elem_type_str[3..] elem_type_str = elem_type_str[3..]
} }
noscan := g.check_noscan(elem_typ) noscan := g.check_noscan(elem_typ)
init_str := '__new_array${noscan}(0, 1, sizeof($elem_type_str))' init_str := '__new_array${noscan}(0, 0, sizeof($elem_type_str))'
if typ.has_flag(.shared_f) { if typ.has_flag(.shared_f) {
atyp := '__shared__Array_${g.table.get_type_symbol(elem_typ).cname}' atyp := '__shared__Array_${g.table.get_type_symbol(elem_typ).cname}'
return '($atyp*)__dup_shared_array(&($atyp){.mtx = {0}, .val =$init_str}, sizeof($atyp))' return '($atyp*)__dup_shared_array(&($atyp){.mtx = {0}, .val =$init_str}, sizeof($atyp))'