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