summaryrefslogtreecommitdiff
path: root/vector.c
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2017-01-17 20:14:45 -0600
committerBobby Bingham <koorogi@koorogi.info>2017-01-17 23:00:52 -0600
commit9244eacb5ab0b9a11480d1ad40d4720575676877 (patch)
treec49c20b4c693c6676d49f35b6c1217131dd4c0da /vector.c
parentbb7ae08ee638cf9b88a69ca89c5b6b12401c37f5 (diff)
nqasm: parse labels
Diffstat (limited to 'vector.c')
-rw-r--r--vector.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/vector.c b/vector.c
new file mode 100644
index 0000000..57f62d7
--- /dev/null
+++ b/vector.c
@@ -0,0 +1,37 @@
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "vector.h"
+
+#define VECTOR_TOOBIG(alloc,elemsize,arrayoff) ((SIZE_MAX - (arrayoff)) / (alloc) < (elemsize))
+#define VECTOR_ALLOCSIZE(alloc,elemsize,arrayoff) ((arrayoff) + (alloc) * (elemsize))
+
+void *vector_init(size_t alloc, size_t elemsize, size_t arrayoff)
+{
+ if (VECTOR_TOOBIG(alloc, elemsize, arrayoff)) return NULL;
+ struct vector *v = malloc(VECTOR_ALLOCSIZE(alloc, elemsize, arrayoff));
+ if (!v) return NULL;
+
+ *v = (struct vector) {
+ .alloc = alloc,
+ .elemsize = elemsize,
+ .arrayoff = arrayoff,
+ };
+ return v;
+}
+
+void *vector_append(struct vector **vp)
+{
+ struct vector *v = *vp;
+ if (v->count >= v->alloc) {
+ size_t alloc = 2 * v->alloc;
+ if (VECTOR_TOOBIG(alloc, v->elemsize, v->arrayoff)) return NULL;
+ v = realloc(v, VECTOR_ALLOCSIZE(alloc, v->elemsize, v->arrayoff));
+ if (!v) return NULL;
+ v->alloc = alloc;
+ *vp = v;
+ }
+ return (char*)v + v->arrayoff + v->count++ * v->elemsize;
+}
+