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 52013Smj162486 * Common Development and Distribution License (the "License"). 62013Smj162486 * 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*9369SNobutomo.Nakano@Sun.COM * Copyright 2009 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 /* 310Sstevel@tonic-gate * UNIX shell 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include "defs.h" 350Sstevel@tonic-gate 360Sstevel@tonic-gate 370Sstevel@tonic-gate /* 380Sstevel@tonic-gate * storage allocator 390Sstevel@tonic-gate * (circular first fit strategy) 400Sstevel@tonic-gate */ 410Sstevel@tonic-gate 420Sstevel@tonic-gate #define BUSY 01 430Sstevel@tonic-gate #define busy(x) (Rcheat((x)->word) & BUSY) 440Sstevel@tonic-gate 450Sstevel@tonic-gate unsigned brkincr = BRKINCR; 460Sstevel@tonic-gate struct blk *blokp; /* current search pointer */ 470Sstevel@tonic-gate struct blk *bloktop; /* top of arena (last blok) */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate unsigned char *brkbegin; 50*9369SNobutomo.Nakano@Sun.COM extern unsigned char *setbrk(); 510Sstevel@tonic-gate 52*9369SNobutomo.Nakano@Sun.COM #ifdef DEBUG 53*9369SNobutomo.Nakano@Sun.COM /* 54*9369SNobutomo.Nakano@Sun.COM * If DEBUG is defined, the following testing will be performed: 55*9369SNobutomo.Nakano@Sun.COM * - chkbptr() checks the linkage of blocks by following links. 56*9369SNobutomo.Nakano@Sun.COM * Note that this makes shell really slow. 57*9369SNobutomo.Nakano@Sun.COM * - fill_pat() fills the memory block with pattern like umem does. 58*9369SNobutomo.Nakano@Sun.COM * The pattern used to fill the memory area is defined below. 59*9369SNobutomo.Nakano@Sun.COM */ 60*9369SNobutomo.Nakano@Sun.COM #define PAT_MAGIC 0xfeedface 61*9369SNobutomo.Nakano@Sun.COM #define PAT_INIT 0xbaddcafe 62*9369SNobutomo.Nakano@Sun.COM #define PAT_FREE 0xdeadbeef 63527Schin 64*9369SNobutomo.Nakano@Sun.COM static void fill_pat(struct blk *, uint32_t); 65*9369SNobutomo.Nakano@Sun.COM static void chkbptr(struct blk *); 66*9369SNobutomo.Nakano@Sun.COM #endif 67*9369SNobutomo.Nakano@Sun.COM 680Sstevel@tonic-gate void * 69*9369SNobutomo.Nakano@Sun.COM alloc(size_t nbytes) 700Sstevel@tonic-gate { 71*9369SNobutomo.Nakano@Sun.COM size_t rbytes = round(nbytes + ALIGNSIZ, ALIGNSIZ); 720Sstevel@tonic-gate 730Sstevel@tonic-gate if (stakbot == 0) { 74*9369SNobutomo.Nakano@Sun.COM addblok((unsigned int)0); 750Sstevel@tonic-gate } 760Sstevel@tonic-gate 77*9369SNobutomo.Nakano@Sun.COM for (;;) { 780Sstevel@tonic-gate int c = 0; 79527Schin struct blk *p = blokp; 80527Schin struct blk *q; 810Sstevel@tonic-gate 820Sstevel@tonic-gate do 830Sstevel@tonic-gate { 84*9369SNobutomo.Nakano@Sun.COM if (!busy(p)) { 850Sstevel@tonic-gate while (!busy(q = p->word)) 860Sstevel@tonic-gate p->word = q->word; 87*9369SNobutomo.Nakano@Sun.COM if ((char *)q - (char *)p >= rbytes) { 880Sstevel@tonic-gate blokp = (struct blk *) 890Sstevel@tonic-gate ((char *)p + rbytes); 900Sstevel@tonic-gate if (q > blokp) 910Sstevel@tonic-gate blokp->word = p->word; 920Sstevel@tonic-gate p->word = (struct blk *) 930Sstevel@tonic-gate (Rcheat(blokp) | BUSY); 94*9369SNobutomo.Nakano@Sun.COM #ifdef DEBUG 95*9369SNobutomo.Nakano@Sun.COM fill_pat(p, PAT_INIT); 96*9369SNobutomo.Nakano@Sun.COM #endif 970Sstevel@tonic-gate return ((char *)(p + 1)); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate q = p; 1010Sstevel@tonic-gate p = (struct blk *)(Rcheat(p->word) & ~BUSY); 1020Sstevel@tonic-gate } while (p > q || (c++) == 0); 1030Sstevel@tonic-gate addblok(rbytes); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 107527Schin void 108527Schin addblok(unsigned int reqd) 1090Sstevel@tonic-gate { 110528Schin if (stakbot == 0) { 1110Sstevel@tonic-gate brkbegin = setbrk(3 * BRKINCR); 1122013Smj162486 /* 1132013Smj162486 * setbrk() returns 8 byte aligned address 1142013Smj162486 * but we could need larger align in future 1152013Smj162486 */ 1162013Smj162486 brkbegin = (unsigned char *)round(brkbegin, ALIGNSIZ); 1170Sstevel@tonic-gate bloktop = (struct blk *)brkbegin; 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 120528Schin if (stakbas != staktop) { 121527Schin unsigned char *rndstak; 122527Schin struct blk *blokstak; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate if (staktop >= brkend) 1250Sstevel@tonic-gate growstak(staktop); 1260Sstevel@tonic-gate pushstak(0); 1272013Smj162486 rndstak = (unsigned char *)round(staktop, ALIGNSIZ); 1280Sstevel@tonic-gate blokstak = (struct blk *)(stakbas) - 1; 1290Sstevel@tonic-gate blokstak->word = stakbsy; 1300Sstevel@tonic-gate stakbsy = blokstak; 1310Sstevel@tonic-gate bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY); 1320Sstevel@tonic-gate bloktop = (struct blk *)(rndstak); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate reqd += brkincr; 1350Sstevel@tonic-gate reqd &= ~(brkincr - 1); 1360Sstevel@tonic-gate blokp = bloktop; 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * brkend points to the first invalid address. 1390Sstevel@tonic-gate * make sure bloktop is valid. 1400Sstevel@tonic-gate */ 141528Schin if ((unsigned char *)&bloktop->word >= brkend) { 1420Sstevel@tonic-gate if (setbrk((unsigned)((unsigned char *) 1430Sstevel@tonic-gate (&bloktop->word) - brkend + sizeof (struct blk))) == 1440Sstevel@tonic-gate (unsigned char *)-1) 1450Sstevel@tonic-gate error(nospace); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd); 148528Schin if ((unsigned char *)&bloktop->word >= brkend) { 1490Sstevel@tonic-gate if (setbrk((unsigned)((unsigned char *) 1500Sstevel@tonic-gate (&bloktop->word) - brkend + sizeof (struct blk))) == 1510Sstevel@tonic-gate (unsigned char *)-1) 1520Sstevel@tonic-gate error(nospace); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate bloktop->word = (struct blk *)(brkbegin + 1); 1550Sstevel@tonic-gate { 156527Schin unsigned char *stakadr = (unsigned char *) 1570Sstevel@tonic-gate (bloktop + 2); 158527Schin unsigned char *sp = stakadr; 159528Schin if (reqd = (staktop-stakbot)) { 1600Sstevel@tonic-gate if (stakadr + reqd >= brkend) 1610Sstevel@tonic-gate growstak(stakadr + reqd); 1620Sstevel@tonic-gate while (reqd-- > 0) 1630Sstevel@tonic-gate *sp++ = *stakbot++; 1640Sstevel@tonic-gate sp--; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate staktop = sp; 1670Sstevel@tonic-gate if (staktop >= brkend) 1680Sstevel@tonic-gate growstak(staktop); 1690Sstevel@tonic-gate stakbas = stakbot = stakadr; 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate void 1740Sstevel@tonic-gate free(ap) 1750Sstevel@tonic-gate void *ap; 1760Sstevel@tonic-gate { 177527Schin struct blk *p; 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate if ((p = (struct blk *)ap) && p < bloktop && p > (struct blk *)brkbegin) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate #ifdef DEBUG 1820Sstevel@tonic-gate chkbptr(p); 1830Sstevel@tonic-gate #endif 1840Sstevel@tonic-gate --p; 1850Sstevel@tonic-gate p->word = (struct blk *)(Rcheat(p->word) & ~BUSY); 186*9369SNobutomo.Nakano@Sun.COM #ifdef DEBUG 187*9369SNobutomo.Nakano@Sun.COM fill_pat(p, PAT_FREE); 188*9369SNobutomo.Nakano@Sun.COM #endif 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate #ifdef DEBUG 1960Sstevel@tonic-gate 197*9369SNobutomo.Nakano@Sun.COM static void 198*9369SNobutomo.Nakano@Sun.COM fill_pat(struct blk *ptr, uint32_t pat) 199*9369SNobutomo.Nakano@Sun.COM { 200*9369SNobutomo.Nakano@Sun.COM uint32_t *ui, *eui; 201*9369SNobutomo.Nakano@Sun.COM 202*9369SNobutomo.Nakano@Sun.COM *(uint32_t *)ptr->pad = PAT_MAGIC; 203*9369SNobutomo.Nakano@Sun.COM eui = (uint32_t *)(Rcheat(ptr->word) & ~BUSY); 204*9369SNobutomo.Nakano@Sun.COM for (ui = (uint32_t *)(ptr + 1); ui < eui; ui++) 205*9369SNobutomo.Nakano@Sun.COM *ui = pat; 206*9369SNobutomo.Nakano@Sun.COM } 207*9369SNobutomo.Nakano@Sun.COM 208*9369SNobutomo.Nakano@Sun.COM static void 209*9369SNobutomo.Nakano@Sun.COM chkbptr(struct blk *ptr) 2100Sstevel@tonic-gate { 2110Sstevel@tonic-gate int exf = 0; 212527Schin struct blk *p = (struct blk *)brkbegin; 213527Schin struct blk *q; 2140Sstevel@tonic-gate int us = 0, un = 0; 2150Sstevel@tonic-gate 216*9369SNobutomo.Nakano@Sun.COM for (;;) { 2170Sstevel@tonic-gate q = (struct blk *)(Rcheat(p->word) & ~BUSY); 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate if (p+1 == ptr) 2200Sstevel@tonic-gate exf++; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate if (q < (struct blk *)brkbegin || q > bloktop) 223*9369SNobutomo.Nakano@Sun.COM abort(); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate if (p == bloktop) 2260Sstevel@tonic-gate break; 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate if (busy(p)) 2290Sstevel@tonic-gate us += q - p; 2300Sstevel@tonic-gate else 2310Sstevel@tonic-gate un += q - p; 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if (p >= q) 234*9369SNobutomo.Nakano@Sun.COM abort(); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate p = q; 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate if (exf == 0) 239*9369SNobutomo.Nakano@Sun.COM abort(); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 242*9369SNobutomo.Nakano@Sun.COM static void 2430Sstevel@tonic-gate chkmem() 2440Sstevel@tonic-gate { 245527Schin struct blk *p = (struct blk *)brkbegin; 246527Schin struct blk *q; 2470Sstevel@tonic-gate int us = 0, un = 0; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate for (;;) { 2500Sstevel@tonic-gate q = (struct blk *)(Rcheat(p->word) & ~BUSY); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if (q < (struct blk *)brkbegin || q > bloktop) 253*9369SNobutomo.Nakano@Sun.COM abort(); 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate if (p == bloktop) 2560Sstevel@tonic-gate break; 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate if (busy(p)) 2590Sstevel@tonic-gate us += q - p; 2600Sstevel@tonic-gate else 2610Sstevel@tonic-gate un += q - p; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate if (p >= q) 264*9369SNobutomo.Nakano@Sun.COM abort(); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate p = q; 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate prs("un/used/avail "); 2700Sstevel@tonic-gate prn(un); 2710Sstevel@tonic-gate blank(); 2720Sstevel@tonic-gate prn(us); 2730Sstevel@tonic-gate blank(); 274*9369SNobutomo.Nakano@Sun.COM prn((uintptr_t)bloktop - (uintptr_t)brkbegin - (un + us)); 2750Sstevel@tonic-gate newline(); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate #endif 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate size_t 2820Sstevel@tonic-gate blklen(q) 2830Sstevel@tonic-gate char *q; 2840Sstevel@tonic-gate { 285527Schin struct blk *pp = (struct blk *)q; 286527Schin struct blk *p; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate --pp; 2890Sstevel@tonic-gate p = (struct blk *)(Rcheat(pp->word) & ~BUSY); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate return ((size_t)((long)p - (long)q)); 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * This is a really hasty hack at putting realloc() in the shell, along 2960Sstevel@tonic-gate * with alloc() and free(). I really hate having to do things like this, 2970Sstevel@tonic-gate * hacking in something before I understand _why_ libcollate does any 2980Sstevel@tonic-gate * memory (re)allocation, let alone feel comfortable with this particular 2990Sstevel@tonic-gate * implementation of realloc, assuming it actually gets used by anything. 3000Sstevel@tonic-gate * 3010Sstevel@tonic-gate * I plan to revist this, for now this is just to get sh to compile so 3020Sstevel@tonic-gate * that xcu4 builds may be done and we get xcu4 on our desktops. 3030Sstevel@tonic-gate * 3040Sstevel@tonic-gate * Eric Brunner, 10/21/94 3050Sstevel@tonic-gate * 3060Sstevel@tonic-gate * Implemented a variation on the suggested fix in Trusted Solaris 2.5, 3070Sstevel@tonic-gate * then forward ported the fix into the mainline shell. 3080Sstevel@tonic-gate * 3090Sstevel@tonic-gate * 3/3/99 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate #ifdef __STDC__ 3120Sstevel@tonic-gate void * 3130Sstevel@tonic-gate realloc(pp, nbytes) 3140Sstevel@tonic-gate void *pp; 3150Sstevel@tonic-gate size_t nbytes; 3160Sstevel@tonic-gate #else 3170Sstevel@tonic-gate char * 3180Sstevel@tonic-gate realloc(pp, nbytes) 3190Sstevel@tonic-gate char *pp; 3200Sstevel@tonic-gate size_t nbytes; 3210Sstevel@tonic-gate #endif 3220Sstevel@tonic-gate { 3230Sstevel@tonic-gate char *q; 3240Sstevel@tonic-gate size_t blen; 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate if (pp == NULL) 3270Sstevel@tonic-gate return (alloc(nbytes)); 3280Sstevel@tonic-gate if ((nbytes == 0) && (pp != NULL)) 3290Sstevel@tonic-gate free(pp); 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate blen = blklen(pp); 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate if (blen < nbytes) { /* need to grow */ 3340Sstevel@tonic-gate q = alloc(nbytes); 3350Sstevel@tonic-gate memcpy(q, pp, blen); 3360Sstevel@tonic-gate free(pp); 3370Sstevel@tonic-gate return ((char *)q); 3380Sstevel@tonic-gate } else if (blen == nbytes) { /* do nothing */ 3390Sstevel@tonic-gate return (pp); 3400Sstevel@tonic-gate } else { /* free excess */ 3410Sstevel@tonic-gate q = alloc(nbytes); 3420Sstevel@tonic-gate memcpy(q, pp, nbytes); 3430Sstevel@tonic-gate free(pp); 3440Sstevel@tonic-gate return ((char *)q); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate #ifdef undef 3480Sstevel@tonic-gate /* 3490Sstevel@tonic-gate * all of what follows is the _idea_ of what is going to be done 3500Sstevel@tonic-gate * getting the size of the block is a problem -- what follows 3510Sstevel@tonic-gate * is _not_ "real", since "sizeof" isn't going to tell me any 3520Sstevel@tonic-gate * thing usefull, probably have to travers the list to the next 3530Sstevel@tonic-gate * blk, then subtract ptr addrs ... and be careful not to leave 3540Sstevel@tonic-gate * holes. 3550Sstevel@tonic-gate */ 3560Sstevel@tonic-gate p = (struct blk *)pp; 3570Sstevel@tonic-gate if (sizeof (p) < nbytes) { /* need to grow */ 3580Sstevel@tonic-gate q = alloc(nbytes); 3590Sstevel@tonic-gate memcpy(q, pp, sizeof (p)); 3600Sstevel@tonic-gate free(pp); 3610Sstevel@tonic-gate return ((char *)q); 3620Sstevel@tonic-gate } else if (sizeof (p) == nbytes) { /* do nothing */ 3630Sstevel@tonic-gate return (pp); 3640Sstevel@tonic-gate } else { /* free excess */ 3650Sstevel@tonic-gate q = alloc(nbytes); 3660Sstevel@tonic-gate memcpy(q, pp, nbytes); 3670Sstevel@tonic-gate free(pp); 3680Sstevel@tonic-gate return ((char *)q); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate #endif 3710Sstevel@tonic-gate } 372