memalloc.c revision 250527
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 250527 2013-05-11 20:51:00Z jilles $");
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
56193221Srseckmalloc(size_t 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 *
98200956Sjillessavestr(const 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
126213760Sobrienstatic struct stack_block *stackp;
127111422Smarcelchar *stacknxt;
128111422Smarcelint stacknleft;
129216743Sjilleschar *sstrend;
1301556Srgrimes
1311556Srgrimes
132213811Sobrienstatic void
133111422Smarcelstnewblock(int nbytes)
134111422Smarcel{
135111422Smarcel	struct stack_block *sp;
136111422Smarcel	int allocsize;
1371556Srgrimes
138111422Smarcel	if (nbytes < MINSIZE)
139111422Smarcel		nbytes = MINSIZE;
140111422Smarcel
141111422Smarcel	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
142111422Smarcel
143111422Smarcel	INTOFF;
144111422Smarcel	sp = ckmalloc(allocsize);
145111422Smarcel	sp->prev = stackp;
146111422Smarcel	stacknxt = SPACE(sp);
147111422Smarcel	stacknleft = allocsize - (stacknxt - (char*)sp);
148216743Sjilles	sstrend = stacknxt + stacknleft;
149111422Smarcel	stackp = sp;
150111422Smarcel	INTON;
151111422Smarcel}
152111422Smarcel
153111422Smarcel
1541556Srgrimespointer
15590111Simpstalloc(int nbytes)
15617987Speter{
15725222Ssteve	char *p;
1581556Srgrimes
1591556Srgrimes	nbytes = ALIGN(nbytes);
160111422Smarcel	if (nbytes > stacknleft)
161111422Smarcel		stnewblock(nbytes);
1621556Srgrimes	p = stacknxt;
1631556Srgrimes	stacknxt += nbytes;
1641556Srgrimes	stacknleft -= nbytes;
1651556Srgrimes	return p;
1661556Srgrimes}
1671556Srgrimes
1681556Srgrimes
1691556Srgrimesvoid
17090111Simpstunalloc(pointer p)
17145618Scracauer{
1721556Srgrimes	if (p == NULL) {		/*DEBUG */
17380381Ssheldonh		write(STDERR_FILENO, "stunalloc\n", 10);
1741556Srgrimes		abort();
1751556Srgrimes	}
1761556Srgrimes	stacknleft += stacknxt - (char *)p;
1771556Srgrimes	stacknxt = p;
1781556Srgrimes}
1791556Srgrimes
1801556Srgrimes
1811556Srgrimes
1821556Srgrimesvoid
18390111Simpsetstackmark(struct stackmark *mark)
18445618Scracauer{
1851556Srgrimes	mark->stackp = stackp;
1861556Srgrimes	mark->stacknxt = stacknxt;
1871556Srgrimes	mark->stacknleft = stacknleft;
188250527Sjilles	/* Ensure this block stays in place. */
189250527Sjilles	if (stackp != NULL && stacknxt == SPACE(stackp))
190250527Sjilles		stalloc(1);
1911556Srgrimes}
1921556Srgrimes
1931556Srgrimes
1941556Srgrimesvoid
19590111Simppopstackmark(struct stackmark *mark)
19645618Scracauer{
1971556Srgrimes	struct stack_block *sp;
1981556Srgrimes
1991556Srgrimes	INTOFF;
2001556Srgrimes	while (stackp != mark->stackp) {
2011556Srgrimes		sp = stackp;
2021556Srgrimes		stackp = sp->prev;
2031556Srgrimes		ckfree(sp);
2041556Srgrimes	}
2051556Srgrimes	stacknxt = mark->stacknxt;
2061556Srgrimes	stacknleft = mark->stacknleft;
207216743Sjilles	sstrend = stacknxt + 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
222216706Sjillesstatic void
223216706Sjillesgrowstackblock(int min)
22445618Scracauer{
2251556Srgrimes	char *p;
22645618Scracauer	int newlen;
22745618Scracauer	char *oldspace;
22845618Scracauer	int oldlen;
2291556Srgrimes	struct stack_block *sp;
23064702Scracauer	struct stack_block *oldstackp;
2311556Srgrimes
232216706Sjilles	if (min < stacknleft)
233216706Sjilles		min = stacknleft;
234248980Sjilles	if ((unsigned int)min >=
235248980Sjilles	    INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
236216706Sjilles		error("Out of space");
237216706Sjilles	min += stacknleft;
238216706Sjilles	min += ALIGN(sizeof(struct stack_block));
239216706Sjilles	newlen = 512;
240216706Sjilles	while (newlen < min)
241216706Sjilles		newlen <<= 1;
24245618Scracauer	oldspace = stacknxt;
24345618Scracauer	oldlen = stacknleft;
24445618Scracauer
245111422Smarcel	if (stackp != NULL && stacknxt == SPACE(stackp)) {
2461556Srgrimes		INTOFF;
24764702Scracauer		oldstackp = stackp;
248111422Smarcel		stackp = oldstackp->prev;
249111422Smarcel		sp = ckrealloc((pointer)oldstackp, newlen);
2501556Srgrimes		sp->prev = stackp;
2511556Srgrimes		stackp = sp;
252111422Smarcel		stacknxt = SPACE(sp);
253111422Smarcel		stacknleft = newlen - (stacknxt - (char*)sp);
254216743Sjilles		sstrend = stacknxt + stacknleft;
2551556Srgrimes		INTON;
2561556Srgrimes	} else {
257216706Sjilles		newlen -= ALIGN(sizeof(struct stack_block));
2581556Srgrimes		p = stalloc(newlen);
259111422Smarcel		if (oldlen != 0)
260111422Smarcel			memcpy(p, oldspace, oldlen);
261111422Smarcel		stunalloc(p);
2621556Srgrimes	}
2631556Srgrimes}
2641556Srgrimes
2651556Srgrimes
2661556Srgrimes
2671556Srgrimes/*
2681556Srgrimes * The following routines are somewhat easier to use that the above.
2691556Srgrimes * The user declares a variable of type STACKSTR, which may be declared
2701556Srgrimes * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2711556Srgrimes * the user uses the macro STPUTC to add characters to the string.  In
2721556Srgrimes * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2731556Srgrimes * grown as necessary.  When the user is done, she can just leave the
2741556Srgrimes * string there and refer to it using stackblock().  Or she can allocate
2751556Srgrimes * the space for it using grabstackstr().  If it is necessary to allow
2761556Srgrimes * someone else to use the stack temporarily and then continue to grow
2771556Srgrimes * the string, the user should use grabstack to allocate the space, and
2781556Srgrimes * then call ungrabstr(p) to return to the previous mode of operation.
2791556Srgrimes *
2801556Srgrimes * USTPUTC is like STPUTC except that it doesn't check for overflow.
2811556Srgrimes * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
2821556Srgrimes * is space for at least one character.
2831556Srgrimes */
2841556Srgrimes
285213814Sobrienstatic char *
286216706Sjillesgrowstrstackblock(int n, int min)
287213814Sobrien{
288216706Sjilles	growstackblock(min);
289213814Sobrien	return stackblock() + n;
290213814Sobrien}
2911556Srgrimes
2921556Srgrimeschar *
29390111Simpgrowstackstr(void)
29445618Scracauer{
29545618Scracauer	int len;
29645618Scracauer
29745618Scracauer	len = stackblocksize();
298216706Sjilles	return (growstrstackblock(len, 0));
2991556Srgrimes}
3001556Srgrimes
3011556Srgrimes
3021556Srgrimes/*
3031556Srgrimes * Called from CHECKSTRSPACE.
3041556Srgrimes */
3051556Srgrimes
3061556Srgrimeschar *
307216743Sjillesmakestrspace(int min, char *p)
30845618Scracauer{
30945618Scracauer	int len;
31045618Scracauer
311216743Sjilles	len = p - stackblock();
312216706Sjilles	return (growstrstackblock(len, min));
3131556Srgrimes}
3141556Srgrimes
3151556Srgrimes
316215783Sjilleschar *
317248980Sjillesstputbin(const char *data, size_t len, char *p)
318215783Sjilles{
319216706Sjilles	CHECKSTRSPACE(len, p);
320216706Sjilles	memcpy(p, data, len);
321216706Sjilles	return (p + len);
322215783Sjilles}
323215783Sjilles
324215783Sjilleschar *
325215783Sjillesstputs(const char *data, char *p)
326215783Sjilles{
327215783Sjilles	return (stputbin(data, strlen(data), p));
328215783Sjilles}
329