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