diff --git a/ncd/NCDEvaluator.c b/ncd/NCDEvaluator.c index 43978ec89..1e5ead090 100644 --- a/ncd/NCDEvaluator.c +++ b/ncd/NCDEvaluator.c @@ -200,22 +200,32 @@ static int add_expr_recurser (NCDEvaluator *o, NCDValue *value, NCDValMem *mem, if (!ncd_make_name_indices(o->string_index, NCDValue_VarName(value), &var.varnames, &var.num_names)) { BLog(BLOG_ERROR, "ncd_make_name_indices failed"); - goto fail; + goto fail_var0; } size_t index; - if (!NCDEvaluator__VarVec_AppendValue(&o->vars, var, &index)) { - BLog(BLOG_ERROR, "failed to grow var array"); - BFree(var.varnames); - goto fail; + struct NCDEvaluator__Var *varptr = NCDEvaluator__VarVec_Push(&o->vars, &index); + if (!varptr) { + BLog(BLOG_ERROR, "NCDEvaluator__VarVec_Push failed"); + goto fail_var1; } if (index >= MAX_LOCAL_IDS) { BLog(BLOG_ERROR, "too many variables"); - goto fail; + goto fail_var2; } + *varptr = var; + *out = NCDVal_NewPlaceholder(mem, ((int)index << 1) | 0); + break; + + fail_var2: + NCDEvaluator__VarVec_Pop(&o->vars, NULL); + fail_var1: + BFree(var.varnames); + fail_var0: + goto fail; } break; case NCDVALUE_INVOC: { @@ -253,19 +263,24 @@ static int add_expr_recurser (NCDEvaluator *o, NCDValue *value, NCDValMem *mem, } size_t index; - if (!NCDEvaluator__CallVec_AppendValue(&o->calls, call, &index)) { - BLog(BLOG_ERROR, "failed to grow call array"); + struct NCDEvaluator__Call *callptr = NCDEvaluator__CallVec_Push(&o->calls, &index); + if (!callptr) { + BLog(BLOG_ERROR, "NCDEvaluator__CallVec_Push failed"); goto fail_invoc1; } if (index >= MAX_LOCAL_IDS) { BLog(BLOG_ERROR, "too many variables"); - goto fail; + goto fail_invoc2; } + *callptr = call; + *out = NCDVal_NewPlaceholder(mem, ((int)index << 1) | 1); break; + fail_invoc2: + NCDEvaluator__CallVec_Pop(&o->calls, NULL); fail_invoc1: while (call.num_args-- > 0) { expr_free(&call.args[call.num_args]); diff --git a/structure/Vector_decl.h b/structure/Vector_decl.h index c29be0b60..8e36c9dd8 100644 --- a/structure/Vector_decl.h +++ b/structure/Vector_decl.h @@ -39,9 +39,7 @@ static int Vector_Init (Vector *o, size_t capacity) WARN_UNUSED; static void Vector_Free (Vector *o); static size_t Vector_Count (Vector *o); static VectorElem * Vector_Get (Vector *o, size_t index); -static int Vector_AllocAppend (Vector *o, size_t count, VectorElem **out_ptr) WARN_UNUSED; -static void Vector_DoAppend (Vector *o, size_t count); -static int Vector_AppendValue (Vector *o, VectorElem value, size_t *out_index) WARN_UNUSED; +static int Vector_Reserve (Vector *o, size_t capacity) WARN_UNUSED; static VectorElem * Vector_Push (Vector *o, size_t *out_index) WARN_UNUSED; static VectorElem * Vector_Pop (Vector *o, size_t *out_index); diff --git a/structure/Vector_footer.h b/structure/Vector_footer.h index df438af62..3e561d9aa 100644 --- a/structure/Vector_footer.h +++ b/structure/Vector_footer.h @@ -36,8 +36,6 @@ #undef Vector_Free #undef Vector_Count #undef Vector_Get -#undef Vector_AllocAppend -#undef Vector_DoAppend -#undef Vector_AppendValue +#undef Vector_Reserve #undef Vector_Push #undef Vector_Pop diff --git a/structure/Vector_header.h b/structure/Vector_header.h index da9d18879..61be2294a 100644 --- a/structure/Vector_header.h +++ b/structure/Vector_header.h @@ -37,8 +37,6 @@ #define Vector_Free MERGE(VECTOR_NAME, _Free) #define Vector_Count MERGE(VECTOR_NAME, _Count) #define Vector_Get MERGE(VECTOR_NAME, _Get) -#define Vector_AllocAppend MERGE(VECTOR_NAME, _AllocAppend) -#define Vector_DoAppend MERGE(VECTOR_NAME, _DoAppend) -#define Vector_AppendValue MERGE(VECTOR_NAME, _AppendValue) +#define Vector_Reserve MERGE(VECTOR_NAME, _Reserve) #define Vector_Push MERGE(VECTOR_NAME, _Push) #define Vector_Pop MERGE(VECTOR_NAME, _Pop) diff --git a/structure/Vector_impl.h b/structure/Vector_impl.h index 84652cd71..b71d515b5 100644 --- a/structure/Vector_impl.h +++ b/structure/Vector_impl.h @@ -61,18 +61,16 @@ static VectorElem * Vector_Get (Vector *o, size_t index) return &o->elems[index]; } -static int Vector_AllocAppend (Vector *o, size_t count, VectorElem **out_ptr) +static int Vector_Reserve (Vector *o, size_t capacity) { - ASSERT(count > 0) - - if (count > o->capacity - o->count) { + if (capacity > o->capacity) { size_t new_capacity = o->capacity; do { if (new_capacity > SIZE_MAX / 2) { return 0; } new_capacity = (new_capacity == 0) ? 1 : (2 * new_capacity); - } while (count > new_capacity - o->count); + } while (capacity > new_capacity); VectorElem *new_elems = BAllocArray(new_capacity, sizeof(VectorElem)); if (!new_elems) { @@ -89,43 +87,19 @@ static int Vector_AllocAppend (Vector *o, size_t count, VectorElem **out_ptr) o->capacity = new_capacity; } - if (out_ptr) { - *out_ptr = &o->elems[o->count]; - } - return 1; -} - -static void Vector_DoAppend (Vector *o, size_t count) -{ - ASSERT(count <= o->capacity - o->count) - - o->count += count; -} - -static int Vector_AppendValue (Vector *o, VectorElem value, size_t *out_index) -{ - VectorElem *ptr; - if (!Vector_AllocAppend(o, 1, &ptr)) { - return 0; - } - *ptr = value; - if (out_index) { - *out_index = o->count; - } - Vector_DoAppend(o, 1); return 1; } static VectorElem * Vector_Push (Vector *o, size_t *out_index) { - VectorElem *ptr; - if (!Vector_AllocAppend(o, 1, &ptr)) { + if (o->count == SIZE_MAX || !Vector_Reserve(o, o->count + 1)) { return NULL; } if (out_index) { *out_index = o->count; } - Vector_DoAppend(o, 1); + VectorElem *ptr = &o->elems[o->count]; + o->count++; return ptr; }