cgit

commit 958a95b37891098133369e835a2ab687e0dfa9dc

Author: Lars Hjemli <hjemli@gmail.com>

Add vector utility functions

Signed-off-by: Lars Hjemli <hjemli@gmail.com>

 Makefile | 1 +
 vector.c | 38 ++++++++++++++++++++++++++++++++++++++
 vector.h | 17 +++++++++++++++++


diff --git a/Makefile b/Makefile
index be1d7961b0a3bc5f36fedbb4df6c6cd71e5bae05..31d88ef4defbbde46b6928cda7f7760e87a72803 100644
--- a/Makefile
+++ b/Makefile
@@ -103,6 +103,7 @@ OBJECTS += ui-stats.o
 OBJECTS += ui-summary.o
 OBJECTS += ui-tag.o
 OBJECTS += ui-tree.o
+OBJECTS += vector.o
 
 ifdef NEEDS_LIBICONV
 	EXTLIBS += -liconv




diff --git a/vector.c b/vector.c
new file mode 100644
index 0000000000000000000000000000000000000000..086390893775686a7ea21fc24da8b8ae7eb049e1
--- /dev/null
+++ b/vector.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include "vector.h"
+
+static int grow(struct vector *vec, int gently)
+{
+	size_t new_alloc;
+	void *new_data;
+
+	new_alloc = vec->alloc * 3 / 2;
+	if (!new_alloc)
+		new_alloc = 8;
+	new_data = realloc(vec->data, new_alloc * vec->size);
+	if (!new_data) {
+		if (gently)
+			return ENOMEM;
+		perror("vector.c:grow()");
+		exit(1);
+	}
+	vec->data = new_data;
+	vec->alloc = new_alloc;
+	return 0;
+}
+
+int vector_push(struct vector *vec, const void *data, int gently)
+{
+	int rc;
+
+	if (vec->count == vec->alloc && (rc = grow(vec, gently)))
+		return rc;
+	if (data)
+		memmove(vec->data + vec->count * vec->size, data, vec->size);
+	else
+		memset(vec->data + vec->count * vec->size, 0, vec->size);
+	vec->count++;
+	return 0;
+}




diff --git a/vector.h b/vector.h
new file mode 100644
index 0000000000000000000000000000000000000000..c64eb1fe90d1f0232750f40d1c55d36a43da1b7c
--- /dev/null
+++ b/vector.h
@@ -0,0 +1,17 @@
+#ifndef CGIT_VECTOR_H
+#define CGIT_VECTOR_H
+
+#include <stdlib.h>
+
+struct vector {
+	size_t size;
+	size_t count;
+	size_t alloc;
+	void *data;
+};
+
+#define VECTOR_INIT(type) {sizeof(type), 0, 0, NULL}
+
+int vector_push(struct vector *vec, const void *data, int gently);
+
+#endif /* CGIT_VECTOR_H */