summaryrefslogtreecommitdiff
path: root/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'alloc.c')
-rw-r--r--alloc.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/alloc.c b/alloc.c
new file mode 100644
index 0000000..c741aa4
--- /dev/null
+++ b/alloc.c
@@ -0,0 +1,33 @@
1/* Public domain. */
2
3#include <stdlib.h>
4#include "alloc.h"
5#include "error.h"
6
7#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */
8#define SPACE 2048 /* must be multiple of ALIGNMENT */
9
10typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
11static aligned realspace[SPACE / ALIGNMENT];
12#define space ((char *) realspace)
13static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
14
15/*@null@*//*@out@*/char *alloc(n)
16unsigned int n;
17{
18 char *x;
19 n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
20 if (n <= avail) { avail -= n; return space + avail; }
21 x = malloc(n);
22 if (!x) errno = error_nomem;
23 return x;
24}
25
26void alloc_free(x)
27char *x;
28{
29 if (x >= space)
30 if (x < space + SPACE)
31 return; /* XXX: assuming that pointers are flat */
32 free(x);
33}