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