Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ambrop72/badvpn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmbroz Bizjak <ambrop7@gmail.com>2014-11-09 12:10:28 +0300
committerAmbroz Bizjak <ambrop7@gmail.com>2014-11-09 12:10:28 +0300
commit194edaec181a34be8dca37f45ecf7693d6b149de (patch)
treebd05ce75194750ab36b41f746ed974f16a001c7b
parent3f83393ac45b2987d8a1bf142747a6a0c261350d (diff)
structure: Vector: Reduce interface.
-rw-r--r--ncd/NCDEvaluator.c33
-rw-r--r--structure/Vector_decl.h4
-rw-r--r--structure/Vector_footer.h4
-rw-r--r--structure/Vector_header.h4
-rw-r--r--structure/Vector_impl.h38
5 files changed, 33 insertions, 50 deletions
diff --git a/ncd/NCDEvaluator.c b/ncd/NCDEvaluator.c
index 43978ec..1e5ead0 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 c29be0b..8e36c9d 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 df438af..3e561d9 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 da9d188..61be229 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 84652cd..b71d515 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;
}