memalloc.c revision 151795
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 * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)memalloc.c	8.3 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/memalloc.c 151795 2005-10-28 10:45:19Z stefanf $");
401556Srgrimes
41111422Smarcel#include <sys/param.h>
421556Srgrimes#include "shell.h"
431556Srgrimes#include "output.h"
441556Srgrimes#include "memalloc.h"
451556Srgrimes#include "error.h"
461556Srgrimes#include "mystring.h"
4739049Scracauer#include "expand.h"
4817987Speter#include <stdlib.h>
4917987Speter#include <unistd.h>
501556Srgrimes
511556Srgrimes/*
521556Srgrimes * Like malloc, but returns an error when out of space.
531556Srgrimes */
541556Srgrimes
551556Srgrimespointer
5690111Simpckmalloc(int nbytes)
5717987Speter{
5825222Ssteve	pointer p;
591556Srgrimes
60151795Sstefanf	INTOFF;
61151795Sstefanf	p = malloc(nbytes);
62151795Sstefanf	INTON;
63151795Sstefanf	if (p == NULL)
641556Srgrimes		error("Out of space");
651556Srgrimes	return p;
661556Srgrimes}
671556Srgrimes
681556Srgrimes
691556Srgrimes/*
701556Srgrimes * Same for realloc.
711556Srgrimes */
721556Srgrimes
731556Srgrimespointer
7490111Simpckrealloc(pointer p, int nbytes)
7517987Speter{
76151795Sstefanf	INTOFF;
77151795Sstefanf	p = realloc(p, nbytes);
78151795Sstefanf	INTON;
79151795Sstefanf	if (p == NULL)
801556Srgrimes		error("Out of space");
811556Srgrimes	return p;
821556Srgrimes}
831556Srgrimes
84151795Sstefanfvoid
85151795Sstefanfckfree(pointer p)
86151795Sstefanf{
87151795Sstefanf	INTOFF;
88151795Sstefanf	free(p);
89151795Sstefanf	INTON;
90151795Sstefanf}
911556Srgrimes
92151795Sstefanf
931556Srgrimes/*
941556Srgrimes * Make a copy of a string in safe storage.
951556Srgrimes */
961556Srgrimes
971556Srgrimeschar *
9890111Simpsavestr(char *s)
9945618Scracauer{
10025222Ssteve	char *p;
1011556Srgrimes
1021556Srgrimes	p = ckmalloc(strlen(s) + 1);
1031556Srgrimes	scopy(s, p);
1041556Srgrimes	return p;
1051556Srgrimes}
1061556Srgrimes
1071556Srgrimes
1081556Srgrimes/*
1091556Srgrimes * Parse trees for commands are allocated in lifo order, so we use a stack
1101556Srgrimes * to make this more efficient, and also to avoid all sorts of exception
1111556Srgrimes * handling code to handle interrupts in the middle of a parse.
1121556Srgrimes *
113111422Smarcel * The size 496 was chosen because with 16-byte alignment the total size
114111422Smarcel * for the allocated block is 512.
1151556Srgrimes */
1161556Srgrimes
117111422Smarcel#define MINSIZE 496		/* minimum size of a block. */
1181556Srgrimes
1191556Srgrimes
1201556Srgrimesstruct stack_block {
1211556Srgrimes	struct stack_block *prev;
122111422Smarcel	/* Data follows */
1231556Srgrimes};
124111422Smarcel#define SPACE(sp)	((char*)(sp) + ALIGN(sizeof(struct stack_block)))
1251556Srgrimes
126117261SddsSTATIC struct stack_block *stackp;
127117261SddsSTATIC struct stackmark *markp;
128111422Smarcelchar *stacknxt;
129111422Smarcelint stacknleft;
1301556Srgrimesint sstrnleft;
1311556Srgrimesint herefd = -1;
1321556Srgrimes
1331556Srgrimes
134111422Smarcelstatic void
135111422Smarcelstnewblock(int nbytes)
136111422Smarcel{
137111422Smarcel	struct stack_block *sp;
138111422Smarcel	int allocsize;
1391556Srgrimes
140111422Smarcel	if (nbytes < MINSIZE)
141111422Smarcel		nbytes = MINSIZE;
142111422Smarcel
143111422Smarcel	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
144111422Smarcel
145111422Smarcel	INTOFF;
146111422Smarcel	sp = ckmalloc(allocsize);
147111422Smarcel	sp->prev = stackp;
148111422Smarcel	stacknxt = SPACE(sp);
149111422Smarcel	stacknleft = allocsize - (stacknxt - (char*)sp);
150111422Smarcel	stackp = sp;
151111422Smarcel	INTON;
152111422Smarcel}
153111422Smarcel
154111422Smarcel
1551556Srgrimespointer
15690111Simpstalloc(int nbytes)
15717987Speter{
15825222Ssteve	char *p;
1591556Srgrimes
1601556Srgrimes	nbytes = ALIGN(nbytes);
161111422Smarcel	if (nbytes > stacknleft)
162111422Smarcel		stnewblock(nbytes);
1631556Srgrimes	p = stacknxt;
1641556Srgrimes	stacknxt += nbytes;
1651556Srgrimes	stacknleft -= nbytes;
1661556Srgrimes	return p;
1671556Srgrimes}
1681556Srgrimes
1691556Srgrimes
1701556Srgrimesvoid
17190111Simpstunalloc(pointer p)
17245618Scracauer{
1731556Srgrimes	if (p == NULL) {		/*DEBUG */
17480381Ssheldonh		write(STDERR_FILENO, "stunalloc\n", 10);
1751556Srgrimes		abort();
1761556Srgrimes	}
1771556Srgrimes	stacknleft += stacknxt - (char *)p;
1781556Srgrimes	stacknxt = p;
1791556Srgrimes}
1801556Srgrimes
1811556Srgrimes
1821556Srgrimes
1831556Srgrimesvoid
18490111Simpsetstackmark(struct stackmark *mark)
18545618Scracauer{
1861556Srgrimes	mark->stackp = stackp;
1871556Srgrimes	mark->stacknxt = stacknxt;
1881556Srgrimes	mark->stacknleft = stacknleft;
18964702Scracauer	mark->marknext = markp;
19064702Scracauer	markp = mark;
1911556Srgrimes}
1921556Srgrimes
1931556Srgrimes
1941556Srgrimesvoid
19590111Simppopstackmark(struct stackmark *mark)
19645618Scracauer{
1971556Srgrimes	struct stack_block *sp;
1981556Srgrimes
1991556Srgrimes	INTOFF;
20064702Scracauer	markp = mark->marknext;
2011556Srgrimes	while (stackp != mark->stackp) {
2021556Srgrimes		sp = stackp;
2031556Srgrimes		stackp = sp->prev;
2041556Srgrimes		ckfree(sp);
2051556Srgrimes	}
2061556Srgrimes	stacknxt = mark->stacknxt;
2071556Srgrimes	stacknleft = mark->stacknleft;
2081556Srgrimes	INTON;
2091556Srgrimes}
2101556Srgrimes
2111556Srgrimes
2121556Srgrimes/*
2131556Srgrimes * When the parser reads in a string, it wants to stick the string on the
2141556Srgrimes * stack and only adjust the stack pointer when it knows how big the
2151556Srgrimes * string is.  Stackblock (defined in stack.h) returns a pointer to a block
2161556Srgrimes * of space on top of the stack and stackblocklen returns the length of
2171556Srgrimes * this block.  Growstackblock will grow this space by at least one byte,
2181556Srgrimes * possibly moving it (like realloc).  Grabstackblock actually allocates the
2191556Srgrimes * part of the block that has been used.
2201556Srgrimes */
2211556Srgrimes
2221556Srgrimesvoid
22390111Simpgrowstackblock(void)
22445618Scracauer{
2251556Srgrimes	char *p;
22645618Scracauer	int newlen;
22745618Scracauer	char *oldspace;
22845618Scracauer	int oldlen;
2291556Srgrimes	struct stack_block *sp;
23064702Scracauer	struct stack_block *oldstackp;
231111422Smarcel	struct stackmark *xmark;
2321556Srgrimes
233111422Smarcel	newlen = (stacknleft == 0) ? MINSIZE : stacknleft * 2 + 100;
234111422Smarcel	newlen = ALIGN(newlen);
23545618Scracauer	oldspace = stacknxt;
23645618Scracauer	oldlen = stacknleft;
23745618Scracauer
238111422Smarcel	if (stackp != NULL && stacknxt == SPACE(stackp)) {
2391556Srgrimes		INTOFF;
24064702Scracauer		oldstackp = stackp;
241111422Smarcel		stackp = oldstackp->prev;
242111422Smarcel		sp = ckrealloc((pointer)oldstackp, newlen);
2431556Srgrimes		sp->prev = stackp;
2441556Srgrimes		stackp = sp;
245111422Smarcel		stacknxt = SPACE(sp);
246111422Smarcel		stacknleft = newlen - (stacknxt - (char*)sp);
247111422Smarcel
248111422Smarcel		/*
249111422Smarcel		 * Stack marks pointing to the start of the old block
250111422Smarcel		 * must be relocated to point to the new block
251111422Smarcel		 */
252111422Smarcel		xmark = markp;
253111422Smarcel		while (xmark != NULL && xmark->stackp == oldstackp) {
254111422Smarcel			xmark->stackp = stackp;
255111422Smarcel			xmark->stacknxt = stacknxt;
256111422Smarcel			xmark->stacknleft = stacknleft;
257111422Smarcel			xmark = xmark->marknext;
25864702Scracauer		}
2591556Srgrimes		INTON;
2601556Srgrimes	} else {
2611556Srgrimes		p = stalloc(newlen);
262111422Smarcel		if (oldlen != 0)
263111422Smarcel			memcpy(p, oldspace, oldlen);
264111422Smarcel		stunalloc(p);
2651556Srgrimes	}
2661556Srgrimes}
2671556Srgrimes
2681556Srgrimes
2691556Srgrimes
2701556Srgrimesvoid
27190111Simpgrabstackblock(int len)
27217987Speter{
2731556Srgrimes	len = ALIGN(len);
2741556Srgrimes	stacknxt += len;
2751556Srgrimes	stacknleft -= len;
2761556Srgrimes}
2771556Srgrimes
2781556Srgrimes
2791556Srgrimes
2801556Srgrimes/*
2811556Srgrimes * The following routines are somewhat easier to use that the above.
2821556Srgrimes * The user declares a variable of type STACKSTR, which may be declared
2831556Srgrimes * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2841556Srgrimes * the user uses the macro STPUTC to add characters to the string.  In
2851556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2861556Srgrimes * grown as necessary.  When the user is done, she can just leave the
2871556Srgrimes * string there and refer to it using stackblock().  Or she can allocate
2881556Srgrimes * the space for it using grabstackstr().  If it is necessary to allow
2891556Srgrimes * someone else to use the stack temporarily and then continue to grow
2901556Srgrimes * the string, the user should use grabstack to allocate the space, and
2911556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation.
2921556Srgrimes *
2931556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow.
2941556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
2951556Srgrimes * is space for at least one character.
2961556Srgrimes */
2971556Srgrimes
2981556Srgrimes
2991556Srgrimeschar *
30090111Simpgrowstackstr(void)
30145618Scracauer{
30245618Scracauer	int len;
30345618Scracauer
30445618Scracauer	len = stackblocksize();
3051556Srgrimes	if (herefd >= 0 && len >= 1024) {
30639137Stegge		xwrite(herefd, stackblock(), len);
3071556Srgrimes		sstrnleft = len - 1;
3081556Srgrimes		return stackblock();
3091556Srgrimes	}
3101556Srgrimes	growstackblock();
3111556Srgrimes	sstrnleft = stackblocksize() - len - 1;
3121556Srgrimes	return stackblock() + len;
3131556Srgrimes}
3141556Srgrimes
3151556Srgrimes
3161556Srgrimes/*
3171556Srgrimes * Called from CHECKSTRSPACE.
3181556Srgrimes */
3191556Srgrimes
3201556Srgrimeschar *
32190111Simpmakestrspace(void)
32245618Scracauer{
32345618Scracauer	int len;
32445618Scracauer
32545618Scracauer	len = stackblocksize() - sstrnleft;
3261556Srgrimes	growstackblock();
3271556Srgrimes	sstrnleft = stackblocksize() - len;
3281556Srgrimes	return stackblock() + len;
3291556Srgrimes}
3301556Srgrimes
3311556Srgrimes
3321556Srgrimes
3331556Srgrimesvoid
33490111Simpungrabstackstr(char *s, char *p)
33545618Scracauer{
3361556Srgrimes	stacknleft += stacknxt - s;
3371556Srgrimes	stacknxt = s;
3381556Srgrimes	sstrnleft = stacknleft - (p - s);
3391556Srgrimes}
340