memalloc.c revision 80381
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)memalloc.c 8.3 (Berkeley) 5/4/95"; 4036150Scharnier#endif 4136150Scharnierstatic const char rcsid[] = 4250471Speter "$FreeBSD: head/bin/sh/memalloc.c 80381 2001-07-26 11:02:39Z sheldonh $"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 451556Srgrimes#include "shell.h" 461556Srgrimes#include "output.h" 471556Srgrimes#include "memalloc.h" 481556Srgrimes#include "error.h" 491556Srgrimes#include "machdep.h" 501556Srgrimes#include "mystring.h" 5139049Scracauer#include "expand.h" 5217987Speter#include <stdlib.h> 5317987Speter#include <unistd.h> 541556Srgrimes 551556Srgrimes/* 561556Srgrimes * Like malloc, but returns an error when out of space. 571556Srgrimes */ 581556Srgrimes 591556Srgrimespointer 6020425Ssteveckmalloc(nbytes) 6117987Speter int nbytes; 6217987Speter{ 6325222Ssteve pointer p; 641556Srgrimes 651556Srgrimes if ((p = malloc(nbytes)) == NULL) 661556Srgrimes error("Out of space"); 671556Srgrimes return p; 681556Srgrimes} 691556Srgrimes 701556Srgrimes 711556Srgrimes/* 721556Srgrimes * Same for realloc. 731556Srgrimes */ 741556Srgrimes 751556Srgrimespointer 761556Srgrimesckrealloc(p, nbytes) 7725222Ssteve pointer p; 7817987Speter int nbytes; 7917987Speter{ 801556Srgrimes if ((p = realloc(p, nbytes)) == NULL) 811556Srgrimes error("Out of space"); 821556Srgrimes return p; 831556Srgrimes} 841556Srgrimes 851556Srgrimes 861556Srgrimes/* 871556Srgrimes * Make a copy of a string in safe storage. 881556Srgrimes */ 891556Srgrimes 901556Srgrimeschar * 911556Srgrimessavestr(s) 921556Srgrimes char *s; 9345618Scracauer{ 9425222Ssteve char *p; 951556Srgrimes 961556Srgrimes p = ckmalloc(strlen(s) + 1); 971556Srgrimes scopy(s, p); 981556Srgrimes return p; 991556Srgrimes} 1001556Srgrimes 1011556Srgrimes 1021556Srgrimes/* 1031556Srgrimes * Parse trees for commands are allocated in lifo order, so we use a stack 1041556Srgrimes * to make this more efficient, and also to avoid all sorts of exception 1051556Srgrimes * handling code to handle interrupts in the middle of a parse. 1061556Srgrimes * 1071556Srgrimes * The size 504 was chosen because the Ultrix malloc handles that size 1081556Srgrimes * well. 1091556Srgrimes */ 1101556Srgrimes 1111556Srgrimes#define MINSIZE 504 /* minimum size of a block */ 1121556Srgrimes 1131556Srgrimes 1141556Srgrimesstruct stack_block { 1151556Srgrimes struct stack_block *prev; 1161556Srgrimes char space[MINSIZE]; 1171556Srgrimes}; 1181556Srgrimes 1191556Srgrimesstruct stack_block stackbase; 1201556Srgrimesstruct stack_block *stackp = &stackbase; 12164702Scracauerstruct stackmark *markp; 1221556Srgrimeschar *stacknxt = stackbase.space; 1231556Srgrimesint stacknleft = MINSIZE; 1241556Srgrimesint sstrnleft; 1251556Srgrimesint herefd = -1; 1261556Srgrimes 1271556Srgrimes 1281556Srgrimes 1291556Srgrimespointer 13020425Sstevestalloc(nbytes) 13117987Speter int nbytes; 13217987Speter{ 13325222Ssteve char *p; 1341556Srgrimes 1351556Srgrimes nbytes = ALIGN(nbytes); 1361556Srgrimes if (nbytes > stacknleft) { 1371556Srgrimes int blocksize; 1381556Srgrimes struct stack_block *sp; 1391556Srgrimes 1401556Srgrimes blocksize = nbytes; 1411556Srgrimes if (blocksize < MINSIZE) 1421556Srgrimes blocksize = MINSIZE; 1431556Srgrimes INTOFF; 14445618Scracauer sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + 14545618Scracauer blocksize); 1461556Srgrimes sp->prev = stackp; 1471556Srgrimes stacknxt = sp->space; 1481556Srgrimes stacknleft = blocksize; 1491556Srgrimes stackp = sp; 1501556Srgrimes INTON; 1511556Srgrimes } 1521556Srgrimes p = stacknxt; 1531556Srgrimes stacknxt += nbytes; 1541556Srgrimes stacknleft -= nbytes; 1551556Srgrimes return p; 1561556Srgrimes} 1571556Srgrimes 1581556Srgrimes 1591556Srgrimesvoid 1601556Srgrimesstunalloc(p) 1611556Srgrimes pointer p; 16245618Scracauer{ 1631556Srgrimes if (p == NULL) { /*DEBUG */ 16480381Ssheldonh write(STDERR_FILENO, "stunalloc\n", 10); 1651556Srgrimes abort(); 1661556Srgrimes } 1671556Srgrimes stacknleft += stacknxt - (char *)p; 1681556Srgrimes stacknxt = p; 1691556Srgrimes} 1701556Srgrimes 1711556Srgrimes 1721556Srgrimes 1731556Srgrimesvoid 1741556Srgrimessetstackmark(mark) 1751556Srgrimes struct stackmark *mark; 17645618Scracauer{ 1771556Srgrimes mark->stackp = stackp; 1781556Srgrimes mark->stacknxt = stacknxt; 1791556Srgrimes mark->stacknleft = stacknleft; 18064702Scracauer mark->marknext = markp; 18164702Scracauer markp = mark; 1821556Srgrimes} 1831556Srgrimes 1841556Srgrimes 1851556Srgrimesvoid 1861556Srgrimespopstackmark(mark) 1871556Srgrimes struct stackmark *mark; 18845618Scracauer{ 1891556Srgrimes struct stack_block *sp; 1901556Srgrimes 1911556Srgrimes INTOFF; 19264702Scracauer markp = mark->marknext; 1931556Srgrimes while (stackp != mark->stackp) { 1941556Srgrimes sp = stackp; 1951556Srgrimes stackp = sp->prev; 1961556Srgrimes ckfree(sp); 1971556Srgrimes } 1981556Srgrimes stacknxt = mark->stacknxt; 1991556Srgrimes stacknleft = mark->stacknleft; 2001556Srgrimes INTON; 2011556Srgrimes} 2021556Srgrimes 2031556Srgrimes 2041556Srgrimes/* 2051556Srgrimes * When the parser reads in a string, it wants to stick the string on the 2061556Srgrimes * stack and only adjust the stack pointer when it knows how big the 2071556Srgrimes * string is. Stackblock (defined in stack.h) returns a pointer to a block 2081556Srgrimes * of space on top of the stack and stackblocklen returns the length of 2091556Srgrimes * this block. Growstackblock will grow this space by at least one byte, 2101556Srgrimes * possibly moving it (like realloc). Grabstackblock actually allocates the 2111556Srgrimes * part of the block that has been used. 2121556Srgrimes */ 2131556Srgrimes 2141556Srgrimesvoid 21545618Scracauergrowstackblock() 21645618Scracauer{ 2171556Srgrimes char *p; 21845618Scracauer int newlen; 21945618Scracauer char *oldspace; 22045618Scracauer int oldlen; 2211556Srgrimes struct stack_block *sp; 22264702Scracauer struct stack_block *oldstackp; 2231556Srgrimes 22445618Scracauer newlen = ALIGN(stacknleft * 2 + 100); 22545618Scracauer oldspace = stacknxt; 22645618Scracauer oldlen = stacknleft; 22745618Scracauer 2281556Srgrimes if (stacknxt == stackp->space && stackp != &stackbase) { 2291556Srgrimes INTOFF; 23064702Scracauer oldstackp = stackp; 2311556Srgrimes sp = stackp; 2321556Srgrimes stackp = sp->prev; 23345618Scracauer sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - 23445618Scracauer MINSIZE + newlen); 2351556Srgrimes sp->prev = stackp; 2361556Srgrimes stackp = sp; 2371556Srgrimes stacknxt = sp->space; 2381556Srgrimes stacknleft = newlen; 23964702Scracauer { 24064702Scracauer /* Stack marks pointing to the start of the old block 24164702Scracauer * must be relocated to point to the new block 24264702Scracauer */ 24364702Scracauer struct stackmark *xmark; 24464702Scracauer xmark = markp; 24564702Scracauer while (xmark != NULL && xmark->stackp == oldstackp) { 24664702Scracauer xmark->stackp = stackp; 24764702Scracauer xmark->stacknxt = stacknxt; 24864702Scracauer xmark->stacknleft = stacknleft; 24964702Scracauer xmark = xmark->marknext; 25064702Scracauer } 25164702Scracauer } 2521556Srgrimes INTON; 2531556Srgrimes } else { 2541556Srgrimes p = stalloc(newlen); 25517987Speter memcpy(p, oldspace, oldlen); 2561556Srgrimes stacknxt = p; /* free the space */ 25718018Speter stacknleft += newlen; /* we just allocated */ 2581556Srgrimes } 2591556Srgrimes} 2601556Srgrimes 2611556Srgrimes 2621556Srgrimes 2631556Srgrimesvoid 26420425Sstevegrabstackblock(len) 26517987Speter int len; 26617987Speter{ 2671556Srgrimes len = ALIGN(len); 2681556Srgrimes stacknxt += len; 2691556Srgrimes stacknleft -= len; 2701556Srgrimes} 2711556Srgrimes 2721556Srgrimes 2731556Srgrimes 2741556Srgrimes/* 2751556Srgrimes * The following routines are somewhat easier to use that the above. 2761556Srgrimes * The user declares a variable of type STACKSTR, which may be declared 2771556Srgrimes * to be a register. The macro STARTSTACKSTR initializes things. Then 2781556Srgrimes * the user uses the macro STPUTC to add characters to the string. In 2791556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is 2801556Srgrimes * grown as necessary. When the user is done, she can just leave the 2811556Srgrimes * string there and refer to it using stackblock(). Or she can allocate 2821556Srgrimes * the space for it using grabstackstr(). If it is necessary to allow 2831556Srgrimes * someone else to use the stack temporarily and then continue to grow 2841556Srgrimes * the string, the user should use grabstack to allocate the space, and 2851556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation. 2861556Srgrimes * 2871556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow. 2881556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there 2891556Srgrimes * is space for at least one character. 2901556Srgrimes */ 2911556Srgrimes 2921556Srgrimes 2931556Srgrimeschar * 29445618Scracauergrowstackstr() 29545618Scracauer{ 29645618Scracauer int len; 29745618Scracauer 29845618Scracauer len = stackblocksize(); 2991556Srgrimes if (herefd >= 0 && len >= 1024) { 30039137Stegge xwrite(herefd, stackblock(), len); 3011556Srgrimes sstrnleft = len - 1; 3021556Srgrimes return stackblock(); 3031556Srgrimes } 3041556Srgrimes growstackblock(); 3051556Srgrimes sstrnleft = stackblocksize() - len - 1; 3061556Srgrimes return stackblock() + len; 3071556Srgrimes} 3081556Srgrimes 3091556Srgrimes 3101556Srgrimes/* 3111556Srgrimes * Called from CHECKSTRSPACE. 3121556Srgrimes */ 3131556Srgrimes 3141556Srgrimeschar * 31545618Scracauermakestrspace() 31645618Scracauer{ 31745618Scracauer int len; 31845618Scracauer 31945618Scracauer len = stackblocksize() - sstrnleft; 3201556Srgrimes growstackblock(); 3211556Srgrimes sstrnleft = stackblocksize() - len; 3221556Srgrimes return stackblock() + len; 3231556Srgrimes} 3241556Srgrimes 3251556Srgrimes 3261556Srgrimes 3271556Srgrimesvoid 3281556Srgrimesungrabstackstr(s, p) 3291556Srgrimes char *s; 3301556Srgrimes char *p; 33145618Scracauer{ 3321556Srgrimes stacknleft += stacknxt - s; 3331556Srgrimes stacknxt = s; 3341556Srgrimes sstrnleft = stacknleft - (p - s); 3351556Srgrimes} 336