Lines 1-7
Link Here
|
|
|
1 |
#include <stdlib.h> |
1 |
#include "alloc.h" |
2 |
#include "alloc.h" |
2 |
#include "error.h" |
3 |
#include "error.h" |
3 |
extern char *malloc(); |
|
|
4 |
extern void free(); |
5 |
|
4 |
|
6 |
#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */ |
5 |
#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */ |
7 |
#define SPACE 4096 /* must be multiple of ALIGNMENT */ |
6 |
#define SPACE 4096 /* must be multiple of ALIGNMENT */ |
Lines 11-25
static aligned realspace[SPACE / ALIGNMENT];
Link Here
|
11 |
#define space ((char *) realspace) |
10 |
#define space ((char *) realspace) |
12 |
static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ |
11 |
static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ |
13 |
|
12 |
|
|
|
13 |
static char *m_alloc(unsigned int n) |
14 |
{ |
15 |
char *x = malloc(n); |
16 |
if (!x) errno = error_nomem; |
17 |
return x; |
18 |
} |
19 |
|
14 |
/*@null@*//*@out@*/char *alloc(n) |
20 |
/*@null@*//*@out@*/char *alloc(n) |
15 |
unsigned int n; |
21 |
unsigned int n; |
16 |
{ |
22 |
{ |
17 |
char *x; |
23 |
if (n >= SPACE) |
18 |
n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */ |
24 |
return m_alloc(n); |
|
|
25 |
/* Round it up to the next multiple of alignment. Could overflow if n is |
26 |
* close to 2**32, but by the check above this is already ruled out. */ |
27 |
n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); |
19 |
if (n <= avail) { avail -= n; return space + avail; } |
28 |
if (n <= avail) { avail -= n; return space + avail; } |
20 |
x = malloc(n); |
29 |
return m_alloc(n); |
21 |
if (!x) errno = error_nomem; |
|
|
22 |
return x; |
23 |
} |
30 |
} |
24 |
|
31 |
|
25 |
void alloc_free(x) |
32 |
void alloc_free(x) |