1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
1.4 |
static const char RCSid[] = "$Id: palloc.c,v 1.3 2003/07/21 22:30:18 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Limited dynamic storage allocator for primitives |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
|
9 |
|
|
#define FBSIZE 72 /* size of malloc call */ |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
#include "meta.h" |
13 |
schorsch |
1.4 |
#include "rtio.h" |
14 |
greg |
1.1 |
|
15 |
|
|
|
16 |
|
|
extern int maxalloc; /* number of prims to allocate */ |
17 |
|
|
int nalloc = 0; /* number allocated so far */ |
18 |
|
|
|
19 |
|
|
static PLIST freelist = {NULL, NULL}; |
20 |
|
|
|
21 |
|
|
|
22 |
schorsch |
1.2 |
static int morefree(void); |
23 |
greg |
1.1 |
|
24 |
|
|
|
25 |
|
|
PRIMITIVE * |
26 |
schorsch |
1.2 |
palloc(void) /* allocate a primitive */ |
27 |
greg |
1.1 |
|
28 |
|
|
{ |
29 |
schorsch |
1.3 |
register PRIMITIVE *p; |
30 |
greg |
1.1 |
|
31 |
schorsch |
1.3 |
if (maxalloc > 0 && nalloc >= maxalloc) |
32 |
|
|
return(NULL); |
33 |
greg |
1.1 |
|
34 |
schorsch |
1.3 |
if ((p = pop(&freelist)) == NULL) { |
35 |
|
|
if (morefree()) |
36 |
|
|
p = pop(&freelist); |
37 |
|
|
else { |
38 |
|
|
sprintf(errmsg, "out of memory in palloc (nalloc = %d)", nalloc); |
39 |
|
|
error(SYSTEM, errmsg); |
40 |
|
|
} |
41 |
|
|
} |
42 |
greg |
1.1 |
|
43 |
schorsch |
1.3 |
nalloc++; |
44 |
|
|
return(p); |
45 |
schorsch |
1.2 |
} |
46 |
greg |
1.1 |
|
47 |
|
|
|
48 |
|
|
|
49 |
schorsch |
1.2 |
void |
50 |
|
|
pfree( /* free a primitive */ |
51 |
|
|
register PRIMITIVE *p |
52 |
|
|
) |
53 |
greg |
1.1 |
{ |
54 |
|
|
|
55 |
|
|
if (p->args != NULL) { |
56 |
|
|
freestr(p->args); |
57 |
|
|
p->args = NULL; |
58 |
|
|
} |
59 |
|
|
push(p, &freelist); |
60 |
|
|
nalloc--; |
61 |
|
|
|
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
schorsch |
1.2 |
void |
67 |
|
|
plfree( /* free a primitive list */ |
68 |
|
|
register PLIST *pl |
69 |
|
|
) |
70 |
greg |
1.1 |
{ |
71 |
|
|
register PRIMITIVE *p; |
72 |
|
|
|
73 |
|
|
for (p = pl->ptop; p != NULL; p = p->pnext) { |
74 |
|
|
if (p->args != NULL) { |
75 |
|
|
freestr(p->args); |
76 |
|
|
p->args = NULL; |
77 |
|
|
} |
78 |
|
|
nalloc--; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
append(pl, &freelist); |
82 |
|
|
pl->ptop = pl->pbot = NULL; |
83 |
|
|
|
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
static int |
89 |
schorsch |
1.2 |
morefree(void) /* get more free space */ |
90 |
greg |
1.1 |
|
91 |
|
|
{ |
92 |
|
|
register PRIMITIVE *p; |
93 |
|
|
register int i; |
94 |
|
|
int rnu; |
95 |
|
|
|
96 |
|
|
if (maxalloc > 0 && (i = maxalloc-nalloc) < FBSIZE) |
97 |
|
|
rnu = i; |
98 |
|
|
else |
99 |
|
|
rnu = i = FBSIZE; |
100 |
|
|
|
101 |
|
|
p = (PRIMITIVE *)malloc((unsigned)i * sizeof(PRIMITIVE)); |
102 |
|
|
|
103 |
|
|
if (p == NULL) |
104 |
|
|
return(0); |
105 |
|
|
|
106 |
|
|
while (i--) { |
107 |
|
|
p->args = NULL; |
108 |
|
|
push(p, &freelist); |
109 |
|
|
p++; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
return(rnu); |
113 |
|
|
} |