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