Home | History | Annotate | Line # | Download | only in sh
memalloc.c revision 1.33.2.1
      1  1.33.2.1    martin /*	$NetBSD: memalloc.c,v 1.33.2.1 2021/11/06 13:35:43 martin Exp $	*/
      2      1.15       cgd 
      3       1.1       cgd /*-
      4       1.5       jtc  * Copyright (c) 1991, 1993
      5       1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Kenneth Almquist.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.28       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35      1.20  christos #include <sys/cdefs.h>
     36       1.1       cgd #ifndef lint
     37      1.15       cgd #if 0
     38      1.16  christos static char sccsid[] = "@(#)memalloc.c	8.3 (Berkeley) 5/4/95";
     39      1.15       cgd #else
     40  1.33.2.1    martin __RCSID("$NetBSD: memalloc.c,v 1.33.2.1 2021/11/06 13:35:43 martin Exp $");
     41      1.15       cgd #endif
     42       1.1       cgd #endif /* not lint */
     43       1.1       cgd 
     44  1.33.2.1    martin #include <limits.h>
     45  1.33.2.1    martin #include <stdarg.h>
     46      1.21  christos #include <stdlib.h>
     47      1.21  christos #include <unistd.h>
     48      1.21  christos 
     49       1.1       cgd #include "shell.h"
     50       1.1       cgd #include "output.h"
     51       1.1       cgd #include "memalloc.h"
     52       1.1       cgd #include "error.h"
     53       1.1       cgd #include "machdep.h"
     54       1.1       cgd #include "mystring.h"
     55       1.1       cgd 
     56       1.1       cgd /*
     57       1.1       cgd  * Like malloc, but returns an error when out of space.
     58       1.1       cgd  */
     59       1.1       cgd 
     60       1.1       cgd pointer
     61      1.29      matt ckmalloc(size_t nbytes)
     62      1.10       cgd {
     63      1.19       tls 	pointer p;
     64       1.1       cgd 
     65      1.22   mycroft 	p = malloc(nbytes);
     66      1.22   mycroft 	if (p == NULL)
     67       1.1       cgd 		error("Out of space");
     68       1.1       cgd 	return p;
     69       1.1       cgd }
     70       1.1       cgd 
     71       1.1       cgd 
     72       1.1       cgd /*
     73       1.1       cgd  * Same for realloc.
     74       1.1       cgd  */
     75       1.1       cgd 
     76       1.1       cgd pointer
     77      1.26  christos ckrealloc(pointer p, int nbytes)
     78      1.10       cgd {
     79      1.25  christos 	p = realloc(p, nbytes);
     80      1.25  christos 	if (p == NULL)
     81       1.1       cgd 		error("Out of space");
     82       1.1       cgd 	return p;
     83       1.1       cgd }
     84       1.1       cgd 
     85       1.1       cgd 
     86       1.1       cgd /*
     87       1.1       cgd  * Make a copy of a string in safe storage.
     88       1.1       cgd  */
     89       1.1       cgd 
     90       1.1       cgd char *
     91      1.27       dsl savestr(const char *s)
     92      1.25  christos {
     93      1.19       tls 	char *p;
     94       1.1       cgd 
     95       1.1       cgd 	p = ckmalloc(strlen(s) + 1);
     96       1.1       cgd 	scopy(s, p);
     97       1.1       cgd 	return p;
     98       1.1       cgd }
     99       1.1       cgd 
    100       1.1       cgd 
    101       1.1       cgd /*
    102       1.1       cgd  * Parse trees for commands are allocated in lifo order, so we use a stack
    103       1.1       cgd  * to make this more efficient, and also to avoid all sorts of exception
    104       1.1       cgd  * handling code to handle interrupts in the middle of a parse.
    105      1.12   mycroft  *
    106      1.12   mycroft  * The size 504 was chosen because the Ultrix malloc handles that size
    107      1.12   mycroft  * well.
    108       1.1       cgd  */
    109       1.1       cgd 
    110      1.12   mycroft #define MINSIZE 504		/* minimum size of a block */
    111       1.1       cgd 
    112       1.1       cgd struct stack_block {
    113       1.1       cgd 	struct stack_block *prev;
    114       1.1       cgd 	char space[MINSIZE];
    115       1.1       cgd };
    116       1.1       cgd 
    117       1.1       cgd struct stack_block stackbase;
    118       1.1       cgd struct stack_block *stackp = &stackbase;
    119      1.23  christos struct stackmark *markp;
    120       1.1       cgd char *stacknxt = stackbase.space;
    121       1.1       cgd int stacknleft = MINSIZE;
    122       1.1       cgd int sstrnleft;
    123       1.1       cgd int herefd = -1;
    124       1.1       cgd 
    125       1.1       cgd pointer
    126      1.26  christos stalloc(int nbytes)
    127      1.10       cgd {
    128      1.19       tls 	char *p;
    129       1.1       cgd 
    130      1.24  christos 	nbytes = SHELL_ALIGN(nbytes);
    131      1.12   mycroft 	if (nbytes > stacknleft) {
    132       1.1       cgd 		int blocksize;
    133       1.1       cgd 		struct stack_block *sp;
    134       1.1       cgd 
    135      1.12   mycroft 		blocksize = nbytes;
    136       1.1       cgd 		if (blocksize < MINSIZE)
    137       1.1       cgd 			blocksize = MINSIZE;
    138       1.1       cgd 		INTOFF;
    139       1.1       cgd 		sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
    140       1.1       cgd 		sp->prev = stackp;
    141       1.1       cgd 		stacknxt = sp->space;
    142       1.1       cgd 		stacknleft = blocksize;
    143       1.1       cgd 		stackp = sp;
    144       1.1       cgd 		INTON;
    145       1.1       cgd 	}
    146      1.33       kre 	INTOFF;
    147       1.1       cgd 	p = stacknxt;
    148      1.12   mycroft 	stacknxt += nbytes;
    149      1.12   mycroft 	stacknleft -= nbytes;
    150      1.33       kre 	INTON;
    151       1.1       cgd 	return p;
    152       1.1       cgd }
    153       1.1       cgd 
    154       1.1       cgd 
    155       1.1       cgd void
    156      1.26  christos stunalloc(pointer p)
    157      1.25  christos {
    158       1.1       cgd 	if (p == NULL) {		/*DEBUG */
    159       1.1       cgd 		write(2, "stunalloc\n", 10);
    160       1.1       cgd 		abort();
    161       1.1       cgd 	}
    162       1.1       cgd 	stacknleft += stacknxt - (char *)p;
    163       1.1       cgd 	stacknxt = p;
    164       1.1       cgd }
    165       1.1       cgd 
    166       1.1       cgd 
    167      1.32       kre /* save the current status of the sh stack */
    168       1.1       cgd void
    169      1.26  christos setstackmark(struct stackmark *mark)
    170      1.25  christos {
    171       1.1       cgd 	mark->stackp = stackp;
    172       1.1       cgd 	mark->stacknxt = stacknxt;
    173       1.1       cgd 	mark->stacknleft = stacknleft;
    174      1.30       kre 	mark->sstrnleft = sstrnleft;
    175      1.23  christos 	mark->marknext = markp;
    176      1.23  christos 	markp = mark;
    177       1.1       cgd }
    178       1.1       cgd 
    179      1.32       kre /* reset the stack mark, and remove it from the list of marks */
    180      1.32       kre void
    181      1.32       kre popstackmark(struct stackmark *mark)
    182      1.32       kre {
    183      1.33       kre 	INTOFF;
    184      1.32       kre 	markp = mark->marknext;		/* delete mark from the list */
    185      1.32       kre 	rststackmark(mark);		/* and reset stack */
    186      1.33       kre 	INTON;
    187      1.32       kre }
    188       1.1       cgd 
    189      1.32       kre /* reset the shell stack to its state recorded in the stack mark */
    190       1.1       cgd void
    191      1.32       kre rststackmark(struct stackmark *mark)
    192      1.25  christos {
    193       1.1       cgd 	struct stack_block *sp;
    194       1.1       cgd 
    195       1.1       cgd 	INTOFF;
    196       1.1       cgd 	while (stackp != mark->stackp) {
    197      1.32       kre 		/* delete any recently allocated mem blocks */
    198       1.1       cgd 		sp = stackp;
    199       1.1       cgd 		stackp = sp->prev;
    200       1.1       cgd 		ckfree(sp);
    201       1.1       cgd 	}
    202       1.1       cgd 	stacknxt = mark->stacknxt;
    203       1.1       cgd 	stacknleft = mark->stacknleft;
    204      1.30       kre 	sstrnleft = mark->sstrnleft;
    205       1.1       cgd 	INTON;
    206       1.1       cgd }
    207       1.1       cgd 
    208       1.1       cgd 
    209       1.1       cgd /*
    210       1.1       cgd  * When the parser reads in a string, it wants to stick the string on the
    211       1.1       cgd  * stack and only adjust the stack pointer when it knows how big the
    212       1.1       cgd  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
    213       1.1       cgd  * of space on top of the stack and stackblocklen returns the length of
    214       1.1       cgd  * this block.  Growstackblock will grow this space by at least one byte,
    215       1.1       cgd  * possibly moving it (like realloc).  Grabstackblock actually allocates the
    216       1.1       cgd  * part of the block that has been used.
    217       1.1       cgd  */
    218       1.1       cgd 
    219       1.1       cgd void
    220      1.26  christos growstackblock(void)
    221      1.25  christos {
    222      1.24  christos 	int newlen = SHELL_ALIGN(stacknleft * 2 + 100);
    223       1.1       cgd 
    224      1.31       kre 	INTOFF;
    225       1.1       cgd 	if (stacknxt == stackp->space && stackp != &stackbase) {
    226      1.25  christos 		struct stack_block *oldstackp;
    227      1.25  christos 		struct stackmark *xmark;
    228      1.25  christos 		struct stack_block *sp;
    229      1.25  christos 
    230      1.23  christos 		oldstackp = stackp;
    231       1.1       cgd 		sp = stackp;
    232       1.1       cgd 		stackp = sp->prev;
    233      1.25  christos 		sp = ckrealloc((pointer)sp,
    234      1.25  christos 		    sizeof(struct stack_block) - MINSIZE + newlen);
    235       1.1       cgd 		sp->prev = stackp;
    236       1.1       cgd 		stackp = sp;
    237       1.1       cgd 		stacknxt = sp->space;
    238      1.31       kre 		sstrnleft += newlen - stacknleft;
    239       1.1       cgd 		stacknleft = newlen;
    240      1.25  christos 
    241      1.25  christos 		/*
    242      1.25  christos 		 * Stack marks pointing to the start of the old block
    243      1.25  christos 		 * must be relocated to point to the new block
    244      1.25  christos 		 */
    245      1.25  christos 		xmark = markp;
    246      1.25  christos 		while (xmark != NULL && xmark->stackp == oldstackp) {
    247      1.25  christos 			xmark->stackp = stackp;
    248      1.25  christos 			xmark->stacknxt = stacknxt;
    249      1.31       kre 			xmark->sstrnleft += stacknleft - xmark->stacknleft;
    250      1.25  christos 			xmark->stacknleft = stacknleft;
    251      1.25  christos 			xmark = xmark->marknext;
    252      1.23  christos 		}
    253       1.1       cgd 	} else {
    254      1.25  christos 		char *oldspace = stacknxt;
    255      1.25  christos 		int oldlen = stacknleft;
    256      1.25  christos 		char *p = stalloc(newlen);
    257      1.25  christos 
    258      1.25  christos 		(void)memcpy(p, oldspace, oldlen);
    259       1.1       cgd 		stacknxt = p;			/* free the space */
    260      1.17       cgd 		stacknleft += newlen;		/* we just allocated */
    261       1.1       cgd 	}
    262      1.31       kre 	INTON;
    263       1.1       cgd }
    264       1.1       cgd 
    265       1.1       cgd void
    266      1.26  christos grabstackblock(int len)
    267      1.10       cgd {
    268      1.24  christos 	len = SHELL_ALIGN(len);
    269      1.33       kre 	INTOFF;
    270       1.1       cgd 	stacknxt += len;
    271       1.1       cgd 	stacknleft -= len;
    272      1.33       kre 	INTON;
    273       1.1       cgd }
    274       1.1       cgd 
    275       1.1       cgd /*
    276      1.26  christos  * The following routines are somewhat easier to use than the above.
    277       1.1       cgd  * The user declares a variable of type STACKSTR, which may be declared
    278       1.1       cgd  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
    279       1.1       cgd  * the user uses the macro STPUTC to add characters to the string.  In
    280       1.1       cgd  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
    281       1.1       cgd  * grown as necessary.  When the user is done, she can just leave the
    282       1.1       cgd  * string there and refer to it using stackblock().  Or she can allocate
    283       1.1       cgd  * the space for it using grabstackstr().  If it is necessary to allow
    284       1.1       cgd  * someone else to use the stack temporarily and then continue to grow
    285       1.1       cgd  * the string, the user should use grabstack to allocate the space, and
    286       1.1       cgd  * then call ungrabstr(p) to return to the previous mode of operation.
    287       1.1       cgd  *
    288       1.1       cgd  * USTPUTC is like STPUTC except that it doesn't check for overflow.
    289       1.1       cgd  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
    290       1.1       cgd  * is space for at least one character.
    291       1.1       cgd  */
    292       1.1       cgd 
    293       1.1       cgd char *
    294      1.26  christos growstackstr(void)
    295      1.25  christos {
    296       1.1       cgd 	int len = stackblocksize();
    297       1.1       cgd 	if (herefd >= 0 && len >= 1024) {
    298       1.1       cgd 		xwrite(herefd, stackblock(), len);
    299       1.1       cgd 		sstrnleft = len - 1;
    300       1.1       cgd 		return stackblock();
    301       1.1       cgd 	}
    302       1.1       cgd 	growstackblock();
    303       1.1       cgd 	sstrnleft = stackblocksize() - len - 1;
    304       1.1       cgd 	return stackblock() + len;
    305       1.1       cgd }
    306       1.1       cgd 
    307       1.1       cgd /*
    308       1.1       cgd  * Called from CHECKSTRSPACE.
    309       1.1       cgd  */
    310       1.1       cgd 
    311       1.1       cgd char *
    312      1.26  christos makestrspace(void)
    313      1.25  christos {
    314       1.1       cgd 	int len = stackblocksize() - sstrnleft;
    315       1.1       cgd 	growstackblock();
    316       1.1       cgd 	sstrnleft = stackblocksize() - len;
    317       1.1       cgd 	return stackblock() + len;
    318       1.1       cgd }
    319       1.1       cgd 
    320      1.30       kre /*
    321      1.30       kre  * Note that this only works to release stack space for reuse
    322      1.30       kre  * if nothing else has allocated space on the stack since the grabstackstr()
    323      1.30       kre  *
    324      1.30       kre  * "s" is the start of the area to be released, and "p" represents the end
    325      1.30       kre  * of the string we have stored beyond there and are now releasing.
    326      1.30       kre  * (ie: "p" should be the same as in the call to grabstackstr()).
    327      1.30       kre  *
    328      1.30       kre  * stunalloc(s) and ungrabstackstr(s, p) are almost interchangable after
    329      1.30       kre  * a grabstackstr(), however the latter also returns string space so we
    330      1.30       kre  * can just continue with STPUTC() etc without needing a new STARTSTACKSTR(s)
    331      1.30       kre  */
    332       1.1       cgd void
    333      1.26  christos ungrabstackstr(char *s, char *p)
    334      1.25  christos {
    335      1.30       kre #ifdef DEBUG
    336      1.30       kre 	if (s < stacknxt || stacknxt + stacknleft < s)
    337      1.30       kre 		abort();
    338      1.30       kre #endif
    339       1.1       cgd 	stacknleft += stacknxt - s;
    340       1.1       cgd 	stacknxt = s;
    341       1.1       cgd 	sstrnleft = stacknleft - (p - s);
    342       1.1       cgd }
    343  1.33.2.1    martin 
    344  1.33.2.1    martin /*
    345  1.33.2.1    martin  * Save the concat of a sequence of strings in stack space
    346  1.33.2.1    martin  *
    347  1.33.2.1    martin  * The first arg (if not NULL) is a pointer to where the final string
    348  1.33.2.1    martin  * length will be returned.
    349  1.33.2.1    martin  *
    350  1.33.2.1    martin  * Remaining args are pointers to strings - sufficient space to hold
    351  1.33.2.1    martin  * the concat of the strings is allocated on the stack, the strings
    352  1.33.2.1    martin  * are copied into that space, and a pointer to its start is retured.
    353  1.33.2.1    martin  * The arg list is terminated with STSTRC_END.
    354  1.33.2.1    martin  *
    355  1.33.2.1    martin  * Use stunalloc(string) (in proper sequence) to release the string
    356  1.33.2.1    martin  */
    357  1.33.2.1    martin char *
    358  1.33.2.1    martin ststrcat(size_t *lp, ...)
    359  1.33.2.1    martin {
    360  1.33.2.1    martin 	va_list ap;
    361  1.33.2.1    martin 	const char *arg;
    362  1.33.2.1    martin 	size_t len, tlen = 0, alen[8];
    363  1.33.2.1    martin 	char *str, *nxt;
    364  1.33.2.1    martin 	unsigned int n;
    365  1.33.2.1    martin 
    366  1.33.2.1    martin 	n = 0;
    367  1.33.2.1    martin 	va_start(ap, lp);
    368  1.33.2.1    martin 	arg = va_arg(ap, const char *);
    369  1.33.2.1    martin 	while (arg != STSTRC_END) {
    370  1.33.2.1    martin 		len = strlen(arg);
    371  1.33.2.1    martin 		if (n < sizeof(alen)/sizeof(alen[0]))
    372  1.33.2.1    martin 			alen[n++] = len;
    373  1.33.2.1    martin 		tlen += len;
    374  1.33.2.1    martin 		arg = va_arg(ap, const char *);
    375  1.33.2.1    martin 	}
    376  1.33.2.1    martin 	va_end(ap);
    377  1.33.2.1    martin 
    378  1.33.2.1    martin 	if (lp != NULL)
    379  1.33.2.1    martin 		*lp = tlen;
    380  1.33.2.1    martin 
    381  1.33.2.1    martin 	if (tlen >= INT_MAX)
    382  1.33.2.1    martin 		error("ststrcat() over length botch");
    383  1.33.2.1    martin 	str = (char *)stalloc((int)tlen + 1);	/* 1 for \0 */
    384  1.33.2.1    martin 	str[tlen] = '\0';	/* in case of no args  */
    385  1.33.2.1    martin 
    386  1.33.2.1    martin 	n = 0;
    387  1.33.2.1    martin 	nxt = str;
    388  1.33.2.1    martin 	va_start(ap, lp);
    389  1.33.2.1    martin 	arg = va_arg(ap, const char *);
    390  1.33.2.1    martin 	while (arg != STSTRC_END) {
    391  1.33.2.1    martin 		if (n < sizeof(alen)/sizeof(alen[0]))
    392  1.33.2.1    martin 			len = alen[n++];
    393  1.33.2.1    martin 		else
    394  1.33.2.1    martin 			len = strlen(arg);
    395  1.33.2.1    martin 
    396  1.33.2.1    martin 		scopy(arg, nxt);
    397  1.33.2.1    martin 		nxt += len;
    398  1.33.2.1    martin 
    399  1.33.2.1    martin 		arg = va_arg(ap, const char *);
    400  1.33.2.1    martin 	}
    401  1.33.2.1    martin 	va_end(ap);
    402  1.33.2.1    martin 
    403  1.33.2.1    martin 	return str;
    404  1.33.2.1    martin }
    405  1.33.2.1    martin 
    406