ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/palloc.c
Revision: 1.4
Committed: Sat Nov 15 02:13:37 2003 UTC (20 years, 5 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R2, rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1, rad5R3, HEAD
Changes since 1.3: +2 -1 lines
Log Message:
Continued ANSIfication, and reduced other compile warnings.

File Contents

# Content
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 }