Pristine Ack-5.5
[Ack-5.5.git] / mach / proto / ncg / salloc.c
1 #ifndef NORCSID
2 static char rcsid[] = "$Id: salloc.c,v 0.5 1994/06/24 13:28:05 ceriel Exp $";
3 #endif
4
5 #include "assert.h"
6 #include "param.h"
7 #include "tables.h"
8 #include "types.h"
9 #include <cgg_cg.h>
10 #include "data.h"
11 #include "result.h"
12 #include "extern.h"
13
14 /*
15  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
16  * See the copyright notice in the ACK home directory, in the file "Copyright".
17  *
18  * Author: Hans van Staveren
19  */
20
21 /*
22  * Package for string allocation and garbage collection.
23  * Call salloc(size) to get room for string.
24  * Every now and then call garbage_collect() from toplevel.
25  */
26
27 #define MAXSTAB         1500
28 #define THRESHOLD       200
29
30 char *stab[MAXSTAB];
31 int nstab=0;
32 string malloc();
33
34 string myalloc(size) {
35         register string p;
36
37         p = (string) malloc((unsigned)size);
38         if (p==0)
39                 fatal("Out of memory");
40         return(p);
41 }
42
43 myfree(p) string p; {
44
45         free(p);
46 }
47
48 popstr(nnstab) {
49         register i;
50
51         for (i=nnstab;i<nstab;i++)
52                 myfree(stab[i]);
53         nstab = nnstab;
54 }
55
56 char *salloc(size) {
57         register char *p;
58
59         if (nstab==MAXSTAB)
60                 fatal("String table overflow");
61         p = myalloc(size+1);    /* extra room for terminating zero */
62         stab[nstab++] = p;
63         return(p);
64 }
65
66 compar(p1,p2) char **p1,**p2; {
67
68         assert(*p1 != *p2);
69         if (*p1 < *p2)
70                 return(-1);
71         return(1);
72 }
73
74 garbage_collect() {
75         register i;
76         struct emline *emlp;
77         token_p tp;
78         tkdef_p tdp;
79         struct reginfo *rp;
80         register char **fillp,**scanp;
81         char used[MAXSTAB];     /* could be bitarray */
82
83         if (nstab<THRESHOLD)
84                 return;
85         qsort((char *)stab,nstab,sizeof (char *),compar);
86         for (i=0;i<nstab;i++)
87                 used[i]= FALSE;
88         for(emlp=emlines;emlp<emlines+nemlines;emlp++)
89                 chkstr(emlp->em_soper,used);
90         for (tp= fakestack;tp<&fakestack[stackheight];tp++) {
91                 if (tp->t_token== -1)
92                         continue;
93                 tdp = &tokens[tp->t_token];
94                 for (i=0;i<TOKENSIZE;i++)
95                         if (tdp->t_type[i] == EV_ADDR)
96                                 chkstr(tp->t_att[i].aa.ea_str,used);
97         }
98         for (rp= machregs+1; rp<machregs+NREGS; rp++) {
99                 tp = &rp->r_contents;
100                 assert(tp->t_token != -1);
101                 tdp= &tokens[tp->t_token];
102                 for (i=0;i<TOKENSIZE;i++)
103                         if (tdp->t_type[i] == EV_ADDR)
104                                 chkstr(tp->t_att[i].aa.ea_str,used);
105         }
106         for (i=0;i<nstab;i++)
107                 if (!used[i]) {
108                         myfree(stab[i]);
109                         stab[i]=0;
110                 }
111         fillp=stab;
112         for (scanp=stab;scanp<stab+nstab;scanp++)
113                 if (*scanp != 0)
114                         *fillp++ = *scanp;
115         nstab = fillp-stab;
116 }
117
118 chkstr(str,used) string str; char used[]; {
119         register low,middle,high;
120
121         low=0; high=nstab-1;
122         while (high>low) {
123                 middle= (low+high)>>1;
124                 if (str==stab[middle]) {
125                         used[middle]=1;
126                         return;
127                 }
128                 if (str<stab[middle])
129                         high = middle-1;
130                 else
131                         low = middle+1;
132         }
133         if (low==high) {
134                 if (str==stab[low]) {
135                         used[low]=1;
136                 }
137                 return;
138         }
139 }