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