1 /*-
2  * %sccs.include.proprietary.c%
3  */
4 
5 #ifndef lint
6 static char sccsid[] = "@(#)0.alloc.c	8.1 (Berkeley) 06/06/93";
7 #endif /* not lint */
8 
9 #include <stdio.h>
10 #include "def.h"
11 int routbeg;
12 
13 extern int debug;
14 struct coreblk	{struct coreblk *nxtblk;
15 			int blksize;
16 			int nxtfree;
17 			int *blk;
18 			};
19 
20 long space;
21 challoc(n)
22 int n;
23 	{
24 	int i;
25 	i = malloc(n);
26 	if(i) { space += n; return(i); }
27 	fprintf(stderr,"alloc out of space\n");
28 	fprintf(stderr,"total space alloc'ed = %D\n",space);
29 	fprintf(stderr,"%d more bytes requested\n",n);
30 	exit(1);
31 	}
32 
33 
34 chfree(p,n)
35 int *p,n;
36 	{
37 	ASSERT(p,chfree);
38 	space -= n;
39 	free(p);
40 	}
41 
42 
43 struct coreblk *tcore, *gcore;
44 int tblksize=12, gblksize=300;
45 
46 
47 balloc(n,p,size)		/* allocate n bytes from coreblk *p */
48 int n,size;		/* use specifies where called */
49 struct coreblk **p;
50 	{
51 	int i;
52 	struct coreblk *q;
53 	n = (n+sizeof(i)-1)/sizeof(i);	/* convert bytes to wds to ensure ints always at wd boundaries */
54 	for (q = *p; ; q = q->nxtblk)
55 		{
56 		if (!q)
57 			{
58 			q = morespace(n,p,size);
59 			break;
60 			}
61 		if (q-> blksize - q->nxtfree >= n)  break;
62 		}
63 	i = q->nxtfree;
64 	q ->nxtfree += n;
65 	return( &(q->blk)[i]);
66 	}
67 
68 talloc(n)		/* allocate from line-by-line storage area */
69 int n;
70 	{return(balloc(n,&tcore,tblksize)); }
71 
72 galloc(n)		/* allocate from graph storage area */
73 int n;
74 	{
75 	return(balloc(n,&gcore,gblksize));
76 	}
77 
78 reuse(p)		/* set nxtfree so coreblk can be reused */
79 struct coreblk *p;
80 	{
81 	for (; p; p=p->nxtblk)  p->nxtfree = 0;
82 	}
83 
84 bfree(p)		/* free coreblk p */
85 struct coreblk *p;
86 	{
87 	if (!p) return;
88 	bfree(p->nxtblk);
89 	p->nxtblk = 0;
90 	free(p);
91 	}
92 
93 
94 morespace(n,p,size)		/* get at least n more wds for coreblk *p */
95 int n,size;
96 struct coreblk **p;
97 	{struct coreblk *q;
98 	int t,i;
99 
100 	t = n<size?size:n;
101 	q = malloc(i=t*sizeof(*(q->blk))+sizeof(*q));
102 	if(!q){
103 		error(": alloc out of space","","");
104 		fprintf(stderr,"space = %D\n",space);
105 		fprintf(stderr,"%d more bytes requested\n",n);
106 		exit(1);
107 		}
108 	space += i;
109 	q->nxtblk = *p;
110 	*p = q;
111 	q -> blksize = t;
112 	q-> nxtfree = 0;
113 	q->blk = q + 1;
114 	return(q);
115 	}
116 
117 
118 
119 
120 freegraf()
121 	{
122 	bfree(gcore);
123 	gcore = 0;
124 
125 
126 	}
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 error(mess1, mess2, mess3)
137 char *mess1, *mess2, *mess3;
138 	{
139 	static lastbeg;
140 	if (lastbeg != routbeg)
141 		{
142 		fprintf(stderr,"routine beginning on line %d:\n",routbeg);
143 		lastbeg = routbeg;
144 		}
145 	fprintf(stderr,"error %s %s %s\n",mess1, mess2, mess3);
146 	}
147 
148 
149 faterr(mess1, mess2, mess3)
150 char *mess1, *mess2, *mess3;
151 	{
152 	error(mess1, mess2, mess3);
153 	exit(1);
154 	}
155 
156 
157 strerr(mess1, mess2, mess3)
158 char *mess1, *mess2, *mess3;
159 	{
160 	error("struct error: ",mess1, mess2);
161 	}
162