xref: /freebsd/bin/sh/memalloc.c (revision b6ae2ec7b03250cc22cd9bf64044d113ec64d9f1)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Kenneth Almquist.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
35caecb2f4SMarcel Moolenaar #include <sys/param.h>
364b88c807SRodney W. Grimes #include "shell.h"
374b88c807SRodney W. Grimes #include "output.h"
384b88c807SRodney W. Grimes #include "memalloc.h"
394b88c807SRodney W. Grimes #include "error.h"
404b88c807SRodney W. Grimes #include "mystring.h"
41e6ac45ddSMartin Cracauer #include "expand.h"
42aa9caaf6SPeter Wemm #include <stdlib.h>
43aa9caaf6SPeter Wemm #include <unistd.h>
444b88c807SRodney W. Grimes 
459f9c9549SJilles Tjoelker static void
badalloc(const char * message)469f9c9549SJilles Tjoelker badalloc(const char *message)
479f9c9549SJilles Tjoelker {
489f9c9549SJilles Tjoelker 	write(2, message, strlen(message));
499f9c9549SJilles Tjoelker 	abort();
509f9c9549SJilles Tjoelker }
519f9c9549SJilles Tjoelker 
524b88c807SRodney W. Grimes /*
534b88c807SRodney W. Grimes  * Like malloc, but returns an error when out of space.
544b88c807SRodney W. Grimes  */
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes pointer
ckmalloc(size_t nbytes)57ac08b882SRalf S. Engelschall ckmalloc(size_t nbytes)
58aa9caaf6SPeter Wemm {
59afb033d5SSteve Price 	pointer p;
604b88c807SRodney W. Grimes 
619f9c9549SJilles Tjoelker 	if (!is_int_on())
629f9c9549SJilles Tjoelker 		badalloc("Unsafe ckmalloc() call\n");
63670528cdSStefan Farfeleder 	p = malloc(nbytes);
64670528cdSStefan Farfeleder 	if (p == NULL)
654b88c807SRodney W. Grimes 		error("Out of space");
664b88c807SRodney W. Grimes 	return p;
674b88c807SRodney W. Grimes }
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes /*
714b88c807SRodney W. Grimes  * Same for realloc.
724b88c807SRodney W. Grimes  */
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes pointer
ckrealloc(pointer p,int nbytes)755134c3f7SWarner Losh ckrealloc(pointer p, int nbytes)
764b88c807SRodney W. Grimes {
779f9c9549SJilles Tjoelker 	if (!is_int_on())
789f9c9549SJilles Tjoelker 		badalloc("Unsafe ckrealloc() call\n");
79670528cdSStefan Farfeleder 	p = realloc(p, nbytes);
80670528cdSStefan Farfeleder 	if (p == NULL)
814b88c807SRodney W. Grimes 		error("Out of space");
824b88c807SRodney W. Grimes 	return p;
834b88c807SRodney W. Grimes }
844b88c807SRodney W. Grimes 
85670528cdSStefan Farfeleder void
ckfree(pointer p)86670528cdSStefan Farfeleder ckfree(pointer p)
87670528cdSStefan Farfeleder {
889f9c9549SJilles Tjoelker 	if (!is_int_on())
899f9c9549SJilles Tjoelker 		badalloc("Unsafe ckfree() call\n");
90670528cdSStefan Farfeleder 	free(p);
91670528cdSStefan Farfeleder }
92670528cdSStefan Farfeleder 
934b88c807SRodney W. Grimes 
944b88c807SRodney W. Grimes /*
954b88c807SRodney W. Grimes  * Make a copy of a string in safe storage.
964b88c807SRodney W. Grimes  */
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes char *
savestr(const char * s)992cac6e36SJilles Tjoelker savestr(const char *s)
1004b88c807SRodney W. Grimes {
101afb033d5SSteve Price 	char *p;
102670dd3f0SJilles Tjoelker 	size_t len;
1034b88c807SRodney W. Grimes 
104670dd3f0SJilles Tjoelker 	len = strlen(s);
105670dd3f0SJilles Tjoelker 	p = ckmalloc(len + 1);
106670dd3f0SJilles Tjoelker 	memcpy(p, s, len + 1);
1074b88c807SRodney W. Grimes 	return p;
1084b88c807SRodney W. Grimes }
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes 
1114b88c807SRodney W. Grimes /*
1124b88c807SRodney W. Grimes  * Parse trees for commands are allocated in lifo order, so we use a stack
1134b88c807SRodney W. Grimes  * to make this more efficient, and also to avoid all sorts of exception
1144b88c807SRodney W. Grimes  * handling code to handle interrupts in the middle of a parse.
1154b88c807SRodney W. Grimes  *
116caecb2f4SMarcel Moolenaar  * The size 496 was chosen because with 16-byte alignment the total size
117caecb2f4SMarcel Moolenaar  * for the allocated block is 512.
1184b88c807SRodney W. Grimes  */
1194b88c807SRodney W. Grimes 
120caecb2f4SMarcel Moolenaar #define MINSIZE 496		/* minimum size of a block. */
1214b88c807SRodney W. Grimes 
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes struct stack_block {
1244b88c807SRodney W. Grimes 	struct stack_block *prev;
125caecb2f4SMarcel Moolenaar 	/* Data follows */
1264b88c807SRodney W. Grimes };
127caecb2f4SMarcel Moolenaar #define SPACE(sp)	((char*)(sp) + ALIGN(sizeof(struct stack_block)))
1284b88c807SRodney W. Grimes 
129aa7b6f82SDavid E. O'Brien static struct stack_block *stackp;
130caecb2f4SMarcel Moolenaar char *stacknxt;
131caecb2f4SMarcel Moolenaar int stacknleft;
132ff802dc7SJilles Tjoelker char *sstrend;
1334b88c807SRodney W. Grimes 
1344b88c807SRodney W. Grimes 
13588328642SDavid E. O'Brien static void
stnewblock(int nbytes)136caecb2f4SMarcel Moolenaar stnewblock(int nbytes)
137caecb2f4SMarcel Moolenaar {
138caecb2f4SMarcel Moolenaar 	struct stack_block *sp;
139caecb2f4SMarcel Moolenaar 	int allocsize;
140caecb2f4SMarcel Moolenaar 
141caecb2f4SMarcel Moolenaar 	if (nbytes < MINSIZE)
142caecb2f4SMarcel Moolenaar 		nbytes = MINSIZE;
143caecb2f4SMarcel Moolenaar 
144caecb2f4SMarcel Moolenaar 	allocsize = ALIGN(sizeof(struct stack_block)) + ALIGN(nbytes);
145caecb2f4SMarcel Moolenaar 
146caecb2f4SMarcel Moolenaar 	INTOFF;
147caecb2f4SMarcel Moolenaar 	sp = ckmalloc(allocsize);
148caecb2f4SMarcel Moolenaar 	sp->prev = stackp;
149caecb2f4SMarcel Moolenaar 	stacknxt = SPACE(sp);
150caecb2f4SMarcel Moolenaar 	stacknleft = allocsize - (stacknxt - (char*)sp);
151ff802dc7SJilles Tjoelker 	sstrend = stacknxt + stacknleft;
152caecb2f4SMarcel Moolenaar 	stackp = sp;
153caecb2f4SMarcel Moolenaar 	INTON;
154caecb2f4SMarcel Moolenaar }
155caecb2f4SMarcel Moolenaar 
1564b88c807SRodney W. Grimes 
1574b88c807SRodney W. Grimes pointer
stalloc(int nbytes)1585134c3f7SWarner Losh stalloc(int nbytes)
159aa9caaf6SPeter Wemm {
160afb033d5SSteve Price 	char *p;
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes 	nbytes = ALIGN(nbytes);
163caecb2f4SMarcel Moolenaar 	if (nbytes > stacknleft)
164caecb2f4SMarcel Moolenaar 		stnewblock(nbytes);
1654b88c807SRodney W. Grimes 	p = stacknxt;
1664b88c807SRodney W. Grimes 	stacknxt += nbytes;
1674b88c807SRodney W. Grimes 	stacknleft -= nbytes;
1684b88c807SRodney W. Grimes 	return p;
1694b88c807SRodney W. Grimes }
1704b88c807SRodney W. Grimes 
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes void
stunalloc(pointer p)1735134c3f7SWarner Losh stunalloc(pointer p)
1744b88c807SRodney W. Grimes {
1754b88c807SRodney W. Grimes 	if (p == NULL) {		/*DEBUG */
176e1b4d8d0SSheldon Hearn 		write(STDERR_FILENO, "stunalloc\n", 10);
1774b88c807SRodney W. Grimes 		abort();
1784b88c807SRodney W. Grimes 	}
1794b88c807SRodney W. Grimes 	stacknleft += stacknxt - (char *)p;
1804b88c807SRodney W. Grimes 	stacknxt = p;
1814b88c807SRodney W. Grimes }
1824b88c807SRodney W. Grimes 
1834b88c807SRodney W. Grimes 
184a4652c28SJilles Tjoelker char *
stsavestr(const char * s)185a4652c28SJilles Tjoelker stsavestr(const char *s)
186a4652c28SJilles Tjoelker {
187a4652c28SJilles Tjoelker 	char *p;
188a4652c28SJilles Tjoelker 	size_t len;
189a4652c28SJilles Tjoelker 
190a4652c28SJilles Tjoelker 	len = strlen(s);
191a4652c28SJilles Tjoelker 	p = stalloc(len + 1);
192a4652c28SJilles Tjoelker 	memcpy(p, s, len + 1);
193a4652c28SJilles Tjoelker 	return p;
194a4652c28SJilles Tjoelker }
195a4652c28SJilles Tjoelker 
1964b88c807SRodney W. Grimes 
1974b88c807SRodney W. Grimes void
setstackmark(struct stackmark * mark)1985134c3f7SWarner Losh setstackmark(struct stackmark *mark)
1994b88c807SRodney W. Grimes {
2004b88c807SRodney W. Grimes 	mark->stackp = stackp;
2014b88c807SRodney W. Grimes 	mark->stacknxt = stacknxt;
2024b88c807SRodney W. Grimes 	mark->stacknleft = stacknleft;
203336e0c87SJilles Tjoelker 	/* Ensure this block stays in place. */
204336e0c87SJilles Tjoelker 	if (stackp != NULL && stacknxt == SPACE(stackp))
205336e0c87SJilles Tjoelker 		stalloc(1);
2064b88c807SRodney W. Grimes }
2074b88c807SRodney W. Grimes 
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes void
popstackmark(struct stackmark * mark)2105134c3f7SWarner Losh popstackmark(struct stackmark *mark)
2114b88c807SRodney W. Grimes {
2124b88c807SRodney W. Grimes 	struct stack_block *sp;
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes 	INTOFF;
2154b88c807SRodney W. Grimes 	while (stackp != mark->stackp) {
2164b88c807SRodney W. Grimes 		sp = stackp;
2174b88c807SRodney W. Grimes 		stackp = sp->prev;
2184b88c807SRodney W. Grimes 		ckfree(sp);
2194b88c807SRodney W. Grimes 	}
2204b88c807SRodney W. Grimes 	stacknxt = mark->stacknxt;
2214b88c807SRodney W. Grimes 	stacknleft = mark->stacknleft;
2225a18515bSPiotr Pawel Stefaniak 	if (stacknleft != 0)
223ff802dc7SJilles Tjoelker 		sstrend = stacknxt + stacknleft;
2245a18515bSPiotr Pawel Stefaniak 	else
2255a18515bSPiotr Pawel Stefaniak 		sstrend = stacknxt;
2264b88c807SRodney W. Grimes 	INTON;
2274b88c807SRodney W. Grimes }
2284b88c807SRodney W. Grimes 
2294b88c807SRodney W. Grimes 
2304b88c807SRodney W. Grimes /*
2314b88c807SRodney W. Grimes  * When the parser reads in a string, it wants to stick the string on the
2324b88c807SRodney W. Grimes  * stack and only adjust the stack pointer when it knows how big the
2334b88c807SRodney W. Grimes  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
2344b88c807SRodney W. Grimes  * of space on top of the stack and stackblocklen returns the length of
2354b88c807SRodney W. Grimes  * this block.  Growstackblock will grow this space by at least one byte,
2364b88c807SRodney W. Grimes  * possibly moving it (like realloc).  Grabstackblock actually allocates the
2374b88c807SRodney W. Grimes  * part of the block that has been used.
2384b88c807SRodney W. Grimes  */
2394b88c807SRodney W. Grimes 
240d8f32e72SJilles Tjoelker static void
growstackblock(int min)241d8f32e72SJilles Tjoelker growstackblock(int min)
2429d5efc15SMartin Cracauer {
2434b88c807SRodney W. Grimes 	char *p;
2449d5efc15SMartin Cracauer 	int newlen;
2459d5efc15SMartin Cracauer 	char *oldspace;
2469d5efc15SMartin Cracauer 	int oldlen;
2474b88c807SRodney W. Grimes 	struct stack_block *sp;
24884c3800cSMartin Cracauer 	struct stack_block *oldstackp;
2494b88c807SRodney W. Grimes 
250d8f32e72SJilles Tjoelker 	if (min < stacknleft)
251d8f32e72SJilles Tjoelker 		min = stacknleft;
25246c6b52dSJilles Tjoelker 	if ((unsigned int)min >=
25346c6b52dSJilles Tjoelker 	    INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
254d8f32e72SJilles Tjoelker 		error("Out of space");
255d8f32e72SJilles Tjoelker 	min += stacknleft;
256d8f32e72SJilles Tjoelker 	min += ALIGN(sizeof(struct stack_block));
257d8f32e72SJilles Tjoelker 	newlen = 512;
258d8f32e72SJilles Tjoelker 	while (newlen < min)
259d8f32e72SJilles Tjoelker 		newlen <<= 1;
2609d5efc15SMartin Cracauer 	oldspace = stacknxt;
2619d5efc15SMartin Cracauer 	oldlen = stacknleft;
2629d5efc15SMartin Cracauer 
263caecb2f4SMarcel Moolenaar 	if (stackp != NULL && stacknxt == SPACE(stackp)) {
2644b88c807SRodney W. Grimes 		INTOFF;
26584c3800cSMartin Cracauer 		oldstackp = stackp;
266caecb2f4SMarcel Moolenaar 		stackp = oldstackp->prev;
267caecb2f4SMarcel Moolenaar 		sp = ckrealloc((pointer)oldstackp, newlen);
2684b88c807SRodney W. Grimes 		sp->prev = stackp;
2694b88c807SRodney W. Grimes 		stackp = sp;
270caecb2f4SMarcel Moolenaar 		stacknxt = SPACE(sp);
271caecb2f4SMarcel Moolenaar 		stacknleft = newlen - (stacknxt - (char*)sp);
272ff802dc7SJilles Tjoelker 		sstrend = stacknxt + stacknleft;
2734b88c807SRodney W. Grimes 		INTON;
2744b88c807SRodney W. Grimes 	} else {
275d8f32e72SJilles Tjoelker 		newlen -= ALIGN(sizeof(struct stack_block));
2764b88c807SRodney W. Grimes 		p = stalloc(newlen);
277caecb2f4SMarcel Moolenaar 		if (oldlen != 0)
278aa9caaf6SPeter Wemm 			memcpy(p, oldspace, oldlen);
279caecb2f4SMarcel Moolenaar 		stunalloc(p);
2804b88c807SRodney W. Grimes 	}
2814b88c807SRodney W. Grimes }
2824b88c807SRodney W. Grimes 
2834b88c807SRodney W. Grimes 
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes /*
286*b6ae2ec7SKai-Yang Chen  * The following routines are somewhat easier to use than the above.
2874b88c807SRodney W. Grimes  * The user declares a variable of type STACKSTR, which may be declared
2884b88c807SRodney W. Grimes  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
2894b88c807SRodney W. Grimes  * the user uses the macro STPUTC to add characters to the string.  In
2904b88c807SRodney W. Grimes  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
2914b88c807SRodney W. Grimes  * grown as necessary.  When the user is done, she can just leave the
2924b88c807SRodney W. Grimes  * string there and refer to it using stackblock().  Or she can allocate
2934b88c807SRodney W. Grimes  * the space for it using grabstackstr().  If it is necessary to allow
2944b88c807SRodney W. Grimes  * someone else to use the stack temporarily and then continue to grow
2954b88c807SRodney W. Grimes  * the string, the user should use grabstack to allocate the space, and
2964b88c807SRodney W. Grimes  * then call ungrabstr(p) to return to the previous mode of operation.
2974b88c807SRodney W. Grimes  *
2984b88c807SRodney W. Grimes  * USTPUTC is like STPUTC except that it doesn't check for overflow.
2994b88c807SRodney W. Grimes  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
3004b88c807SRodney W. Grimes  * is space for at least one character.
3014b88c807SRodney W. Grimes  */
3024b88c807SRodney W. Grimes 
3037cfe6941SDavid E. O'Brien static char *
growstrstackblock(int n,int min)304d8f32e72SJilles Tjoelker growstrstackblock(int n, int min)
3057cfe6941SDavid E. O'Brien {
306d8f32e72SJilles Tjoelker 	growstackblock(min);
3077cfe6941SDavid E. O'Brien 	return stackblock() + n;
3087cfe6941SDavid E. O'Brien }
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes char *
growstackstr(void)3115134c3f7SWarner Losh growstackstr(void)
3129d5efc15SMartin Cracauer {
3139d5efc15SMartin Cracauer 	int len;
3149d5efc15SMartin Cracauer 
3159d5efc15SMartin Cracauer 	len = stackblocksize();
316d8f32e72SJilles Tjoelker 	return (growstrstackblock(len, 0));
3174b88c807SRodney W. Grimes }
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes /*
3214b88c807SRodney W. Grimes  * Called from CHECKSTRSPACE.
3224b88c807SRodney W. Grimes  */
3234b88c807SRodney W. Grimes 
3244b88c807SRodney W. Grimes char *
makestrspace(int min,char * p)325ff802dc7SJilles Tjoelker makestrspace(int min, char *p)
3269d5efc15SMartin Cracauer {
3279d5efc15SMartin Cracauer 	int len;
3289d5efc15SMartin Cracauer 
329ff802dc7SJilles Tjoelker 	len = p - stackblock();
330d8f32e72SJilles Tjoelker 	return (growstrstackblock(len, min));
3314b88c807SRodney W. Grimes }
3324b88c807SRodney W. Grimes 
3334b88c807SRodney W. Grimes 
3349d37e157SJilles Tjoelker char *
stputbin(const char * data,size_t len,char * p)33546c6b52dSJilles Tjoelker stputbin(const char *data, size_t len, char *p)
3369d37e157SJilles Tjoelker {
337d8f32e72SJilles Tjoelker 	CHECKSTRSPACE(len, p);
338d8f32e72SJilles Tjoelker 	memcpy(p, data, len);
339d8f32e72SJilles Tjoelker 	return (p + len);
3409d37e157SJilles Tjoelker }
3419d37e157SJilles Tjoelker 
3429d37e157SJilles Tjoelker char *
stputs(const char * data,char * p)3439d37e157SJilles Tjoelker stputs(const char *data, char *p)
3449d37e157SJilles Tjoelker {
3459d37e157SJilles Tjoelker 	return (stputbin(data, strlen(data), p));
3469d37e157SJilles Tjoelker }
347