xref: /illumos-gate/usr/src/cmd/sh/blok.c (revision db397771)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22965005c8Schin 
237c478bd9Sstevel@tonic-gate /*
24965005c8Schin  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
25965005c8Schin  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	UNIX shell
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	"defs.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  *	storage allocator
417c478bd9Sstevel@tonic-gate  *	(circular first fit strategy)
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	BUSY 01
457c478bd9Sstevel@tonic-gate #define	busy(x)	(Rcheat((x)->word) & BUSY)
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate unsigned	brkincr = BRKINCR;
487c478bd9Sstevel@tonic-gate struct blk *blokp;			/* current search pointer */
497c478bd9Sstevel@tonic-gate struct blk *bloktop;		/* top of arena (last blok) */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate unsigned char		*brkbegin;
527c478bd9Sstevel@tonic-gate unsigned char		*setbrk();
537c478bd9Sstevel@tonic-gate 
54965005c8Schin void addblok(unsigned int);
55965005c8Schin 
567c478bd9Sstevel@tonic-gate #ifdef __STDC__
577c478bd9Sstevel@tonic-gate void *
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate char *
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate alloc(nbytes)
627c478bd9Sstevel@tonic-gate 	size_t nbytes;
637c478bd9Sstevel@tonic-gate {
64965005c8Schin 	unsigned rbytes = round(nbytes+BYTESPERWORD, BYTESPERWORD);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (stakbot == 0) {
677c478bd9Sstevel@tonic-gate 		addblok((unsigned)0);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	for (;;)
717c478bd9Sstevel@tonic-gate 	{
727c478bd9Sstevel@tonic-gate 		int	c = 0;
73965005c8Schin 		struct blk *p = blokp;
74965005c8Schin 		struct blk *q;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 		do
777c478bd9Sstevel@tonic-gate 		{
787c478bd9Sstevel@tonic-gate 			if (!busy(p))
797c478bd9Sstevel@tonic-gate 			{
807c478bd9Sstevel@tonic-gate 				while (!busy(q = p->word))
817c478bd9Sstevel@tonic-gate 					p->word = q->word;
827c478bd9Sstevel@tonic-gate 				if ((char *)q - (char *)p >= rbytes)
837c478bd9Sstevel@tonic-gate 				{
847c478bd9Sstevel@tonic-gate 					blokp = (struct blk *)
857c478bd9Sstevel@tonic-gate 							((char *)p + rbytes);
867c478bd9Sstevel@tonic-gate 					if (q > blokp)
877c478bd9Sstevel@tonic-gate 						blokp->word = p->word;
887c478bd9Sstevel@tonic-gate 					p->word = (struct blk *)
897c478bd9Sstevel@tonic-gate 							(Rcheat(blokp) | BUSY);
907c478bd9Sstevel@tonic-gate 					return ((char *)(p + 1));
917c478bd9Sstevel@tonic-gate 				}
927c478bd9Sstevel@tonic-gate 			}
937c478bd9Sstevel@tonic-gate 			q = p;
947c478bd9Sstevel@tonic-gate 			p = (struct blk *)(Rcheat(p->word) & ~BUSY);
957c478bd9Sstevel@tonic-gate 		} while (p > q || (c++) == 0);
967c478bd9Sstevel@tonic-gate 		addblok(rbytes);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
100965005c8Schin void
101965005c8Schin addblok(unsigned int reqd)
1027c478bd9Sstevel@tonic-gate {
103*db397771Schin 	if (stakbot == 0) {
1047c478bd9Sstevel@tonic-gate 		brkbegin = setbrk(3 * BRKINCR);
1057c478bd9Sstevel@tonic-gate 		bloktop = (struct blk *)brkbegin;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
108*db397771Schin 	if (stakbas != staktop) {
109965005c8Schin 		unsigned char *rndstak;
110965005c8Schin 		struct blk *blokstak;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		if (staktop >= brkend)
1137c478bd9Sstevel@tonic-gate 			growstak(staktop);
1147c478bd9Sstevel@tonic-gate 		pushstak(0);
1157c478bd9Sstevel@tonic-gate 		rndstak = (unsigned char *)round(staktop, BYTESPERWORD);
1167c478bd9Sstevel@tonic-gate 		blokstak = (struct blk *)(stakbas) - 1;
1177c478bd9Sstevel@tonic-gate 		blokstak->word = stakbsy;
1187c478bd9Sstevel@tonic-gate 		stakbsy = blokstak;
1197c478bd9Sstevel@tonic-gate 		bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY);
1207c478bd9Sstevel@tonic-gate 		bloktop = (struct blk *)(rndstak);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	reqd += brkincr;
1237c478bd9Sstevel@tonic-gate 	reqd &= ~(brkincr - 1);
1247c478bd9Sstevel@tonic-gate 	blokp = bloktop;
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * brkend points to the first invalid address.
1277c478bd9Sstevel@tonic-gate 	 * make sure bloktop is valid.
1287c478bd9Sstevel@tonic-gate 	 */
129*db397771Schin 	if ((unsigned char *)&bloktop->word >= brkend) {
1307c478bd9Sstevel@tonic-gate 		if (setbrk((unsigned)((unsigned char *)
1317c478bd9Sstevel@tonic-gate 		    (&bloktop->word) - brkend + sizeof (struct blk))) ==
1327c478bd9Sstevel@tonic-gate 		    (unsigned char *)-1)
1337c478bd9Sstevel@tonic-gate 			error(nospace);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd);
136*db397771Schin 	if ((unsigned char *)&bloktop->word >= brkend) {
1377c478bd9Sstevel@tonic-gate 		if (setbrk((unsigned)((unsigned char *)
1387c478bd9Sstevel@tonic-gate 		    (&bloktop->word) - brkend + sizeof (struct blk))) ==
1397c478bd9Sstevel@tonic-gate 		    (unsigned char *)-1)
1407c478bd9Sstevel@tonic-gate 			error(nospace);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	bloktop->word = (struct blk *)(brkbegin + 1);
1437c478bd9Sstevel@tonic-gate 	{
144965005c8Schin 		unsigned char *stakadr = (unsigned char *)
1457c478bd9Sstevel@tonic-gate 							(bloktop + 2);
146965005c8Schin 		unsigned char *sp = stakadr;
147*db397771Schin 		if (reqd = (staktop-stakbot)) {
1487c478bd9Sstevel@tonic-gate 			if (stakadr + reqd >= brkend)
1497c478bd9Sstevel@tonic-gate 				growstak(stakadr + reqd);
1507c478bd9Sstevel@tonic-gate 			while (reqd-- > 0)
1517c478bd9Sstevel@tonic-gate 				*sp++ = *stakbot++;
1527c478bd9Sstevel@tonic-gate 			sp--;
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		staktop = sp;
1557c478bd9Sstevel@tonic-gate 		if (staktop >= brkend)
1567c478bd9Sstevel@tonic-gate 			growstak(staktop);
1577c478bd9Sstevel@tonic-gate 		stakbas = stakbot = stakadr;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
1627c478bd9Sstevel@tonic-gate free(ap)
1637c478bd9Sstevel@tonic-gate 	void *ap;
1647c478bd9Sstevel@tonic-gate {
165965005c8Schin 	struct blk *p;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if ((p = (struct blk *)ap) && p < bloktop && p > (struct blk *)brkbegin)
1687c478bd9Sstevel@tonic-gate 	{
1697c478bd9Sstevel@tonic-gate #ifdef DEBUG
1707c478bd9Sstevel@tonic-gate 		chkbptr(p);
1717c478bd9Sstevel@tonic-gate #endif
1727c478bd9Sstevel@tonic-gate 		--p;
1737c478bd9Sstevel@tonic-gate 		p->word = (struct blk *)(Rcheat(p->word) & ~BUSY);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #ifdef DEBUG
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate chkbptr(ptr)
1837c478bd9Sstevel@tonic-gate 	struct blk *ptr;
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	int	exf = 0;
186965005c8Schin 	struct blk *p = (struct blk *)brkbegin;
187965005c8Schin 	struct blk *q;
1887c478bd9Sstevel@tonic-gate 	int	us = 0, un = 0;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	for (;;)
1917c478bd9Sstevel@tonic-gate 	{
1927c478bd9Sstevel@tonic-gate 		q = (struct blk *)(Rcheat(p->word) & ~BUSY);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		if (p+1 == ptr)
1957c478bd9Sstevel@tonic-gate 			exf++;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		if (q < (struct blk *)brkbegin || q > bloktop)
1987c478bd9Sstevel@tonic-gate 			abort(3);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		if (p == bloktop)
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		if (busy(p))
2047c478bd9Sstevel@tonic-gate 			us += q - p;
2057c478bd9Sstevel@tonic-gate 		else
2067c478bd9Sstevel@tonic-gate 			un += q - p;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (p >= q)
2097c478bd9Sstevel@tonic-gate 			abort(4);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		p = q;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	if (exf == 0)
2147c478bd9Sstevel@tonic-gate 		abort(1);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate chkmem()
2197c478bd9Sstevel@tonic-gate {
220965005c8Schin 	struct blk *p = (struct blk *)brkbegin;
221965005c8Schin 	struct blk *q;
2227c478bd9Sstevel@tonic-gate 	int	us = 0, un = 0;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	for (;;) {
2257c478bd9Sstevel@tonic-gate 		q = (struct blk *)(Rcheat(p->word) & ~BUSY);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		if (q < (struct blk *)brkbegin || q > bloktop)
2287c478bd9Sstevel@tonic-gate 			abort(3);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		if (p == bloktop)
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (busy(p))
2347c478bd9Sstevel@tonic-gate 			us += q - p;
2357c478bd9Sstevel@tonic-gate 		else
2367c478bd9Sstevel@tonic-gate 			un += q - p;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		if (p >= q)
2397c478bd9Sstevel@tonic-gate 			abort(4);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		p = q;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	prs("un/used/avail ");
2457c478bd9Sstevel@tonic-gate 	prn(un);
2467c478bd9Sstevel@tonic-gate 	blank();
2477c478bd9Sstevel@tonic-gate 	prn(us);
2487c478bd9Sstevel@tonic-gate 	blank();
2497c478bd9Sstevel@tonic-gate 	prn((char *)bloktop - brkbegin - (un + us));
2507c478bd9Sstevel@tonic-gate 	newline();
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #endif
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate size_t
2577c478bd9Sstevel@tonic-gate blklen(q)
2587c478bd9Sstevel@tonic-gate char *q;
2597c478bd9Sstevel@tonic-gate {
260965005c8Schin 	struct blk *pp = (struct blk *)q;
261965005c8Schin 	struct blk *p;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	--pp;
2647c478bd9Sstevel@tonic-gate 	p = (struct blk *)(Rcheat(pp->word) & ~BUSY);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return ((size_t)((long)p - (long)q));
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * This is a really hasty hack at putting realloc() in the shell, along
2717c478bd9Sstevel@tonic-gate  * with alloc() and free(). I really hate having to do things like this,
2727c478bd9Sstevel@tonic-gate  * hacking in something before I understand _why_ libcollate does any
2737c478bd9Sstevel@tonic-gate  * memory (re)allocation, let alone feel comfortable with this particular
2747c478bd9Sstevel@tonic-gate  * implementation of realloc, assuming it actually gets used by anything.
2757c478bd9Sstevel@tonic-gate  *
2767c478bd9Sstevel@tonic-gate  * I plan to revist this, for now this is just to get sh to compile so
2777c478bd9Sstevel@tonic-gate  * that xcu4 builds may be done and we get xcu4 on our desktops.
2787c478bd9Sstevel@tonic-gate  *
2797c478bd9Sstevel@tonic-gate  * Eric Brunner, 10/21/94
2807c478bd9Sstevel@tonic-gate  *
2817c478bd9Sstevel@tonic-gate  * Implemented a variation on the suggested fix in Trusted Solaris 2.5,
2827c478bd9Sstevel@tonic-gate  * then forward ported the fix into the mainline shell.
2837c478bd9Sstevel@tonic-gate  *
2847c478bd9Sstevel@tonic-gate  * 3/3/99
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate #ifdef __STDC__
2877c478bd9Sstevel@tonic-gate void *
2887c478bd9Sstevel@tonic-gate realloc(pp, nbytes)
2897c478bd9Sstevel@tonic-gate void *pp;
2907c478bd9Sstevel@tonic-gate size_t nbytes;
2917c478bd9Sstevel@tonic-gate #else
2927c478bd9Sstevel@tonic-gate char *
2937c478bd9Sstevel@tonic-gate realloc(pp, nbytes)
2947c478bd9Sstevel@tonic-gate char *pp;
2957c478bd9Sstevel@tonic-gate size_t nbytes;
2967c478bd9Sstevel@tonic-gate #endif
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	char *q;
2997c478bd9Sstevel@tonic-gate 	size_t blen;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (pp == NULL)
3027c478bd9Sstevel@tonic-gate 		return (alloc(nbytes));
3037c478bd9Sstevel@tonic-gate 	if ((nbytes == 0) && (pp != NULL))
3047c478bd9Sstevel@tonic-gate 		free(pp);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	blen = blklen(pp);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (blen < nbytes) {		/* need to grow */
3097c478bd9Sstevel@tonic-gate 		q = alloc(nbytes);
3107c478bd9Sstevel@tonic-gate 		memcpy(q, pp, blen);
3117c478bd9Sstevel@tonic-gate 		free(pp);
3127c478bd9Sstevel@tonic-gate 		return ((char *)q);
3137c478bd9Sstevel@tonic-gate 	} else if (blen == nbytes) {	/* do nothing */
3147c478bd9Sstevel@tonic-gate 		return (pp);
3157c478bd9Sstevel@tonic-gate 	} else {			/* free excess */
3167c478bd9Sstevel@tonic-gate 		q = alloc(nbytes);
3177c478bd9Sstevel@tonic-gate 		memcpy(q, pp, nbytes);
3187c478bd9Sstevel@tonic-gate 		free(pp);
3197c478bd9Sstevel@tonic-gate 		return ((char *)q);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #ifdef undef
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * all of what follows is the _idea_ of what is going to be done
3257c478bd9Sstevel@tonic-gate 	 * getting the size of the block is a problem -- what follows
3267c478bd9Sstevel@tonic-gate 	 * is _not_ "real", since "sizeof" isn't going to tell me any
3277c478bd9Sstevel@tonic-gate 	 * thing usefull, probably have to travers the list to the next
3287c478bd9Sstevel@tonic-gate 	 * blk, then subtract ptr addrs ... and be careful not to leave
3297c478bd9Sstevel@tonic-gate 	 * holes.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 	p = (struct blk *)pp;
3327c478bd9Sstevel@tonic-gate 	if (sizeof (p) < nbytes) {			/* need to grow */
3337c478bd9Sstevel@tonic-gate 		q = alloc(nbytes);
3347c478bd9Sstevel@tonic-gate 		memcpy(q, pp, sizeof (p));
3357c478bd9Sstevel@tonic-gate 		free(pp);
3367c478bd9Sstevel@tonic-gate 		return ((char *)q);
3377c478bd9Sstevel@tonic-gate 	} else if (sizeof (p) == nbytes) {		/* do nothing */
3387c478bd9Sstevel@tonic-gate 		return (pp);
3397c478bd9Sstevel@tonic-gate 	} else {					/* free excess */
3407c478bd9Sstevel@tonic-gate 		q = alloc(nbytes);
3417c478bd9Sstevel@tonic-gate 		memcpy(q, pp, nbytes);
3427c478bd9Sstevel@tonic-gate 		free(pp);
3437c478bd9Sstevel@tonic-gate 		return ((char *)q);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate #endif
3467c478bd9Sstevel@tonic-gate }
347