summaryrefslogtreecommitdiff
path: root/util/alloc.c
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-02-22 21:14:36 -0800
committerOri Bernstein <ori@eigenstate.org>2016-02-22 21:14:36 -0800
commit8583926c2ca8407a025991f65287bd693dce9060 (patch)
tree17bbb66dae1bd8b577858e68385854eba27f498f /util/alloc.c
parent100ed8406343f027aa442e79fe59c653c8fc02a4 (diff)
downloadmc-8583926c2ca8407a025991f65287bd693dce9060.tar.gz
Move more stuff around in util code.
Diffstat (limited to 'util/alloc.c')
-rw-r--r--util/alloc.c135
1 files changed, 135 insertions, 0 deletions
diff --git a/util/alloc.c b/util/alloc.c
new file mode 100644
index 0000000..662c452
--- /dev/null
+++ b/util/alloc.c
@@ -0,0 +1,135 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <string.h>
+#include <assert.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "util.h"
+
+/* Some systems don't have strndup. */
+/* malloc wrappers */
+void *zalloc(size_t sz)
+{
+ void *mem;
+
+ mem = calloc(1, sz);
+ if (!mem && sz)
+ die("Out of memory");
+ return mem;
+}
+
+void *xalloc(size_t sz)
+{
+ void *mem;
+
+ mem = malloc(sz);
+ if (!mem && sz)
+ die("Out of memory");
+ return mem;
+}
+
+void *zrealloc(void *mem, size_t oldsz, size_t sz)
+{
+ char *p;
+
+ p = xrealloc(mem, sz);
+ if (sz > oldsz)
+ memset(&p[oldsz], 0, sz - oldsz);
+ return p;
+}
+
+void *xrealloc(void *mem, size_t sz)
+{
+ mem = realloc(mem, sz);
+ if (!mem && sz)
+ die("Out of memory");
+ return mem;
+}
+
+/* lists */
+void lappend(void *l, size_t *len, void *n)
+{
+ void ***pl;
+
+ pl = l;
+ *pl = xrealloc(*pl, (*len + 1) * sizeof(void *));
+ (*pl)[*len] = n;
+ (*len)++;
+}
+
+void *lpop(void *l, size_t *len)
+{
+ void ***pl;
+ void *v;
+
+ pl = l;
+ (*len)--;
+ v = (*pl)[*len];
+ *pl = xrealloc(*pl, *len * sizeof(void *));
+ return v;
+}
+
+void linsert(void *p, size_t *len, size_t idx, void *v)
+{
+ void ***pl, **l;
+
+ pl = p;
+ *pl = xrealloc(*pl, (*len + 1) * sizeof(void *));
+ l = *pl;
+
+ memmove(&l[idx + 1], &l[idx], (*len - idx) * sizeof(void *));
+ l[idx] = v;
+ (*len)++;
+}
+
+void ldel(void *p, size_t *len, size_t idx)
+{
+ void ***pl, **l;
+
+ assert(p != NULL);
+ assert(idx < *len);
+ pl = p;
+ l = *pl;
+ memmove(&l[idx], &l[idx + 1], (*len - idx - 1) * sizeof(void *));
+ (*len)--;
+ *pl = xrealloc(l, *len * sizeof(void *));
+}
+
+void lcat(void *dst, size_t *ndst, void *src, size_t nsrc)
+{
+ size_t i;
+ void ***d, **s;
+
+ d = dst;
+ s = src;
+ for (i = 0; i < nsrc; i++)
+ lappend(d, ndst, s[i]);
+}
+
+void lfree(void *l, size_t *len)
+{
+ void ***pl;
+
+ assert(l != NULL);
+ pl = l;
+ free(*pl);
+ *pl = NULL;
+ *len = 0;
+}
+
+void *memdup(void *mem, size_t len)
+{
+ void *ret;
+
+ if (!mem)
+ return NULL;
+ ret = xalloc(len);
+ return memcpy(ret, mem, len);
+}