memalloc.c revision 117261
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 411556Srgrimes#endif /* not lint */ 4299110Sobrien#include <sys/cdefs.h> 4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/memalloc.c 117261 2003-07-05 15:18:44Z dds $"); 441556Srgrimes 45111422Smarcel#include <sys/param.h> 461556Srgrimes#include "shell.h" 471556Srgrimes#include "output.h" 481556Srgrimes#include "memalloc.h" 491556Srgrimes#include "error.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 6090111Simpckmalloc(int nbytes) 6117987Speter{ 6225222Ssteve pointer p; 631556Srgrimes 641556Srgrimes if ((p = malloc(nbytes)) == NULL) 651556Srgrimes error("Out of space"); 661556Srgrimes return p; 671556Srgrimes} 681556Srgrimes 691556Srgrimes 701556Srgrimes/* 711556Srgrimes * Same for realloc. 721556Srgrimes */ 731556Srgrimes 741556Srgrimespointer 7590111Simpckrealloc(pointer p, int nbytes) 7617987Speter{ 771556Srgrimes if ((p = realloc(p, nbytes)) == NULL) 781556Srgrimes error("Out of space"); 791556Srgrimes return p; 801556Srgrimes} 811556Srgrimes 821556Srgrimes 831556Srgrimes/* 841556Srgrimes * Make a copy of a string in safe storage. 851556Srgrimes */ 861556Srgrimes 871556Srgrimeschar * 8890111Simpsavestr(char *s) 8945618Scracauer{ 9025222Ssteve char *p; 911556Srgrimes 921556Srgrimes p = ckmalloc(strlen(s) + 1); 931556Srgrimes scopy(s, p); 941556Srgrimes return p; 951556Srgrimes} 961556Srgrimes 971556Srgrimes 981556Srgrimes/* 991556Srgrimes * Parse trees for commands are allocated in lifo order, so we use a stack 1001556Srgrimes * to make this more efficient, and also to avoid all sorts of exception 1011556Srgrimes * handling code to handle interrupts in the middle of a parse. 1021556Srgrimes * 103111422Smarcel * The size 496 was chosen because with 16-byte alignment the total size 104111422Smarcel * for the allocated block is 512. 1051556Srgrimes */ 1061556Srgrimes 107111422Smarcel#define MINSIZE 496 /* minimum size of a block. */ 1081556Srgrimes 1091556Srgrimes 1101556Srgrimesstruct stack_block { 1111556Srgrimes struct stack_block *prev; 112111422Smarcel /* Data follows */ 1131556Srgrimes}; 114111422Smarcel#define SPACE(sp) ((char*)(sp) + ALIGN(sizeof(struct stack_block))) 1151556Srgrimes 116117261SddsSTATIC struct stack_block *stackp; 117117261SddsSTATIC struct stackmark *markp; 118111422Smarcelchar *stacknxt; 119111422Smarcelint stacknleft; 1201556Srgrimesint sstrnleft; 1211556Srgrimesint herefd = -1; 1221556Srgrimes 1231556Srgrimes 124111422Smarcelstatic void 125111422Smarcelstnewblock(int nbytes) 126111422Smarcel{ 127111422Smarcel struct stack_block *sp; 128111422Smarcel int allocsize; 1291556Srgrimes 130111422Smarcel if (nbytes < MINSIZE) 131111422Smarcel nbytes = MINSIZE; 132111422Smarcel 133111422Smarcel allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes); 134111422Smarcel 135111422Smarcel INTOFF; 136111422Smarcel sp = ckmalloc(allocsize); 137111422Smarcel sp->prev = stackp; 138111422Smarcel stacknxt = SPACE(sp); 139111422Smarcel stacknleft = allocsize - (stacknxt - (char*)sp); 140111422Smarcel stackp = sp; 141111422Smarcel INTON; 142111422Smarcel} 143111422Smarcel 144111422Smarcel 1451556Srgrimespointer 14690111Simpstalloc(int nbytes) 14717987Speter{ 14825222Ssteve char *p; 1491556Srgrimes 1501556Srgrimes nbytes = ALIGN(nbytes); 151111422Smarcel if (nbytes > stacknleft) 152111422Smarcel stnewblock(nbytes); 1531556Srgrimes p = stacknxt; 1541556Srgrimes stacknxt += nbytes; 1551556Srgrimes stacknleft -= nbytes; 1561556Srgrimes return p; 1571556Srgrimes} 1581556Srgrimes 1591556Srgrimes 1601556Srgrimesvoid 16190111Simpstunalloc(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 17490111Simpsetstackmark(struct stackmark *mark) 17545618Scracauer{ 1761556Srgrimes mark->stackp = stackp; 1771556Srgrimes mark->stacknxt = stacknxt; 1781556Srgrimes mark->stacknleft = stacknleft; 17964702Scracauer mark->marknext = markp; 18064702Scracauer markp = mark; 1811556Srgrimes} 1821556Srgrimes 1831556Srgrimes 1841556Srgrimesvoid 18590111Simppopstackmark(struct stackmark *mark) 18645618Scracauer{ 1871556Srgrimes struct stack_block *sp; 1881556Srgrimes 1891556Srgrimes INTOFF; 19064702Scracauer markp = mark->marknext; 1911556Srgrimes while (stackp != mark->stackp) { 1921556Srgrimes sp = stackp; 1931556Srgrimes stackp = sp->prev; 1941556Srgrimes ckfree(sp); 1951556Srgrimes } 1961556Srgrimes stacknxt = mark->stacknxt; 1971556Srgrimes stacknleft = mark->stacknleft; 1981556Srgrimes INTON; 1991556Srgrimes} 2001556Srgrimes 2011556Srgrimes 2021556Srgrimes/* 2031556Srgrimes * When the parser reads in a string, it wants to stick the string on the 2041556Srgrimes * stack and only adjust the stack pointer when it knows how big the 2051556Srgrimes * string is. Stackblock (defined in stack.h) returns a pointer to a block 2061556Srgrimes * of space on top of the stack and stackblocklen returns the length of 2071556Srgrimes * this block. Growstackblock will grow this space by at least one byte, 2081556Srgrimes * possibly moving it (like realloc). Grabstackblock actually allocates the 2091556Srgrimes * part of the block that has been used. 2101556Srgrimes */ 2111556Srgrimes 2121556Srgrimesvoid 21390111Simpgrowstackblock(void) 21445618Scracauer{ 2151556Srgrimes char *p; 21645618Scracauer int newlen; 21745618Scracauer char *oldspace; 21845618Scracauer int oldlen; 2191556Srgrimes struct stack_block *sp; 22064702Scracauer struct stack_block *oldstackp; 221111422Smarcel struct stackmark *xmark; 2221556Srgrimes 223111422Smarcel newlen = (stacknleft == 0) ? MINSIZE : stacknleft * 2 + 100; 224111422Smarcel newlen = ALIGN(newlen); 22545618Scracauer oldspace = stacknxt; 22645618Scracauer oldlen = stacknleft; 22745618Scracauer 228111422Smarcel if (stackp != NULL && stacknxt == SPACE(stackp)) { 2291556Srgrimes INTOFF; 23064702Scracauer oldstackp = stackp; 231111422Smarcel stackp = oldstackp->prev; 232111422Smarcel sp = ckrealloc((pointer)oldstackp, newlen); 2331556Srgrimes sp->prev = stackp; 2341556Srgrimes stackp = sp; 235111422Smarcel stacknxt = SPACE(sp); 236111422Smarcel stacknleft = newlen - (stacknxt - (char*)sp); 237111422Smarcel 238111422Smarcel /* 239111422Smarcel * Stack marks pointing to the start of the old block 240111422Smarcel * must be relocated to point to the new block 241111422Smarcel */ 242111422Smarcel xmark = markp; 243111422Smarcel while (xmark != NULL && xmark->stackp == oldstackp) { 244111422Smarcel xmark->stackp = stackp; 245111422Smarcel xmark->stacknxt = stacknxt; 246111422Smarcel xmark->stacknleft = stacknleft; 247111422Smarcel xmark = xmark->marknext; 24864702Scracauer } 2491556Srgrimes INTON; 2501556Srgrimes } else { 2511556Srgrimes p = stalloc(newlen); 252111422Smarcel if (oldlen != 0) 253111422Smarcel memcpy(p, oldspace, oldlen); 254111422Smarcel stunalloc(p); 2551556Srgrimes } 2561556Srgrimes} 2571556Srgrimes 2581556Srgrimes 2591556Srgrimes 2601556Srgrimesvoid 26190111Simpgrabstackblock(int len) 26217987Speter{ 2631556Srgrimes len = ALIGN(len); 2641556Srgrimes stacknxt += len; 2651556Srgrimes stacknleft -= len; 2661556Srgrimes} 2671556Srgrimes 2681556Srgrimes 2691556Srgrimes 2701556Srgrimes/* 2711556Srgrimes * The following routines are somewhat easier to use that the above. 2721556Srgrimes * The user declares a variable of type STACKSTR, which may be declared 2731556Srgrimes * to be a register. The macro STARTSTACKSTR initializes things. Then 2741556Srgrimes * the user uses the macro STPUTC to add characters to the string. In 2751556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is 2761556Srgrimes * grown as necessary. When the user is done, she can just leave the 2771556Srgrimes * string there and refer to it using stackblock(). Or she can allocate 2781556Srgrimes * the space for it using grabstackstr(). If it is necessary to allow 2791556Srgrimes * someone else to use the stack temporarily and then continue to grow 2801556Srgrimes * the string, the user should use grabstack to allocate the space, and 2811556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation. 2821556Srgrimes * 2831556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow. 2841556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there 2851556Srgrimes * is space for at least one character. 2861556Srgrimes */ 2871556Srgrimes 2881556Srgrimes 2891556Srgrimeschar * 29090111Simpgrowstackstr(void) 29145618Scracauer{ 29245618Scracauer int len; 29345618Scracauer 29445618Scracauer len = stackblocksize(); 2951556Srgrimes if (herefd >= 0 && len >= 1024) { 29639137Stegge xwrite(herefd, stackblock(), len); 2971556Srgrimes sstrnleft = len - 1; 2981556Srgrimes return stackblock(); 2991556Srgrimes } 3001556Srgrimes growstackblock(); 3011556Srgrimes sstrnleft = stackblocksize() - len - 1; 3021556Srgrimes return stackblock() + len; 3031556Srgrimes} 3041556Srgrimes 3051556Srgrimes 3061556Srgrimes/* 3071556Srgrimes * Called from CHECKSTRSPACE. 3081556Srgrimes */ 3091556Srgrimes 3101556Srgrimeschar * 31190111Simpmakestrspace(void) 31245618Scracauer{ 31345618Scracauer int len; 31445618Scracauer 31545618Scracauer len = stackblocksize() - sstrnleft; 3161556Srgrimes growstackblock(); 3171556Srgrimes sstrnleft = stackblocksize() - len; 3181556Srgrimes return stackblock() + len; 3191556Srgrimes} 3201556Srgrimes 3211556Srgrimes 3221556Srgrimes 3231556Srgrimesvoid 32490111Simpungrabstackstr(char *s, char *p) 32545618Scracauer{ 3261556Srgrimes stacknleft += stacknxt - s; 3271556Srgrimes stacknxt = s; 3281556Srgrimes sstrnleft = stacknleft - (p - s); 3291556Srgrimes} 330