1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: palloc.c,v 1.3 2003/07/21 22:30:18 schorsch Exp $"; |
3 |
#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 |
#include "rtio.h" |
14 |
|
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 |
static int morefree(void); |
23 |
|
24 |
|
25 |
PRIMITIVE * |
26 |
palloc(void) /* allocate a primitive */ |
27 |
|
28 |
{ |
29 |
register PRIMITIVE *p; |
30 |
|
31 |
if (maxalloc > 0 && nalloc >= maxalloc) |
32 |
return(NULL); |
33 |
|
34 |
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 |
|
43 |
nalloc++; |
44 |
return(p); |
45 |
} |
46 |
|
47 |
|
48 |
|
49 |
void |
50 |
pfree( /* free a primitive */ |
51 |
register PRIMITIVE *p |
52 |
) |
53 |
{ |
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 |
void |
67 |
plfree( /* free a primitive list */ |
68 |
register PLIST *pl |
69 |
) |
70 |
{ |
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 |
morefree(void) /* get more free space */ |
90 |
|
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 |
} |