From 707ab6c2626575cd090a2ba02db17306a25d49ed Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 29 Jan 2025 20:37:39 -0800 Subject: vector: ensure minimum alloc size. Like in a recent commit for mkstring, we impose a minimum allocation size of 6 for vectors, which means 8 cells together with the two informaton words at the base of the vector. * lib.c (vec_own): Take an alloc parameter in addition to the length, which is stored in v[vec_alloc]. (vector): Impose a minimum alloc size of 6. (copy_vec, nested_vec_of_v): Pass alloc parameter to vec_own which is the same as the length parameter; i.e. no behavior change for these functions. --- lib.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib.c b/lib.c index 99674be5..44f2b1ef 100644 --- a/lib.c +++ b/lib.c @@ -9665,7 +9665,7 @@ static val *vec_allocate(ucnum len, val self) return coerce(val *, chk_xalloc(size, sizeof (val), self)); } -static val vec_own(val *v, val length) +static val vec_own(val *v, val length, val alloc) { val vec = make_obj(); @@ -9675,7 +9675,7 @@ static val vec_own(val *v, val length) #endif v += 2; vec->v.vec = v; - v[vec_alloc] = length; + v[vec_alloc] = alloc; v[vec_length] = length; return vec; @@ -9695,8 +9695,9 @@ val vector(val length, val initval) val self = lit("vector"); ucnum len = c_unum(length, self); - val *v = vec_allocate(len, self); - val vec = vec_own(v, length); + ucnum alloc = max(6, len); + val *v = vec_allocate(alloc, self); + val vec = vec_own(v, length, unum(alloc)); vec_init(v, len, initval); return vec; } @@ -9867,7 +9868,7 @@ val copy_vec(val vec_in) val length = length_vec(vec_in); ucnum len = c_unum(length, self); val *v = vec_allocate(len, self); - val vec = vec_own(v, length); + val vec = vec_own(v, length, length); memcpy(v + 2, vec_in->v.vec, len * sizeof *v); return vec; } @@ -10166,7 +10167,7 @@ val nested_vec_of_v(val initval, struct args *args) for (i = 0; i < n; i++) rawvec[i + 2] = nested_vec_of_v(initval, args_copy); - vec = vec_own(rawvec, dim); + vec = vec_own(rawvec, dim, dim); gc_state(gc_save); return vec; } else { -- cgit v1.2.3