ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/palloc.c
Revision: 1.3
Committed: Mon Jul 21 22:30:18 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 1.2: +14 -13 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: palloc.c,v 1.2 2003/06/08 12:03:10 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
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 static int morefree(void);
22
23
24 PRIMITIVE *
25 palloc(void) /* allocate a primitive */
26
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
42 nalloc++;
43 return(p);
44 }
45
46
47
48 void
49 pfree( /* free a primitive */
50 register PRIMITIVE *p
51 )
52 {
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 void
66 plfree( /* free a primitive list */
67 register PLIST *pl
68 )
69 {
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 morefree(void) /* get more free space */
89
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 }