10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22527Schin 230Sstevel@tonic-gate /* 24527Schin * Copyright 2001 Sun Microsystems, Inc. All rights reserved. 25527Schin * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * UNIX shell 340Sstevel@tonic-gate */ 350Sstevel@tonic-gate 360Sstevel@tonic-gate #include "defs.h" 370Sstevel@tonic-gate 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * storage allocator 410Sstevel@tonic-gate * (circular first fit strategy) 420Sstevel@tonic-gate */ 430Sstevel@tonic-gate 440Sstevel@tonic-gate #define BUSY 01 450Sstevel@tonic-gate #define busy(x) (Rcheat((x)->word) & BUSY) 460Sstevel@tonic-gate 470Sstevel@tonic-gate unsigned brkincr = BRKINCR; 480Sstevel@tonic-gate struct blk *blokp; /* current search pointer */ 490Sstevel@tonic-gate struct blk *bloktop; /* top of arena (last blok) */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate unsigned char *brkbegin; 520Sstevel@tonic-gate unsigned char *setbrk(); 530Sstevel@tonic-gate 54527Schin void addblok(unsigned int); 55527Schin 560Sstevel@tonic-gate #ifdef __STDC__ 570Sstevel@tonic-gate void * 580Sstevel@tonic-gate #else 590Sstevel@tonic-gate char * 600Sstevel@tonic-gate #endif 610Sstevel@tonic-gate alloc(nbytes) 620Sstevel@tonic-gate size_t nbytes; 630Sstevel@tonic-gate { 64527Schin unsigned rbytes = round(nbytes+BYTESPERWORD, BYTESPERWORD); 650Sstevel@tonic-gate 660Sstevel@tonic-gate if (stakbot == 0) { 670Sstevel@tonic-gate addblok((unsigned)0); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate 700Sstevel@tonic-gate for (;;) 710Sstevel@tonic-gate { 720Sstevel@tonic-gate int c = 0; 73527Schin struct blk *p = blokp; 74527Schin struct blk *q; 750Sstevel@tonic-gate 760Sstevel@tonic-gate do 770Sstevel@tonic-gate { 780Sstevel@tonic-gate if (!busy(p)) 790Sstevel@tonic-gate { 800Sstevel@tonic-gate while (!busy(q = p->word)) 810Sstevel@tonic-gate p->word = q->word; 820Sstevel@tonic-gate if ((char *)q - (char *)p >= rbytes) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate blokp = (struct blk *) 850Sstevel@tonic-gate ((char *)p + rbytes); 860Sstevel@tonic-gate if (q > blokp) 870Sstevel@tonic-gate blokp->word = p->word; 880Sstevel@tonic-gate p->word = (struct blk *) 890Sstevel@tonic-gate (Rcheat(blokp) | BUSY); 900Sstevel@tonic-gate return ((char *)(p + 1)); 910Sstevel@tonic-gate } 920Sstevel@tonic-gate } 930Sstevel@tonic-gate q = p; 940Sstevel@tonic-gate p = (struct blk *)(Rcheat(p->word) & ~BUSY); 950Sstevel@tonic-gate } while (p > q || (c++) == 0); 960Sstevel@tonic-gate addblok(rbytes); 970Sstevel@tonic-gate } 980Sstevel@tonic-gate } 990Sstevel@tonic-gate 100527Schin void 101527Schin addblok(unsigned int reqd) 1020Sstevel@tonic-gate { 103*528Schin if (stakbot == 0) { 1040Sstevel@tonic-gate brkbegin = setbrk(3 * BRKINCR); 1050Sstevel@tonic-gate bloktop = (struct blk *)brkbegin; 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 108*528Schin if (stakbas != staktop) { 109527Schin unsigned char *rndstak; 110527Schin struct blk *blokstak; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate if (staktop >= brkend) 1130Sstevel@tonic-gate growstak(staktop); 1140Sstevel@tonic-gate pushstak(0); 1150Sstevel@tonic-gate rndstak = (unsigned char *)round(staktop, BYTESPERWORD); 1160Sstevel@tonic-gate blokstak = (struct blk *)(stakbas) - 1; 1170Sstevel@tonic-gate blokstak->word = stakbsy; 1180Sstevel@tonic-gate stakbsy = blokstak; 1190Sstevel@tonic-gate bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY); 1200Sstevel@tonic-gate bloktop = (struct blk *)(rndstak); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate reqd += brkincr; 1230Sstevel@tonic-gate reqd &= ~(brkincr - 1); 1240Sstevel@tonic-gate blokp = bloktop; 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * brkend points to the first invalid address. 1270Sstevel@tonic-gate * make sure bloktop is valid. 1280Sstevel@tonic-gate */ 129*528Schin if ((unsigned char *)&bloktop->word >= brkend) { 1300Sstevel@tonic-gate if (setbrk((unsigned)((unsigned char *) 1310Sstevel@tonic-gate (&bloktop->word) - brkend + sizeof (struct blk))) == 1320Sstevel@tonic-gate (unsigned char *)-1) 1330Sstevel@tonic-gate error(nospace); 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd); 136*528Schin if ((unsigned char *)&bloktop->word >= brkend) { 1370Sstevel@tonic-gate if (setbrk((unsigned)((unsigned char *) 1380Sstevel@tonic-gate (&bloktop->word) - brkend + sizeof (struct blk))) == 1390Sstevel@tonic-gate (unsigned char *)-1) 1400Sstevel@tonic-gate error(nospace); 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate bloktop->word = (struct blk *)(brkbegin + 1); 1430Sstevel@tonic-gate { 144527Schin unsigned char *stakadr = (unsigned char *) 1450Sstevel@tonic-gate (bloktop + 2); 146527Schin unsigned char *sp = stakadr; 147*528Schin if (reqd = (staktop-stakbot)) { 1480Sstevel@tonic-gate if (stakadr + reqd >= brkend) 1490Sstevel@tonic-gate growstak(stakadr + reqd); 1500Sstevel@tonic-gate while (reqd-- > 0) 1510Sstevel@tonic-gate *sp++ = *stakbot++; 1520Sstevel@tonic-gate sp--; 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate staktop = sp; 1550Sstevel@tonic-gate if (staktop >= brkend) 1560Sstevel@tonic-gate growstak(staktop); 1570Sstevel@tonic-gate stakbas = stakbot = stakadr; 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate void 1620Sstevel@tonic-gate free(ap) 1630Sstevel@tonic-gate void *ap; 1640Sstevel@tonic-gate { 165527Schin struct blk *p; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if ((p = (struct blk *)ap) && p < bloktop && p > (struct blk *)brkbegin) 1680Sstevel@tonic-gate { 1690Sstevel@tonic-gate #ifdef DEBUG 1700Sstevel@tonic-gate chkbptr(p); 1710Sstevel@tonic-gate #endif 1720Sstevel@tonic-gate --p; 1730Sstevel@tonic-gate p->word = (struct blk *)(Rcheat(p->word) & ~BUSY); 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate #ifdef DEBUG 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate chkbptr(ptr) 1830Sstevel@tonic-gate struct blk *ptr; 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate int exf = 0; 186527Schin struct blk *p = (struct blk *)brkbegin; 187527Schin struct blk *q; 1880Sstevel@tonic-gate int us = 0, un = 0; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate for (;;) 1910Sstevel@tonic-gate { 1920Sstevel@tonic-gate q = (struct blk *)(Rcheat(p->word) & ~BUSY); 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if (p+1 == ptr) 1950Sstevel@tonic-gate exf++; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate if (q < (struct blk *)brkbegin || q > bloktop) 1980Sstevel@tonic-gate abort(3); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate if (p == bloktop) 2010Sstevel@tonic-gate break; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if (busy(p)) 2040Sstevel@tonic-gate us += q - p; 2050Sstevel@tonic-gate else 2060Sstevel@tonic-gate un += q - p; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate if (p >= q) 2090Sstevel@tonic-gate abort(4); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate p = q; 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate if (exf == 0) 2140Sstevel@tonic-gate abort(1); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate chkmem() 2190Sstevel@tonic-gate { 220527Schin struct blk *p = (struct blk *)brkbegin; 221527Schin struct blk *q; 2220Sstevel@tonic-gate int us = 0, un = 0; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate for (;;) { 2250Sstevel@tonic-gate q = (struct blk *)(Rcheat(p->word) & ~BUSY); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate if (q < (struct blk *)brkbegin || q > bloktop) 2280Sstevel@tonic-gate abort(3); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate if (p == bloktop) 2310Sstevel@tonic-gate break; 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if (busy(p)) 2340Sstevel@tonic-gate us += q - p; 2350Sstevel@tonic-gate else 2360Sstevel@tonic-gate un += q - p; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate if (p >= q) 2390Sstevel@tonic-gate abort(4); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate p = q; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate prs("un/used/avail "); 2450Sstevel@tonic-gate prn(un); 2460Sstevel@tonic-gate blank(); 2470Sstevel@tonic-gate prn(us); 2480Sstevel@tonic-gate blank(); 2490Sstevel@tonic-gate prn((char *)bloktop - brkbegin - (un + us)); 2500Sstevel@tonic-gate newline(); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate #endif 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate size_t 2570Sstevel@tonic-gate blklen(q) 2580Sstevel@tonic-gate char *q; 2590Sstevel@tonic-gate { 260527Schin struct blk *pp = (struct blk *)q; 261527Schin struct blk *p; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate --pp; 2640Sstevel@tonic-gate p = (struct blk *)(Rcheat(pp->word) & ~BUSY); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate return ((size_t)((long)p - (long)q)); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * This is a really hasty hack at putting realloc() in the shell, along 2710Sstevel@tonic-gate * with alloc() and free(). I really hate having to do things like this, 2720Sstevel@tonic-gate * hacking in something before I understand _why_ libcollate does any 2730Sstevel@tonic-gate * memory (re)allocation, let alone feel comfortable with this particular 2740Sstevel@tonic-gate * implementation of realloc, assuming it actually gets used by anything. 2750Sstevel@tonic-gate * 2760Sstevel@tonic-gate * I plan to revist this, for now this is just to get sh to compile so 2770Sstevel@tonic-gate * that xcu4 builds may be done and we get xcu4 on our desktops. 2780Sstevel@tonic-gate * 2790Sstevel@tonic-gate * Eric Brunner, 10/21/94 2800Sstevel@tonic-gate * 2810Sstevel@tonic-gate * Implemented a variation on the suggested fix in Trusted Solaris 2.5, 2820Sstevel@tonic-gate * then forward ported the fix into the mainline shell. 2830Sstevel@tonic-gate * 2840Sstevel@tonic-gate * 3/3/99 2850Sstevel@tonic-gate */ 2860Sstevel@tonic-gate #ifdef __STDC__ 2870Sstevel@tonic-gate void * 2880Sstevel@tonic-gate realloc(pp, nbytes) 2890Sstevel@tonic-gate void *pp; 2900Sstevel@tonic-gate size_t nbytes; 2910Sstevel@tonic-gate #else 2920Sstevel@tonic-gate char * 2930Sstevel@tonic-gate realloc(pp, nbytes) 2940Sstevel@tonic-gate char *pp; 2950Sstevel@tonic-gate size_t nbytes; 2960Sstevel@tonic-gate #endif 2970Sstevel@tonic-gate { 2980Sstevel@tonic-gate char *q; 2990Sstevel@tonic-gate size_t blen; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate if (pp == NULL) 3020Sstevel@tonic-gate return (alloc(nbytes)); 3030Sstevel@tonic-gate if ((nbytes == 0) && (pp != NULL)) 3040Sstevel@tonic-gate free(pp); 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate blen = blklen(pp); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (blen < nbytes) { /* need to grow */ 3090Sstevel@tonic-gate q = alloc(nbytes); 3100Sstevel@tonic-gate memcpy(q, pp, blen); 3110Sstevel@tonic-gate free(pp); 3120Sstevel@tonic-gate return ((char *)q); 3130Sstevel@tonic-gate } else if (blen == nbytes) { /* do nothing */ 3140Sstevel@tonic-gate return (pp); 3150Sstevel@tonic-gate } else { /* free excess */ 3160Sstevel@tonic-gate q = alloc(nbytes); 3170Sstevel@tonic-gate memcpy(q, pp, nbytes); 3180Sstevel@tonic-gate free(pp); 3190Sstevel@tonic-gate return ((char *)q); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate #ifdef undef 3230Sstevel@tonic-gate /* 3240Sstevel@tonic-gate * all of what follows is the _idea_ of what is going to be done 3250Sstevel@tonic-gate * getting the size of the block is a problem -- what follows 3260Sstevel@tonic-gate * is _not_ "real", since "sizeof" isn't going to tell me any 3270Sstevel@tonic-gate * thing usefull, probably have to travers the list to the next 3280Sstevel@tonic-gate * blk, then subtract ptr addrs ... and be careful not to leave 3290Sstevel@tonic-gate * holes. 3300Sstevel@tonic-gate */ 3310Sstevel@tonic-gate p = (struct blk *)pp; 3320Sstevel@tonic-gate if (sizeof (p) < nbytes) { /* need to grow */ 3330Sstevel@tonic-gate q = alloc(nbytes); 3340Sstevel@tonic-gate memcpy(q, pp, sizeof (p)); 3350Sstevel@tonic-gate free(pp); 3360Sstevel@tonic-gate return ((char *)q); 3370Sstevel@tonic-gate } else if (sizeof (p) == nbytes) { /* do nothing */ 3380Sstevel@tonic-gate return (pp); 3390Sstevel@tonic-gate } else { /* free excess */ 3400Sstevel@tonic-gate q = alloc(nbytes); 3410Sstevel@tonic-gate memcpy(q, pp, nbytes); 3420Sstevel@tonic-gate free(pp); 3430Sstevel@tonic-gate return ((char *)q); 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate #endif 3460Sstevel@tonic-gate } 347