|
|
1.1 root 1:
2: #ifndef lint
3: static char sccsid[] = "@(#)alloc.c 1.1 86/02/03 Copyr 1983 Sun Micro";
4: #endif
5:
6: /*
7: * Copyright (c) 1983 by Sun Microsystems, Inc.
8: */
9:
10: #include "as.h"
11: #include "c2.h"
12:
13: char *malloc();
14: void freeoperand();
15: struct oper * freeoper;
16: struct optree * freeoptree;
17:
18:
19: #define NCHUNK 100 /* NODES at a time, for starters */
20: #define MAXCHUNK 1600 /* max value of 'allocsize' parameter */
21: NODE *
22: new(){
23: NODE *p;
24: static int allocsize = NCHUNK;
25: static NODE z = { OP_COMMENT, SUBOP_Z /* rest zero */ };
26:
27: if (!freenodes){
28: /* allocate new ones */
29: register NODE * t;
30: register int nnode;
31: freenodes = (NODE *)malloc( (unsigned)(allocsize * sizeof *p));
32: if (freenodes==NULL)
33: sys_error("cannot allocate %d new nodes", allocsize);
34: /* put all on free list */
35: for (t=freenodes, nnode=allocsize-1; --nnode; t++ )
36: t->forw = t+1;
37: t->forw = NULL;
38: if (allocsize < MAXCHUNK)
39: allocsize += allocsize; /* double allocation for next time */
40: }
41: p = freenodes;
42: freenodes = p->forw;
43: *p = z;
44: p->lineno = line_no;
45: return p;
46: }
47:
48: NODE *
49: addnode( p )
50: register NODE *p;
51: {
52: p->back = first.back;
53: p->back->forw = p;
54: first.back = p;
55: p->forw = &first;
56: return p;
57: }
58:
59: NODE *
60: deletenode( p )
61: register NODE *p;
62: {
63: NODE *pp = p->back;
64: register int i;
65: if (p->op == OP_FIRST)
66: sys_error("deletenode: Freeing First\n");
67: if (ISINSTRUC( p->op ) || ISPSEUDO( p->op ) )
68: for (i=0; i<p->nref; i++)
69: freeoperand( p->ref[i] );
70: pp->forw = p->forw;
71: p->forw->back = pp;
72: p->forw = freenodes;
73: freenodes = p;
74: return pp;
75: }
76:
77:
78: struct oper *
79: newoperand( o )
80: struct oper *o;
81: {
82: struct oper *p;
83: static int allocsize = NCHUNK;
84:
85: if (!freeoper){
86: /* allocate new ones */
87: register struct oper * t;
88: register int nnode;
89: freeoper = (struct oper *)malloc( (unsigned)(allocsize * sizeof *p));
90: if (freeoper==NULL)
91: sys_error("cannot allocate %d new operands", allocsize);
92: /* put all on free list */
93: for (t=freeoper, nnode=allocsize-1; --nnode; t++ )
94: t->nsym_o = t+1;
95: t->nsym_o = NULL;
96: if (allocsize < MAXCHUNK)
97: allocsize += allocsize; /* double allocation for next time */
98: }
99: p = freeoper;
100: freeoper = p->nsym_o;
101: *p = *o;
102: return p;
103: }
104:
105: void
106: freeoperand( o )
107: struct oper *o;
108: {
109: if (o==NULL) return;
110: o->nsym_o = freeoper;
111: freeoper = o;
112: }
113:
114: void
115: freetree( o )
116: struct optree *o;
117: {
118: if (o==NULL) return;
119: o->right_t = (struct oper *)freeoptree;
120: freeoptree = o;
121: }
122:
123: struct optree *
124: newtree()
125: {
126: struct optree *p;
127: static int allocsize = NCHUNK;
128:
129: if (!freeoptree){
130: /* allocate new ones */
131: register struct optree * t;
132: register int nnode;
133: freeoptree = (struct optree *)malloc( (unsigned)(allocsize * sizeof *p));
134: if (freeoptree==NULL)
135: sys_error("cannot allocate %d new operand subtrees", allocsize);
136: /* put all on free list */
137: for (t=freeoptree, nnode=allocsize-1; --nnode; t++ )
138: t->right_t = (struct oper *)(t+1);
139: t->right_t = NULL;
140: if (allocsize < MAXCHUNK)
141: allocsize += allocsize; /* double allocation for next time */
142: }
143: p = freeoptree;
144: freeoptree = (struct optree *)(p->right_t);
145: return p;
146: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.