-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.c
57 lines (50 loc) · 1.14 KB
/
alloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* $Log: alloc.c,v $
* Revision 1.1 2014-08-18 10:17:41+05:30 Cprogrammer
* Initial revision
*
*
*/
#include "alloc.h"
#include "error.h"
#include <stdlib.h>
#define ALIGNMENT 16 /*- XXX: assuming that this alignment is enough */
#define SPACE 4096 /*- must be multiple of ALIGNMENT */
#ifndef lint
static char sccsid[] = "$Id: alloc.c,v 1.1 2014-08-18 10:17:41+05:30 Cprogrammer Exp mbhangui $";
#endif
typedef union
{
char irrelevant[ALIGNMENT];
double d;
} aligned;
static aligned realspace[SPACE / ALIGNMENT];
#define space ((char *) realspace)
static unsigned int avail = SPACE; /*- multiple of ALIGNMENT; 0<=avail<=SPACE */
/*@null@*//*@out@*/char *alloc(n)
unsigned int n;
{
char *x;
unsigned int m = n;
if ((n = ALIGNMENT + n - (n & (ALIGNMENT - 1))) < m) /*- handle overflow */
{
errno = error_nomem;
return 0;
}
if (n <= avail)
{
avail -= n;
return space + avail;
}
if (!(x = malloc(n)))
errno = error_nomem;
return x;
}
void
alloc_free(x)
char *x;
{
if (x >= space && (x < space + SPACE))
return; /*- XXX: assuming that pointers are flat */
free(x);
}