From 220534453eecc2c07ad710d72268aafd20b83138 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 8 May 2015 23:07:47 -0400 Subject: initial commit --- alloc.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 alloc.c (limited to 'alloc.c') diff --git a/alloc.c b/alloc.c new file mode 100644 index 0000000..c741aa4 --- /dev/null +++ b/alloc.c @@ -0,0 +1,33 @@ +/* Public domain. */ + +#include +#include "alloc.h" +#include "error.h" + +#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */ +#define SPACE 2048 /* must be multiple of ALIGNMENT */ + +typedef union { char irrelevant[ALIGNMENT]; double d; } aligned; +static aligned realspace[SPACE / ALIGNMENT]; +#define space ((char *) realspace) +static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ + +/*@null@*//*@out@*/char *alloc(n) +unsigned int n; +{ + char *x; + n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */ + if (n <= avail) { avail -= n; return space + avail; } + x = malloc(n); + if (!x) errno = error_nomem; + return x; +} + +void alloc_free(x) +char *x; +{ + if (x >= space) + if (x < space + SPACE) + return; /* XXX: assuming that pointers are flat */ + free(x); +} -- cgit v1.2.3