release
[hcoop/zz_old/debian/djbdns.git] / alloc.c
CommitLineData
dc0d77d7
CE
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
8typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
9static aligned realspace[SPACE / ALIGNMENT];
10#define space ((char *) realspace)
11static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
12
13/*@null@*//*@out@*/char *alloc(n)
14unsigned 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
24void alloc_free(x)
25char *x;
26{
27 if (x >= space)
28 if (x < space + SPACE)
29 return; /* XXX: assuming that pointers are flat */
30 free(x);
31}