xref: /titanic_51/usr/src/cmd/sh/blok.c (revision e067707b65c63939ade45e268f2b0dff6f65f75d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 /*
33  *	UNIX shell
34  */
35 
36 #include	"defs.h"
37 
38 
39 /*
40  *	storage allocator
41  *	(circular first fit strategy)
42  */
43 
44 #define	BUSY 01
45 #define	busy(x)	(Rcheat((x)->word) & BUSY)
46 
47 unsigned	brkincr = BRKINCR;
48 struct blk *blokp;			/* current search pointer */
49 struct blk *bloktop;		/* top of arena (last blok) */
50 
51 unsigned char		*brkbegin;
52 unsigned char		*setbrk();
53 
54 void addblok(unsigned int);
55 
56 #ifdef __STDC__
57 void *
58 #else
59 char *
60 #endif
61 alloc(nbytes)
62 	size_t nbytes;
63 {
64 	unsigned rbytes = round(nbytes+BYTESPERWORD, BYTESPERWORD);
65 
66 	if (stakbot == 0) {
67 		addblok((unsigned)0);
68 	}
69 
70 	for (;;)
71 	{
72 		int	c = 0;
73 		struct blk *p = blokp;
74 		struct blk *q;
75 
76 		do
77 		{
78 			if (!busy(p))
79 			{
80 				while (!busy(q = p->word))
81 					p->word = q->word;
82 				if ((char *)q - (char *)p >= rbytes)
83 				{
84 					blokp = (struct blk *)
85 							((char *)p + rbytes);
86 					if (q > blokp)
87 						blokp->word = p->word;
88 					p->word = (struct blk *)
89 							(Rcheat(blokp) | BUSY);
90 					return ((char *)(p + 1));
91 				}
92 			}
93 			q = p;
94 			p = (struct blk *)(Rcheat(p->word) & ~BUSY);
95 		} while (p > q || (c++) == 0);
96 		addblok(rbytes);
97 	}
98 }
99 
100 void
101 addblok(unsigned int reqd)
102 {
103 	if (stakbot == 0) {
104 		brkbegin = setbrk(3 * BRKINCR);
105 		bloktop = (struct blk *)brkbegin;
106 	}
107 
108 	if (stakbas != staktop) {
109 		unsigned char *rndstak;
110 		struct blk *blokstak;
111 
112 		if (staktop >= brkend)
113 			growstak(staktop);
114 		pushstak(0);
115 		rndstak = (unsigned char *)round(staktop, BYTESPERWORD);
116 		blokstak = (struct blk *)(stakbas) - 1;
117 		blokstak->word = stakbsy;
118 		stakbsy = blokstak;
119 		bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY);
120 		bloktop = (struct blk *)(rndstak);
121 	}
122 	reqd += brkincr;
123 	reqd &= ~(brkincr - 1);
124 	blokp = bloktop;
125 	/*
126 	 * brkend points to the first invalid address.
127 	 * make sure bloktop is valid.
128 	 */
129 	if ((unsigned char *)&bloktop->word >= brkend) {
130 		if (setbrk((unsigned)((unsigned char *)
131 		    (&bloktop->word) - brkend + sizeof (struct blk))) ==
132 		    (unsigned char *)-1)
133 			error(nospace);
134 	}
135 	bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd);
136 	if ((unsigned char *)&bloktop->word >= brkend) {
137 		if (setbrk((unsigned)((unsigned char *)
138 		    (&bloktop->word) - brkend + sizeof (struct blk))) ==
139 		    (unsigned char *)-1)
140 			error(nospace);
141 	}
142 	bloktop->word = (struct blk *)(brkbegin + 1);
143 	{
144 		unsigned char *stakadr = (unsigned char *)
145 							(bloktop + 2);
146 		unsigned char *sp = stakadr;
147 		if (reqd = (staktop-stakbot)) {
148 			if (stakadr + reqd >= brkend)
149 				growstak(stakadr + reqd);
150 			while (reqd-- > 0)
151 				*sp++ = *stakbot++;
152 			sp--;
153 		}
154 		staktop = sp;
155 		if (staktop >= brkend)
156 			growstak(staktop);
157 		stakbas = stakbot = stakadr;
158 	}
159 }
160 
161 void
162 free(ap)
163 	void *ap;
164 {
165 	struct blk *p;
166 
167 	if ((p = (struct blk *)ap) && p < bloktop && p > (struct blk *)brkbegin)
168 	{
169 #ifdef DEBUG
170 		chkbptr(p);
171 #endif
172 		--p;
173 		p->word = (struct blk *)(Rcheat(p->word) & ~BUSY);
174 	}
175 
176 
177 }
178 
179 
180 #ifdef DEBUG
181 
182 chkbptr(ptr)
183 	struct blk *ptr;
184 {
185 	int	exf = 0;
186 	struct blk *p = (struct blk *)brkbegin;
187 	struct blk *q;
188 	int	us = 0, un = 0;
189 
190 	for (;;)
191 	{
192 		q = (struct blk *)(Rcheat(p->word) & ~BUSY);
193 
194 		if (p+1 == ptr)
195 			exf++;
196 
197 		if (q < (struct blk *)brkbegin || q > bloktop)
198 			abort(3);
199 
200 		if (p == bloktop)
201 			break;
202 
203 		if (busy(p))
204 			us += q - p;
205 		else
206 			un += q - p;
207 
208 		if (p >= q)
209 			abort(4);
210 
211 		p = q;
212 	}
213 	if (exf == 0)
214 		abort(1);
215 }
216 
217 
218 chkmem()
219 {
220 	struct blk *p = (struct blk *)brkbegin;
221 	struct blk *q;
222 	int	us = 0, un = 0;
223 
224 	for (;;) {
225 		q = (struct blk *)(Rcheat(p->word) & ~BUSY);
226 
227 		if (q < (struct blk *)brkbegin || q > bloktop)
228 			abort(3);
229 
230 		if (p == bloktop)
231 			break;
232 
233 		if (busy(p))
234 			us += q - p;
235 		else
236 			un += q - p;
237 
238 		if (p >= q)
239 			abort(4);
240 
241 		p = q;
242 	}
243 
244 	prs("un/used/avail ");
245 	prn(un);
246 	blank();
247 	prn(us);
248 	blank();
249 	prn((char *)bloktop - brkbegin - (un + us));
250 	newline();
251 
252 }
253 
254 #endif
255 
256 size_t
257 blklen(q)
258 char *q;
259 {
260 	struct blk *pp = (struct blk *)q;
261 	struct blk *p;
262 
263 	--pp;
264 	p = (struct blk *)(Rcheat(pp->word) & ~BUSY);
265 
266 	return ((size_t)((long)p - (long)q));
267 }
268 
269 /*
270  * This is a really hasty hack at putting realloc() in the shell, along
271  * with alloc() and free(). I really hate having to do things like this,
272  * hacking in something before I understand _why_ libcollate does any
273  * memory (re)allocation, let alone feel comfortable with this particular
274  * implementation of realloc, assuming it actually gets used by anything.
275  *
276  * I plan to revist this, for now this is just to get sh to compile so
277  * that xcu4 builds may be done and we get xcu4 on our desktops.
278  *
279  * Eric Brunner, 10/21/94
280  *
281  * Implemented a variation on the suggested fix in Trusted Solaris 2.5,
282  * then forward ported the fix into the mainline shell.
283  *
284  * 3/3/99
285  */
286 #ifdef __STDC__
287 void *
288 realloc(pp, nbytes)
289 void *pp;
290 size_t nbytes;
291 #else
292 char *
293 realloc(pp, nbytes)
294 char *pp;
295 size_t nbytes;
296 #endif
297 {
298 	char *q;
299 	size_t blen;
300 
301 	if (pp == NULL)
302 		return (alloc(nbytes));
303 	if ((nbytes == 0) && (pp != NULL))
304 		free(pp);
305 
306 	blen = blklen(pp);
307 
308 	if (blen < nbytes) {		/* need to grow */
309 		q = alloc(nbytes);
310 		memcpy(q, pp, blen);
311 		free(pp);
312 		return ((char *)q);
313 	} else if (blen == nbytes) {	/* do nothing */
314 		return (pp);
315 	} else {			/* free excess */
316 		q = alloc(nbytes);
317 		memcpy(q, pp, nbytes);
318 		free(pp);
319 		return ((char *)q);
320 	}
321 
322 #ifdef undef
323 	/*
324 	 * all of what follows is the _idea_ of what is going to be done
325 	 * getting the size of the block is a problem -- what follows
326 	 * is _not_ "real", since "sizeof" isn't going to tell me any
327 	 * thing usefull, probably have to travers the list to the next
328 	 * blk, then subtract ptr addrs ... and be careful not to leave
329 	 * holes.
330 	 */
331 	p = (struct blk *)pp;
332 	if (sizeof (p) < nbytes) {			/* need to grow */
333 		q = alloc(nbytes);
334 		memcpy(q, pp, sizeof (p));
335 		free(pp);
336 		return ((char *)q);
337 	} else if (sizeof (p) == nbytes) {		/* do nothing */
338 		return (pp);
339 	} else {					/* free excess */
340 		q = alloc(nbytes);
341 		memcpy(q, pp, nbytes);
342 		free(pp);
343 		return ((char *)q);
344 	}
345 #endif
346 }
347