commit 21844ce80c24e84e7fb5d4ba599253aeb9e54cbf
Author: ssnf <ssnf@ssnf.xyz>
Date: Fri, 29 Mar 2024 22:51:23 +0000
initial commit
Diffstat:
A | .gitignore | | | 3 | +++ |
A | Makefile | | | 29 | +++++++++++++++++++++++++++++ |
A | config.mk | | | 11 | +++++++++++ |
A | vec.c | | | 127 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | vec.def.pc | | | 5 | +++++ |
A | vec.h | | | 28 | ++++++++++++++++++++++++++++ |
6 files changed, 203 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+*.a
+*.o
+vec.pc
diff --git a/Makefile b/Makefile
@@ -0,0 +1,29 @@
+include config.mk
+
+all: libvec.a vec.pc
+
+.c.o: vec.h
+ ${CC} ${CFLAGS} -c $<
+
+libvec.a: vec.o
+ ${AR} ${ARFLAGS} $@ vec.o
+
+vec.pc: vec.def.pc config.mk
+ sed -e 's|_PREFIX|'${PREFIX}'|g' \
+ -e 's|_VERSION|'${VERSION}'|g' \
+ vec.def.pc > $@
+
+install: libvec.a vec.pc
+ install -Dm644 vec.h ${PREFIX}/include
+ install -Dm644 libvec.a ${PREFIX}/lib
+ install -Dm644 vec.pc ${PKG_CONFIG_PATH}
+
+uninstall:
+ rm -f ${PREFIX}/include/vec.h
+ rm -f ${PREFIX}/lib/libvec.a
+ rm -f ${PKG_CONFIG_PATH}/vec.pc
+
+clean:
+ rm -f vec.o libvec.a vec.pc
+
+.PHONY: all clean install uninstall
diff --git a/config.mk b/config.mk
@@ -0,0 +1,11 @@
+VERSION = 0.1
+
+PREFIX = /usr/local
+PKG_CONFIG_PATH = /usr/lib/pkgconfig
+
+ARFLAGS = rc
+#CFLAGS = -Os -s -ansi -Wall -Wpedantic
+CFLAGS = -O0 -g -ansi -Wall -Wpedantic
+
+AR = ar
+CC = cc
diff --git a/vec.c b/vec.c
@@ -0,0 +1,127 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "vec.h"
+
+typedef unsigned char uchar;
+typedef unsigned short ushort;
+typedef unsigned long ulong;
+
+static void*
+emalloc(ulong n)
+{
+ void* p;
+
+ p = malloc(n);
+ if (p == NULL) {
+ perror("libvec vec.c:emalloc()");
+ abort();
+ }
+ return p;
+}
+
+static void*
+erealloc(void* p, ulong n)
+{
+ p = realloc(p, n);
+ if (p == NULL) {
+ perror("libvec vec.c:erealloc()");
+ abort();
+ }
+ return p;
+}
+
+Vector*
+Vec(void* p)
+{
+ return (Vector*)p - 1;
+}
+
+void*
+Vecadd_(void** p)
+{
+ Vector* v;
+
+ Vecinsure(p, Vecsiz(*p) + 1);
+ v = Vec(*p);
+ if (v->init != NULL)
+ v->init((char*)*p + v->n * v->ms);
+ return (char*)*p + v->n++ * v->ms;
+}
+
+void
+Vecclose_(void** p)
+{
+ Vector* v;
+
+ v = Vec(*p);
+ if (v->close != NULL)
+ for (;v->n;)
+ v->close((char*)*p + --v->n * v->ms);
+ free(v);
+ *p = NULL;
+}
+
+void
+Vecdel_(void** p)
+{
+ Vector* v;
+
+ v = Vec(*p);
+ if (!v->n)
+ return;
+ if (v->close != NULL)
+ v->close((char*)*p + (v->n - 1) * v->ms);
+ memset((char*)*p + --v->n * v->ms, 0, v->ms);
+}
+
+void
+Vecinit_(void** p, ulong ms, void (*init)(), void (*close)())
+{
+ Vector* v;
+
+ v = emalloc(sizeof(*v));
+ v->init = init;
+ v->close = close;
+ v->ms = ms;
+ v->n = 0;
+ v->size = 0;
+ *p = v + 1;
+}
+
+void
+Vecinsure_(void** p, ulong n)
+{
+ Vector* v;
+
+ v = Vec(*p);
+ if (v->size >= n)
+ return;
+ if (!v->size)
+ v->size = n;
+ else
+ for (; n > v->size; v->size *= 2);
+ v = erealloc(v, sizeof(*v) + v->size * v->ms);
+ *p = v + 1;
+}
+
+ulong
+Vecsiz(void* p)
+{
+ return Vec(p)->n;
+}
+
+void
+Veczero_(void** p)
+{
+ Vector* v;
+
+ v = Vec(*p);
+ if (v->close != NULL)
+ for (;v->n;)
+ v->close((char*)*p + --v->n * v->ms);
+ v = erealloc(v, sizeof(*v));
+ v->size = 0;
+ *p = v + 1;
+}
diff --git a/vec.def.pc b/vec.def.pc
@@ -0,0 +1,5 @@
+Name: vec
+Description: a vector library
+Version: _VERSION
+Cflags: -I_PREFIX/include
+Libs: -L_PREFIX/lib -lvec
diff --git a/vec.h b/vec.h
@@ -0,0 +1,28 @@
+#define Vecadd(p) Vecadd_((void**)p)
+#define Vecaddv(p, a, type) *(type*)Vecadd(p) = a
+#define Vecclose(p) Vecclose_((void**)p)
+#define Vecdel(p) Vecdel_((void**)p)
+#define Vecinit(p) Vecinit_((void**)p, sizeof(*p[0]), NULL, NULL)
+#define Vecinitf(p, i, c) Vecinit_((void**)p, sizeof(*p[0]), i, c)
+#define Vecinsure(p, n) Vecinsure_((void**)p, n);
+#define Veczero(p) Veczero_((void**)p);
+
+typedef struct {
+ void (*init)();
+ void (*close)();
+ unsigned long ms; /*member size*/
+ unsigned long n;
+ unsigned long size;
+} Vector;
+
+/* These take the vector pointer as arguments */
+Vector* Vec(void* p);
+unsigned long Vecsiz(void* p);
+
+/* These take the address of the vector pointer */
+void* Vecadd_(void** p);
+void Vecclose_(void** p);
+void Vecdel_(void** p);
+void Vecinit_(void** p, unsigned long ms, void (*init)(), void (*close)());
+void Vecinsure_(void** p, unsigned long n);
+void Veczero_(void** p);