release
[hcoop/zz_old/debian/djbdns.git] / alloc.c
1 #include <stdlib.h>
2 #include "alloc.h"
3 #include "error.h"
4
5 #define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */
6 #define SPACE 2048 /* must be multiple of ALIGNMENT */
7
8 typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
9 static aligned realspace[SPACE / ALIGNMENT];
10 #define space ((char *) realspace)
11 static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
12
13 /*@null@*//*@out@*/char *alloc(n)
14 unsigned int n;
15 {
16 char *x;
17 n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
18 if (n <= avail) { avail -= n; return space + avail; }
19 x = malloc(n);
20 if (!x) errno = error_nomem;
21 return x;
22 }
23
24 void alloc_free(x)
25 char *x;
26 {
27 if (x >= space)
28 if (x < space + SPACE)
29 return; /* XXX: assuming that pointers are flat */
30 free(x);
31 }