1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
1.3 |
static const char RCSid[] = "$Id: palloc.c,v 1.2 2003/06/08 12:03:10 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 |
|
|
|
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 |
schorsch |
1.3 |
register PRIMITIVE *p; |
29 |
greg |
1.1 |
|
30 |
schorsch |
1.3 |
if (maxalloc > 0 && nalloc >= maxalloc) |
31 |
|
|
return(NULL); |
32 |
greg |
1.1 |
|
33 |
schorsch |
1.3 |
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 |
greg |
1.1 |
|
42 |
schorsch |
1.3 |
nalloc++; |
43 |
|
|
return(p); |
44 |
schorsch |
1.2 |
} |
45 |
greg |
1.1 |
|
46 |
|
|
|
47 |
|
|
|
48 |
schorsch |
1.2 |
void |
49 |
|
|
pfree( /* free a primitive */ |
50 |
|
|
register PRIMITIVE *p |
51 |
|
|
) |
52 |
greg |
1.1 |
{ |
53 |
|
|
|
54 |
|
|
if (p->args != NULL) { |
55 |
|
|
freestr(p->args); |
56 |
|
|
p->args = NULL; |
57 |
|
|
} |
58 |
|
|
push(p, &freelist); |
59 |
|
|
nalloc--; |
60 |
|
|
|
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
schorsch |
1.2 |
void |
66 |
|
|
plfree( /* free a primitive list */ |
67 |
|
|
register PLIST *pl |
68 |
|
|
) |
69 |
greg |
1.1 |
{ |
70 |
|
|
register PRIMITIVE *p; |
71 |
|
|
|
72 |
|
|
for (p = pl->ptop; p != NULL; p = p->pnext) { |
73 |
|
|
if (p->args != NULL) { |
74 |
|
|
freestr(p->args); |
75 |
|
|
p->args = NULL; |
76 |
|
|
} |
77 |
|
|
nalloc--; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
append(pl, &freelist); |
81 |
|
|
pl->ptop = pl->pbot = NULL; |
82 |
|
|
|
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
static int |
88 |
schorsch |
1.2 |
morefree(void) /* get more free space */ |
89 |
greg |
1.1 |
|
90 |
|
|
{ |
91 |
|
|
register PRIMITIVE *p; |
92 |
|
|
register int i; |
93 |
|
|
int rnu; |
94 |
|
|
|
95 |
|
|
if (maxalloc > 0 && (i = maxalloc-nalloc) < FBSIZE) |
96 |
|
|
rnu = i; |
97 |
|
|
else |
98 |
|
|
rnu = i = FBSIZE; |
99 |
|
|
|
100 |
|
|
p = (PRIMITIVE *)malloc((unsigned)i * sizeof(PRIMITIVE)); |
101 |
|
|
|
102 |
|
|
if (p == NULL) |
103 |
|
|
return(0); |
104 |
|
|
|
105 |
|
|
while (i--) { |
106 |
|
|
p->args = NULL; |
107 |
|
|
push(p, &freelist); |
108 |
|
|
p++; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
return(rnu); |
112 |
|
|
} |