Skip to content

Commit

Permalink
structure: Vector: Reduce interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrop72 committed Nov 9, 2014
1 parent 3f83393 commit 194edae
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 50 deletions.
33 changes: 24 additions & 9 deletions ncd/NCDEvaluator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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]);
Expand Down
4 changes: 1 addition & 3 deletions structure/Vector_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions structure/Vector_footer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions structure/Vector_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
38 changes: 6 additions & 32 deletions structure/Vector_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down

0 comments on commit 194edae

Please sign in to comment.