memalloc.c revision 3044
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.
353044Sdg *
363044Sdg *	$Id$
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
401556Srgrimesstatic char sccsid[] = "@(#)memalloc.c	8.1 (Berkeley) 5/31/93";
411556Srgrimes#endif /* not lint */
421556Srgrimes
431556Srgrimes#include "shell.h"
441556Srgrimes#include "output.h"
451556Srgrimes#include "memalloc.h"
461556Srgrimes#include "error.h"
471556Srgrimes#include "machdep.h"
481556Srgrimes#include "mystring.h"
491556Srgrimes
501556Srgrimes/*
511556Srgrimes * Like malloc, but returns an error when out of space.
521556Srgrimes */
531556Srgrimes
541556Srgrimespointer
551556Srgrimesckmalloc(nbytes) {
561556Srgrimes	register pointer p;
571556Srgrimes	pointer malloc();
581556Srgrimes
591556Srgrimes	if ((p = malloc(nbytes)) == NULL)
601556Srgrimes		error("Out of space");
611556Srgrimes	return p;
621556Srgrimes}
631556Srgrimes
641556Srgrimes
651556Srgrimes/*
661556Srgrimes * Same for realloc.
671556Srgrimes */
681556Srgrimes
691556Srgrimespointer
701556Srgrimesckrealloc(p, nbytes)
711556Srgrimes	register pointer p;
721556Srgrimes	{
731556Srgrimes	pointer realloc();
741556Srgrimes
751556Srgrimes	if ((p = realloc(p, nbytes)) == NULL)
761556Srgrimes		error("Out of space");
771556Srgrimes	return p;
781556Srgrimes}
791556Srgrimes
801556Srgrimes
811556Srgrimes/*
821556Srgrimes * Make a copy of a string in safe storage.
831556Srgrimes */
841556Srgrimes
851556Srgrimeschar *
861556Srgrimessavestr(s)
871556Srgrimes	char *s;
881556Srgrimes	{
891556Srgrimes	register char *p;
901556Srgrimes
911556Srgrimes	p = ckmalloc(strlen(s) + 1);
921556Srgrimes	scopy(s, p);
931556Srgrimes	return p;
941556Srgrimes}
951556Srgrimes
961556Srgrimes
971556Srgrimes/*
981556Srgrimes * Parse trees for commands are allocated in lifo order, so we use a stack
991556Srgrimes * to make this more efficient, and also to avoid all sorts of exception
1001556Srgrimes * handling code to handle interrupts in the middle of a parse.
1011556Srgrimes *
1021556Srgrimes * The size 504 was chosen because the Ultrix malloc handles that size
1031556Srgrimes * well.
1041556Srgrimes */
1051556Srgrimes
1061556Srgrimes#define MINSIZE 504		/* minimum size of a block */
1071556Srgrimes
1081556Srgrimes
1091556Srgrimesstruct stack_block {
1101556Srgrimes	struct stack_block *prev;
1111556Srgrimes	char space[MINSIZE];
1121556Srgrimes};
1131556Srgrimes
1141556Srgrimesstruct stack_block stackbase;
1151556Srgrimesstruct stack_block *stackp = &stackbase;
1161556Srgrimeschar *stacknxt = stackbase.space;
1171556Srgrimesint stacknleft = MINSIZE;
1181556Srgrimesint sstrnleft;
1191556Srgrimesint herefd = -1;
1201556Srgrimes
1211556Srgrimes
1221556Srgrimes
1231556Srgrimespointer
1241556Srgrimesstalloc(nbytes) {
1251556Srgrimes	register char *p;
1261556Srgrimes
1271556Srgrimes	nbytes = ALIGN(nbytes);
1281556Srgrimes	if (nbytes > stacknleft) {
1291556Srgrimes		int blocksize;
1301556Srgrimes		struct stack_block *sp;
1311556Srgrimes
1321556Srgrimes		blocksize = nbytes;
1331556Srgrimes		if (blocksize < MINSIZE)
1341556Srgrimes			blocksize = MINSIZE;
1351556Srgrimes		INTOFF;
1361556Srgrimes		sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
1371556Srgrimes		sp->prev = stackp;
1381556Srgrimes		stacknxt = sp->space;
1391556Srgrimes		stacknleft = blocksize;
1401556Srgrimes		stackp = sp;
1411556Srgrimes		INTON;
1421556Srgrimes	}
1431556Srgrimes	p = stacknxt;
1441556Srgrimes	stacknxt += nbytes;
1451556Srgrimes	stacknleft -= nbytes;
1461556Srgrimes	return p;
1471556Srgrimes}
1481556Srgrimes
1491556Srgrimes
1501556Srgrimesvoid
1511556Srgrimesstunalloc(p)
1521556Srgrimes	pointer p;
1531556Srgrimes	{
1541556Srgrimes	if (p == NULL) {		/*DEBUG */
1551556Srgrimes		write(2, "stunalloc\n", 10);
1561556Srgrimes		abort();
1571556Srgrimes	}
1581556Srgrimes	stacknleft += stacknxt - (char *)p;
1591556Srgrimes	stacknxt = p;
1601556Srgrimes}
1611556Srgrimes
1621556Srgrimes
1631556Srgrimes
1641556Srgrimesvoid
1651556Srgrimessetstackmark(mark)
1661556Srgrimes	struct stackmark *mark;
1671556Srgrimes	{
1681556Srgrimes	mark->stackp = stackp;
1691556Srgrimes	mark->stacknxt = stacknxt;
1701556Srgrimes	mark->stacknleft = stacknleft;
1711556Srgrimes}
1721556Srgrimes
1731556Srgrimes
1741556Srgrimesvoid
1751556Srgrimespopstackmark(mark)
1761556Srgrimes	struct stackmark *mark;
1771556Srgrimes	{
1781556Srgrimes	struct stack_block *sp;
1791556Srgrimes
1801556Srgrimes	INTOFF;
1811556Srgrimes	while (stackp != mark->stackp) {
1821556Srgrimes		sp = stackp;
1831556Srgrimes		stackp = sp->prev;
1841556Srgrimes		ckfree(sp);
1851556Srgrimes	}
1861556Srgrimes	stacknxt = mark->stacknxt;
1871556Srgrimes	stacknleft = mark->stacknleft;
1881556Srgrimes	INTON;
1891556Srgrimes}
1901556Srgrimes
1911556Srgrimes
1921556Srgrimes/*
1931556Srgrimes * When the parser reads in a string, it wants to stick the string on the
1941556Srgrimes * stack and only adjust the stack pointer when it knows how big the
1951556Srgrimes * string is.  Stackblock (defined in stack.h) returns a pointer to a block
1961556Srgrimes * of space on top of the stack and stackblocklen returns the length of
1971556Srgrimes * this block.  Growstackblock will grow this space by at least one byte,
1981556Srgrimes * possibly moving it (like realloc).  Grabstackblock actually allocates the
1991556Srgrimes * part of the block that has been used.
2001556Srgrimes */
2011556Srgrimes
2021556Srgrimesvoid
2031556Srgrimesgrowstackblock() {
2041556Srgrimes	char *p;
2051556Srgrimes	int newlen = stacknleft * 2 + 100;
2061556Srgrimes	char *oldspace = stacknxt;
2071556Srgrimes	int oldlen = stacknleft;
2081556Srgrimes	struct stack_block *sp;
2091556Srgrimes
2101556Srgrimes	if (stacknxt == stackp->space && stackp != &stackbase) {
2111556Srgrimes		INTOFF;
2121556Srgrimes		sp = stackp;
2131556Srgrimes		stackp = sp->prev;
2141556Srgrimes		sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
2151556Srgrimes		sp->prev = stackp;
2161556Srgrimes		stackp = sp;
2171556Srgrimes		stacknxt = sp->space;
2181556Srgrimes		stacknleft = newlen;
2191556Srgrimes		INTON;
2201556Srgrimes	} else {
2211556Srgrimes		p = stalloc(newlen);
2221556Srgrimes		bcopy(oldspace, p, oldlen);
2231556Srgrimes		stacknxt = p;			/* free the space */
2241556Srgrimes		stacknleft += newlen;		/* we just allocated */
2251556Srgrimes	}
2261556Srgrimes}
2271556Srgrimes
2281556Srgrimes
2291556Srgrimes
2301556Srgrimesvoid
2311556Srgrimesgrabstackblock(len) {
2321556Srgrimes	len = ALIGN(len);
2331556Srgrimes	stacknxt += len;
2341556Srgrimes	stacknleft -= len;
2351556Srgrimes}
2361556Srgrimes
2371556Srgrimes
2381556Srgrimes
2391556Srgrimes/*
2401556Srgrimes * The following routines are somewhat easier to use that the above.
2411556Srgrimes * The user declares a variable of type STACKSTR, which may be declared
2421556Srgrimes * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2431556Srgrimes * the user uses the macro STPUTC to add characters to the string.  In
2441556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2451556Srgrimes * grown as necessary.  When the user is done, she can just leave the
2461556Srgrimes * string there and refer to it using stackblock().  Or she can allocate
2471556Srgrimes * the space for it using grabstackstr().  If it is necessary to allow
2481556Srgrimes * someone else to use the stack temporarily and then continue to grow
2491556Srgrimes * the string, the user should use grabstack to allocate the space, and
2501556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation.
2511556Srgrimes *
2521556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow.
2531556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
2541556Srgrimes * is space for at least one character.
2551556Srgrimes */
2561556Srgrimes
2571556Srgrimes
2581556Srgrimeschar *
2591556Srgrimesgrowstackstr() {
2601556Srgrimes	int len = stackblocksize();
2611556Srgrimes	if (herefd >= 0 && len >= 1024) {
2621556Srgrimes		xwrite(herefd, stackblock(), len);
2631556Srgrimes		sstrnleft = len - 1;
2641556Srgrimes		return stackblock();
2651556Srgrimes	}
2661556Srgrimes	growstackblock();
2671556Srgrimes	sstrnleft = stackblocksize() - len - 1;
2681556Srgrimes	return stackblock() + len;
2691556Srgrimes}
2701556Srgrimes
2711556Srgrimes
2721556Srgrimes/*
2731556Srgrimes * Called from CHECKSTRSPACE.
2741556Srgrimes */
2751556Srgrimes
2761556Srgrimeschar *
2771556Srgrimesmakestrspace() {
2781556Srgrimes	int len = stackblocksize() - sstrnleft;
2791556Srgrimes	growstackblock();
2801556Srgrimes	sstrnleft = stackblocksize() - len;
2811556Srgrimes	return stackblock() + len;
2821556Srgrimes}
2831556Srgrimes
2841556Srgrimes
2851556Srgrimes
2861556Srgrimesvoid
2871556Srgrimesungrabstackstr(s, p)
2881556Srgrimes	char *s;
2891556Srgrimes	char *p;
2901556Srgrimes	{
2911556Srgrimes	stacknleft += stacknxt - s;
2921556Srgrimes	stacknxt = s;
2931556Srgrimes	sstrnleft = stacknleft - (p - s);
2941556Srgrimes}
295