diff options
Diffstat (limited to 'alloc.c')
-rw-r--r-- | alloc.c | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -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 | |||
10 | typedef union { char irrelevant[ALIGNMENT]; double d; } aligned; | ||
11 | static aligned realspace[SPACE / ALIGNMENT]; | ||
12 | #define space ((char *) realspace) | ||
13 | static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ | ||
14 | |||
15 | /*@null@*//*@out@*/char *alloc(n) | ||
16 | unsigned 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 | |||
26 | void alloc_free(x) | ||
27 | char *x; | ||
28 | { | ||
29 | if (x >= space) | ||
30 | if (x < space + SPACE) | ||
31 | return; /* XXX: assuming that pointers are flat */ | ||
32 | free(x); | ||
33 | } | ||