|
|
1.1 root 1: /*
2: * common/talloc.c
3: * C compiler common library.
4: * Tree space sub-allocator.
5: */
6:
7: #ifdef vax
8: #include "INC$LIB:cc0.h"
9: #else
10: #include "cc0.h"
11: #endif
12:
13: /*
14: * Tree space management.
15: */
16: struct tree_space {
17: struct tree_space *ts_link;
18: char *ts_ptr;
19: char *ts_end;
20: char ts_base[];
21: } *ts_bp, *ts_cp;
22: int ts_size;
23:
24: /*
25: * Allocate some space from the tree area.
26: * Always allocate the size of a tree node,
27: * the other uses are too few and far between
28: * to bother with.
29: * Abort the compiler if no space to get.
30: * To make life a lot easier for the rest of the world,
31: * tree space is zeroed.
32: */
33: extern char *malloc();
34: char *talloc()
35: {
36: register char *p;
37: register int n;
38:
39: n = ts_size;
40: if (ts_cp->ts_ptr == ts_cp->ts_end) {
41: if (ts_cp->ts_link == NULL) {
42: if ((ts_cp->ts_link = malloc(sizeof(*ts_bp)+n*128)) == NULL)
43: cfatal("out of space in tree node allocator");
44: ts_cp->ts_link->ts_link = NULL;
45: }
46: ts_cp = ts_cp->ts_link;
47: ts_cp->ts_ptr = ts_cp->ts_base;
48: ts_cp->ts_end = ts_cp->ts_base+n*128;
49: }
50: p = ts_cp->ts_ptr += n;
51: while (--n >= 0) *--p = 0;
52: return (p);
53: }
54:
55: /*
56: * Clear tree space.
57: * If tsize == ts_size then reset the tree allocator.
58: * If tsize != ts_size then free all space allocated
59: * and if tsize != 0 then reallocate with new granularity.
60: * tsize is the size of the tree nodes allocated.
61: */
62: newtree(tsize) int tsize;
63: {
64: if (tsize != ts_size) {
65: while (ts_bp != NULL) {
66: ts_cp = ts_bp;
67: ts_bp = ts_bp->ts_link;
68: free(ts_cp);
69: }
70: if (ts_size = tsize) {
71: if ((ts_bp = malloc(sizeof(*ts_bp) + ts_size*128)) == NULL)
72: cfatal("no space in tree allocator");
73: ts_bp->ts_link = NULL;
74: } else {
75: ts_bp = NULL;
76: }
77: }
78: if (ts_cp = ts_bp) {
79: ts_cp->ts_ptr = ts_cp->ts_base;
80: ts_cp->ts_end = ts_cp->ts_base+ts_size*128;
81: }
82: }
83:
84: /*
85: * Reset tree space to saved location.
86: * Used to parse #if expressions in mid-expression.
87: */
88: treset(p) char *p;
89: {
90: for (ts_cp = ts_bp; ts_cp != NULL; ts_cp = ts_cp->ts_link)
91: if (ts_cp->ts_base <= p && p <= ts_cp->ts_end)
92: break;
93: if (ts_cp == NULL) cbotch("tree reset");
94: ts_cp->ts_ptr = p;
95: }
96:
97: #if OVERLAID
98: /*
99: * Free all of the tree nodes.
100: */
101: freetree()
102: {
103: newtree(0);
104: }
105: #endif
106:
107: /* end of common/talloc.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.