ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/palloc.c
Revision: 1.2
Committed: Sun Jun 8 12:03:10 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 1.1: +13 -26 lines
Log Message:
Reduced compile warnings/errors on Windows.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: palloc.c,v 1.1 2003/02/22 02:07:26 greg 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 nalloc++;
42 return(p);
43 }
44
45
46
47 void
48 pfree( /* free a primitive */
49 register PRIMITIVE *p
50 )
51 {
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 void
65 plfree( /* free a primitive list */
66 register PLIST *pl
67 )
68 {
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 morefree(void) /* get more free space */
88
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 }