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 */ 22*965005c8Schin 237c478bd9Sstevel@tonic-gate /* 24*965005c8Schin * Copyright 2001 Sun Microsystems, Inc. All rights reserved. 25*965005c8Schin * 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 54*965005c8Schin void addblok(unsigned int); 55*965005c8Schin 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 { 64*965005c8Schin 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; 73*965005c8Schin struct blk *p = blokp; 74*965005c8Schin 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 100*965005c8Schin void 101*965005c8Schin addblok(unsigned int reqd) 1027c478bd9Sstevel@tonic-gate { 1037c478bd9Sstevel@tonic-gate if (stakbot == 0) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate brkbegin = setbrk(3 * BRKINCR); 1067c478bd9Sstevel@tonic-gate bloktop = (struct blk *)brkbegin; 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate if (stakbas != staktop) 1107c478bd9Sstevel@tonic-gate { 111*965005c8Schin unsigned char *rndstak; 112*965005c8Schin struct blk *blokstak; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate if (staktop >= brkend) 1157c478bd9Sstevel@tonic-gate growstak(staktop); 1167c478bd9Sstevel@tonic-gate pushstak(0); 1177c478bd9Sstevel@tonic-gate rndstak = (unsigned char *)round(staktop, BYTESPERWORD); 1187c478bd9Sstevel@tonic-gate blokstak = (struct blk *)(stakbas) - 1; 1197c478bd9Sstevel@tonic-gate blokstak->word = stakbsy; 1207c478bd9Sstevel@tonic-gate stakbsy = blokstak; 1217c478bd9Sstevel@tonic-gate bloktop->word = (struct blk *)(Rcheat(rndstak) | BUSY); 1227c478bd9Sstevel@tonic-gate bloktop = (struct blk *)(rndstak); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate reqd += brkincr; 1257c478bd9Sstevel@tonic-gate reqd &= ~(brkincr - 1); 1267c478bd9Sstevel@tonic-gate blokp = bloktop; 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * brkend points to the first invalid address. 1297c478bd9Sstevel@tonic-gate * make sure bloktop is valid. 1307c478bd9Sstevel@tonic-gate */ 1317c478bd9Sstevel@tonic-gate if ((unsigned char *)&bloktop->word >= brkend) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate if (setbrk((unsigned)((unsigned char *) 1347c478bd9Sstevel@tonic-gate (&bloktop->word) - brkend + sizeof (struct blk))) == 1357c478bd9Sstevel@tonic-gate (unsigned char *)-1) 1367c478bd9Sstevel@tonic-gate error(nospace); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate bloktop = bloktop->word = (struct blk *)(Rcheat(bloktop) + reqd); 1397c478bd9Sstevel@tonic-gate if ((unsigned char *)&bloktop->word >= brkend) 1407c478bd9Sstevel@tonic-gate { 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 { 148*965005c8Schin unsigned char *stakadr = (unsigned char *) 1497c478bd9Sstevel@tonic-gate (bloktop + 2); 150*965005c8Schin unsigned char *sp = stakadr; 1517c478bd9Sstevel@tonic-gate if (reqd = (staktop-stakbot)) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate if (stakadr + reqd >= brkend) 1547c478bd9Sstevel@tonic-gate growstak(stakadr + reqd); 1557c478bd9Sstevel@tonic-gate while (reqd-- > 0) 1567c478bd9Sstevel@tonic-gate *sp++ = *stakbot++; 1577c478bd9Sstevel@tonic-gate sp--; 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate staktop = sp; 1607c478bd9Sstevel@tonic-gate if (staktop >= brkend) 1617c478bd9Sstevel@tonic-gate growstak(staktop); 1627c478bd9Sstevel@tonic-gate stakbas = stakbot = stakadr; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate void 1677c478bd9Sstevel@tonic-gate free(ap) 1687c478bd9Sstevel@tonic-gate void *ap; 1697c478bd9Sstevel@tonic-gate { 170*965005c8Schin struct blk *p; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate if ((p = (struct blk *)ap) && p < bloktop && p > (struct blk *)brkbegin) 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate #ifdef DEBUG 1757c478bd9Sstevel@tonic-gate chkbptr(p); 1767c478bd9Sstevel@tonic-gate #endif 1777c478bd9Sstevel@tonic-gate --p; 1787c478bd9Sstevel@tonic-gate p->word = (struct blk *)(Rcheat(p->word) & ~BUSY); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate #ifdef DEBUG 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate chkbptr(ptr) 1887c478bd9Sstevel@tonic-gate struct blk *ptr; 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate int exf = 0; 191*965005c8Schin struct blk *p = (struct blk *)brkbegin; 192*965005c8Schin struct blk *q; 1937c478bd9Sstevel@tonic-gate int us = 0, un = 0; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate for (;;) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate q = (struct blk *)(Rcheat(p->word) & ~BUSY); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if (p+1 == ptr) 2007c478bd9Sstevel@tonic-gate exf++; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate if (q < (struct blk *)brkbegin || q > bloktop) 2037c478bd9Sstevel@tonic-gate abort(3); 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (p == bloktop) 2067c478bd9Sstevel@tonic-gate break; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate if (busy(p)) 2097c478bd9Sstevel@tonic-gate us += q - p; 2107c478bd9Sstevel@tonic-gate else 2117c478bd9Sstevel@tonic-gate un += q - p; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate if (p >= q) 2147c478bd9Sstevel@tonic-gate abort(4); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate p = q; 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate if (exf == 0) 2197c478bd9Sstevel@tonic-gate abort(1); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate chkmem() 2247c478bd9Sstevel@tonic-gate { 225*965005c8Schin struct blk *p = (struct blk *)brkbegin; 226*965005c8Schin struct blk *q; 2277c478bd9Sstevel@tonic-gate int us = 0, un = 0; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate for (;;) { 2307c478bd9Sstevel@tonic-gate q = (struct blk *)(Rcheat(p->word) & ~BUSY); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if (q < (struct blk *)brkbegin || q > bloktop) 2337c478bd9Sstevel@tonic-gate abort(3); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (p == bloktop) 2367c478bd9Sstevel@tonic-gate break; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate if (busy(p)) 2397c478bd9Sstevel@tonic-gate us += q - p; 2407c478bd9Sstevel@tonic-gate else 2417c478bd9Sstevel@tonic-gate un += q - p; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate if (p >= q) 2447c478bd9Sstevel@tonic-gate abort(4); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate p = q; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate prs("un/used/avail "); 2507c478bd9Sstevel@tonic-gate prn(un); 2517c478bd9Sstevel@tonic-gate blank(); 2527c478bd9Sstevel@tonic-gate prn(us); 2537c478bd9Sstevel@tonic-gate blank(); 2547c478bd9Sstevel@tonic-gate prn((char *)bloktop - brkbegin - (un + us)); 2557c478bd9Sstevel@tonic-gate newline(); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate #endif 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate size_t 2627c478bd9Sstevel@tonic-gate blklen(q) 2637c478bd9Sstevel@tonic-gate char *q; 2647c478bd9Sstevel@tonic-gate { 265*965005c8Schin struct blk *pp = (struct blk *)q; 266*965005c8Schin struct blk *p; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate --pp; 2697c478bd9Sstevel@tonic-gate p = (struct blk *)(Rcheat(pp->word) & ~BUSY); 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate return ((size_t)((long)p - (long)q)); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate /* 2757c478bd9Sstevel@tonic-gate * This is a really hasty hack at putting realloc() in the shell, along 2767c478bd9Sstevel@tonic-gate * with alloc() and free(). I really hate having to do things like this, 2777c478bd9Sstevel@tonic-gate * hacking in something before I understand _why_ libcollate does any 2787c478bd9Sstevel@tonic-gate * memory (re)allocation, let alone feel comfortable with this particular 2797c478bd9Sstevel@tonic-gate * implementation of realloc, assuming it actually gets used by anything. 2807c478bd9Sstevel@tonic-gate * 2817c478bd9Sstevel@tonic-gate * I plan to revist this, for now this is just to get sh to compile so 2827c478bd9Sstevel@tonic-gate * that xcu4 builds may be done and we get xcu4 on our desktops. 2837c478bd9Sstevel@tonic-gate * 2847c478bd9Sstevel@tonic-gate * Eric Brunner, 10/21/94 2857c478bd9Sstevel@tonic-gate * 2867c478bd9Sstevel@tonic-gate * Implemented a variation on the suggested fix in Trusted Solaris 2.5, 2877c478bd9Sstevel@tonic-gate * then forward ported the fix into the mainline shell. 2887c478bd9Sstevel@tonic-gate * 2897c478bd9Sstevel@tonic-gate * 3/3/99 2907c478bd9Sstevel@tonic-gate */ 2917c478bd9Sstevel@tonic-gate #ifdef __STDC__ 2927c478bd9Sstevel@tonic-gate void * 2937c478bd9Sstevel@tonic-gate realloc(pp, nbytes) 2947c478bd9Sstevel@tonic-gate void *pp; 2957c478bd9Sstevel@tonic-gate size_t nbytes; 2967c478bd9Sstevel@tonic-gate #else 2977c478bd9Sstevel@tonic-gate char * 2987c478bd9Sstevel@tonic-gate realloc(pp, nbytes) 2997c478bd9Sstevel@tonic-gate char *pp; 3007c478bd9Sstevel@tonic-gate size_t nbytes; 3017c478bd9Sstevel@tonic-gate #endif 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate char *q; 3047c478bd9Sstevel@tonic-gate size_t blen; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate if (pp == NULL) 3077c478bd9Sstevel@tonic-gate return (alloc(nbytes)); 3087c478bd9Sstevel@tonic-gate if ((nbytes == 0) && (pp != NULL)) 3097c478bd9Sstevel@tonic-gate free(pp); 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate blen = blklen(pp); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate if (blen < nbytes) { /* need to grow */ 3147c478bd9Sstevel@tonic-gate q = alloc(nbytes); 3157c478bd9Sstevel@tonic-gate memcpy(q, pp, blen); 3167c478bd9Sstevel@tonic-gate free(pp); 3177c478bd9Sstevel@tonic-gate return ((char *)q); 3187c478bd9Sstevel@tonic-gate } else if (blen == nbytes) { /* do nothing */ 3197c478bd9Sstevel@tonic-gate return (pp); 3207c478bd9Sstevel@tonic-gate } else { /* free excess */ 3217c478bd9Sstevel@tonic-gate q = alloc(nbytes); 3227c478bd9Sstevel@tonic-gate memcpy(q, pp, nbytes); 3237c478bd9Sstevel@tonic-gate free(pp); 3247c478bd9Sstevel@tonic-gate return ((char *)q); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate #ifdef undef 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * all of what follows is the _idea_ of what is going to be done 3307c478bd9Sstevel@tonic-gate * getting the size of the block is a problem -- what follows 3317c478bd9Sstevel@tonic-gate * is _not_ "real", since "sizeof" isn't going to tell me any 3327c478bd9Sstevel@tonic-gate * thing usefull, probably have to travers the list to the next 3337c478bd9Sstevel@tonic-gate * blk, then subtract ptr addrs ... and be careful not to leave 3347c478bd9Sstevel@tonic-gate * holes. 3357c478bd9Sstevel@tonic-gate */ 3367c478bd9Sstevel@tonic-gate p = (struct blk *)pp; 3377c478bd9Sstevel@tonic-gate if (sizeof (p) < nbytes) { /* need to grow */ 3387c478bd9Sstevel@tonic-gate q = alloc(nbytes); 3397c478bd9Sstevel@tonic-gate memcpy(q, pp, sizeof (p)); 3407c478bd9Sstevel@tonic-gate free(pp); 3417c478bd9Sstevel@tonic-gate return ((char *)q); 3427c478bd9Sstevel@tonic-gate } else if (sizeof (p) == nbytes) { /* do nothing */ 3437c478bd9Sstevel@tonic-gate return (pp); 3447c478bd9Sstevel@tonic-gate } else { /* free excess */ 3457c478bd9Sstevel@tonic-gate q = alloc(nbytes); 3467c478bd9Sstevel@tonic-gate memcpy(q, pp, nbytes); 3477c478bd9Sstevel@tonic-gate free(pp); 3487c478bd9Sstevel@tonic-gate return ((char *)q); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate #endif 3517c478bd9Sstevel@tonic-gate } 352