Pristine Ack-5.5
[Ack-5.5.git] / util / ego / share / alloc.c
1 /* $Id: alloc.c,v 1.6 1994/06/24 10:29:20 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*  S H A R E D   F I L E
7  *
8  *  A L L O C . C
9  */
10
11
12
13 #include <stdio.h>
14 #include "types.h"
15 #include "debug.h"
16 #include "alloc.h"
17
18
19 char * myalloc();
20 char * calloc();
21
22 #ifdef DEBUG
23
24 STATIC unsigned maxuse, curruse;
25
26 char *newcore(size)
27         int size;
28 {
29         if ((curruse += (unsigned)  (size+2)) > maxuse) maxuse = curruse;
30         return myalloc(size);
31 }
32
33 oldcore(p,size)
34         char *p;
35         int size;
36 {
37         curruse -= (size+2);
38         free(p);
39 }
40
41 coreusage()
42 {
43         fprintf(stderr,"Maximal core usage (excl. buffers):%u\n",maxuse);
44 }
45
46 #endif
47
48
49 /*
50  * The following two sizetables contain the sizes of the various kinds
51  * of line and argument structures.
52  * The assumption when making the tables was that every non-byte object
53  * had to be aligned on an even boundary. On machines where alignment
54  * is worse ( for example a long has to be aligned on a longword bound )
55  * these tables should be revised.
56  * A wasteful but safe approach is to replace every line of them by
57  *  sizeof(line_t)
58  * and
59  *  sizeof(arg_t)
60  * respectively.
61  */
62
63 #ifndef NOTCOMPACT
64 int lsizetab[] = {
65         2*sizeof(line_p)+2*sizeof(byte),
66         2*sizeof(line_p)+2*sizeof(byte)+sizeof(short),
67         2*sizeof(line_p)+2*sizeof(byte)+sizeof(offset),
68         2*sizeof(line_p)+2*sizeof(byte)+sizeof(lab_id),
69         2*sizeof(line_p)+2*sizeof(byte)+sizeof(obj_p),
70         2*sizeof(line_p)+2*sizeof(byte)+sizeof(proc_p),
71         2*sizeof(line_p)+2*sizeof(byte)+sizeof(arg_p),
72 };
73
74 int asizetab[] = {
75         sizeof(arg_p)+sizeof(short)+sizeof(offset),
76         sizeof(arg_p)+sizeof(short)+sizeof(lab_id),
77         sizeof(arg_p)+sizeof(short)+sizeof(obj_p),
78         sizeof(arg_p)+sizeof(short)+sizeof(proc_p),
79         sizeof(arg_p)+sizeof(short)+sizeof(argb_t),
80         sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t),
81         sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t),
82         sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t)
83 };
84 #else
85 int lsizetab[] = {
86         sizeof(struct line),
87         sizeof(struct line),
88         sizeof(struct line),
89         sizeof(struct line),
90         sizeof(struct line),
91         sizeof(struct line),
92         sizeof(struct line)
93 };
94
95 int asizetab[] = {
96         sizeof (struct arg),
97         sizeof (struct arg),
98         sizeof (struct arg),
99         sizeof (struct arg),
100         sizeof (struct arg),
101         sizeof (struct arg),
102         sizeof (struct arg),
103         sizeof (struct arg)
104 };
105 #endif
106
107 /*
108  * alloc routines:
109  * Two parts:
110  *   1) typed alloc and free routines
111  *   2) untyped raw core allocation
112  */
113
114 /*
115  * PART 1
116  */
117
118 line_p  newline(optyp) int optyp; {
119         register line_p lnp;
120         register kind=optyp;
121
122         lnp = (line_p) newcore(lsizetab[kind]);
123         TYPE(lnp) = optyp;
124         return(lnp);
125 }
126
127 oldline(lnp) register line_p lnp; {
128         register kind=TYPE(lnp)&BMASK;
129
130         if (kind == OPLIST)
131                 oldargs(ARG(lnp));
132         oldcore((char *) lnp,lsizetab[kind]);
133 }
134
135 arg_p newarg(kind) int kind; {
136         register arg_p ap;
137
138         ap = (arg_p) newcore(asizetab[kind]);
139         ap->a_type = kind;
140         return(ap);
141 }
142
143 oldargs(ap) register arg_p ap; {
144         register arg_p  next;
145
146         while (ap != (arg_p) 0) {
147                 next = ap->a_next;
148                 switch(ap->a_type) {
149                 case ARGSTRING:
150                         oldargb(ap->a_a.a_string.ab_next);
151                         break;
152                 case ARGICN:
153                 case ARGUCN:
154                 case ARGFCN:
155                         oldargb(ap->a_a.a_con.ac_con.ab_next);
156                         break;
157                 }
158                 oldcore((char *) ap,asizetab[ap->a_type]);
159                 ap = next;
160         }
161 }
162
163 oldargb(abp) register argb_p abp; {
164         register argb_p next;
165
166         while (abp != (argb_p) 0) {
167                 next = abp->ab_next;
168                 oldcore((char *) abp,sizeof (argb_t));
169                 abp = next;
170         }
171 }
172
173 oldobjects(op) register obj_p op; {
174         register obj_p next;
175
176         while (op != (obj_p) 0) {
177                 next = op->o_next;
178                 oldcore((char *) op, sizeof(struct obj));
179                 op = next;
180         }
181 }
182
183 olddblock(dbl) dblock_p dbl; {
184         oldobjects(dbl->d_objlist);
185         oldargs(dbl->d_values);
186         oldcore((char *) dbl, sizeof(struct dblock));
187 }
188
189
190 short **newmap(length) short length; {
191         return((short **) newcore((length+1) * sizeof(short *)));
192 }
193
194 /*ARGSUSED1*/
195 oldmap(mp,length) short **mp, length; {
196         oldcore((char *) mp, (length+1) * sizeof(short *));
197 }
198
199
200 cset newbitvect(n) short n; {
201         return((cset) newcore((n-1)*sizeof(int) + sizeof(struct bitvector)));
202         /* sizeof(struct bitvector) equals to the size of a struct with
203          * one short, followed by one ALLIGNED int. So the above statement
204          * also works e.g. on a VAX.
205          */
206 }
207
208 /*ARGSUSED1*/
209 oldbitvect(s,n) cset s; short n; {
210         oldcore((char *) s, (n-1)*sizeof(int) + sizeof(struct bitvector));
211 }
212
213
214 short *newtable(length) short length; {
215         return((short *) newcore((length+1) * sizeof(short)));
216 }
217
218 /*ARGSUSED1*/
219 oldtable(mp,length) short **mp, length; {
220         oldcore((char *) mp, (length+1) * sizeof(short));
221 }
222
223 cond_p newcondtab(l) int l;
224 {
225         return (cond_p) newcore(l * (sizeof (struct cond_tab)));
226 }
227
228 oldcondtab(tab) cond_p tab;
229 {
230         int i;
231         for (i = 0; tab[i].mc_cond != DEFAULT; i++);
232         oldcore((char *) tab,((i+1) * sizeof (struct cond_tab)));
233 }
234
235
236 char *myalloc(size) register size; {
237         register char *p;
238
239         p = calloc((unsigned) size, 1);
240         if (p == 0)
241                 error("out of memory");
242         return(p);
243 }