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