Home | History | Annotate | Line # | Download | only in sh
memalloc.c revision 1.30.6.1
      1  1.30.6.1  christos /*	$NetBSD: memalloc.c,v 1.30.6.1 2019/06/10 21:41:04 christos 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.30.6.1  christos __RCSID("$NetBSD: memalloc.c,v 1.30.6.1 2019/06/10 21:41:04 christos Exp $");
     41      1.15       cgd #endif
     42       1.1       cgd #endif /* not lint */
     43       1.1       cgd 
     44      1.21  christos #include <stdlib.h>
     45      1.21  christos #include <unistd.h>
     46      1.21  christos 
     47       1.1       cgd #include "shell.h"
     48       1.1       cgd #include "output.h"
     49       1.1       cgd #include "memalloc.h"
     50       1.1       cgd #include "error.h"
     51       1.1       cgd #include "machdep.h"
     52       1.1       cgd #include "mystring.h"
     53       1.1       cgd 
     54       1.1       cgd /*
     55       1.1       cgd  * Like malloc, but returns an error when out of space.
     56       1.1       cgd  */
     57       1.1       cgd 
     58       1.1       cgd pointer
     59      1.29      matt ckmalloc(size_t nbytes)
     60      1.10       cgd {
     61      1.19       tls 	pointer p;
     62       1.1       cgd 
     63      1.22   mycroft 	p = malloc(nbytes);
     64      1.22   mycroft 	if (p == NULL)
     65       1.1       cgd 		error("Out of space");
     66       1.1       cgd 	return p;
     67       1.1       cgd }
     68       1.1       cgd 
     69       1.1       cgd 
     70       1.1       cgd /*
     71       1.1       cgd  * Same for realloc.
     72       1.1       cgd  */
     73       1.1       cgd 
     74       1.1       cgd pointer
     75      1.26  christos ckrealloc(pointer p, int nbytes)
     76      1.10       cgd {
     77      1.25  christos 	p = realloc(p, nbytes);
     78      1.25  christos 	if (p == NULL)
     79       1.1       cgd 		error("Out of space");
     80       1.1       cgd 	return p;
     81       1.1       cgd }
     82       1.1       cgd 
     83       1.1       cgd 
     84       1.1       cgd /*
     85       1.1       cgd  * Make a copy of a string in safe storage.
     86       1.1       cgd  */
     87       1.1       cgd 
     88       1.1       cgd char *
     89      1.27       dsl savestr(const char *s)
     90      1.25  christos {
     91      1.19       tls 	char *p;
     92       1.1       cgd 
     93       1.1       cgd 	p = ckmalloc(strlen(s) + 1);
     94       1.1       cgd 	scopy(s, p);
     95       1.1       cgd 	return p;
     96       1.1       cgd }
     97       1.1       cgd 
     98       1.1       cgd 
     99       1.1       cgd /*
    100       1.1       cgd  * Parse trees for commands are allocated in lifo order, so we use a stack
    101       1.1       cgd  * to make this more efficient, and also to avoid all sorts of exception
    102       1.1       cgd  * handling code to handle interrupts in the middle of a parse.
    103      1.12   mycroft  *
    104      1.12   mycroft  * The size 504 was chosen because the Ultrix malloc handles that size
    105      1.12   mycroft  * well.
    106       1.1       cgd  */
    107       1.1       cgd 
    108      1.12   mycroft #define MINSIZE 504		/* minimum size of a block */
    109       1.1       cgd 
    110       1.1       cgd struct stack_block {
    111       1.1       cgd 	struct stack_block *prev;
    112       1.1       cgd 	char space[MINSIZE];
    113       1.1       cgd };
    114       1.1       cgd 
    115       1.1       cgd struct stack_block stackbase;
    116       1.1       cgd struct stack_block *stackp = &stackbase;
    117      1.23  christos struct stackmark *markp;
    118       1.1       cgd char *stacknxt = stackbase.space;
    119       1.1       cgd int stacknleft = MINSIZE;
    120       1.1       cgd int sstrnleft;
    121       1.1       cgd int herefd = -1;
    122       1.1       cgd 
    123       1.1       cgd pointer
    124      1.26  christos stalloc(int nbytes)
    125      1.10       cgd {
    126      1.19       tls 	char *p;
    127       1.1       cgd 
    128      1.24  christos 	nbytes = SHELL_ALIGN(nbytes);
    129      1.12   mycroft 	if (nbytes > stacknleft) {
    130       1.1       cgd 		int blocksize;
    131       1.1       cgd 		struct stack_block *sp;
    132       1.1       cgd 
    133      1.12   mycroft 		blocksize = nbytes;
    134       1.1       cgd 		if (blocksize < MINSIZE)
    135       1.1       cgd 			blocksize = MINSIZE;
    136       1.1       cgd 		INTOFF;
    137       1.1       cgd 		sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
    138       1.1       cgd 		sp->prev = stackp;
    139       1.1       cgd 		stacknxt = sp->space;
    140       1.1       cgd 		stacknleft = blocksize;
    141       1.1       cgd 		stackp = sp;
    142       1.1       cgd 		INTON;
    143       1.1       cgd 	}
    144  1.30.6.1  christos 	INTOFF;
    145       1.1       cgd 	p = stacknxt;
    146      1.12   mycroft 	stacknxt += nbytes;
    147      1.12   mycroft 	stacknleft -= nbytes;
    148  1.30.6.1  christos 	INTON;
    149       1.1       cgd 	return p;
    150       1.1       cgd }
    151       1.1       cgd 
    152       1.1       cgd 
    153       1.1       cgd void
    154      1.26  christos stunalloc(pointer p)
    155      1.25  christos {
    156       1.1       cgd 	if (p == NULL) {		/*DEBUG */
    157       1.1       cgd 		write(2, "stunalloc\n", 10);
    158       1.1       cgd 		abort();
    159       1.1       cgd 	}
    160       1.1       cgd 	stacknleft += stacknxt - (char *)p;
    161       1.1       cgd 	stacknxt = p;
    162       1.1       cgd }
    163       1.1       cgd 
    164       1.1       cgd 
    165  1.30.6.1  christos /* save the current status of the sh stack */
    166       1.1       cgd void
    167      1.26  christos setstackmark(struct stackmark *mark)
    168      1.25  christos {
    169       1.1       cgd 	mark->stackp = stackp;
    170       1.1       cgd 	mark->stacknxt = stacknxt;
    171       1.1       cgd 	mark->stacknleft = stacknleft;
    172      1.30       kre 	mark->sstrnleft = sstrnleft;
    173      1.23  christos 	mark->marknext = markp;
    174      1.23  christos 	markp = mark;
    175       1.1       cgd }
    176       1.1       cgd 
    177  1.30.6.1  christos /* reset the stack mark, and remove it from the list of marks */
    178       1.1       cgd void
    179      1.26  christos popstackmark(struct stackmark *mark)
    180      1.25  christos {
    181  1.30.6.1  christos 	INTOFF;
    182  1.30.6.1  christos 	markp = mark->marknext;		/* delete mark from the list */
    183  1.30.6.1  christos 	rststackmark(mark);		/* and reset stack */
    184  1.30.6.1  christos 	INTON;
    185  1.30.6.1  christos }
    186  1.30.6.1  christos 
    187  1.30.6.1  christos /* reset the shell stack to its state recorded in the stack mark */
    188  1.30.6.1  christos void
    189  1.30.6.1  christos rststackmark(struct stackmark *mark)
    190  1.30.6.1  christos {
    191       1.1       cgd 	struct stack_block *sp;
    192       1.1       cgd 
    193       1.1       cgd 	INTOFF;
    194       1.1       cgd 	while (stackp != mark->stackp) {
    195  1.30.6.1  christos 		/* delete any recently allocated mem blocks */
    196       1.1       cgd 		sp = stackp;
    197       1.1       cgd 		stackp = sp->prev;
    198       1.1       cgd 		ckfree(sp);
    199       1.1       cgd 	}
    200       1.1       cgd 	stacknxt = mark->stacknxt;
    201       1.1       cgd 	stacknleft = mark->stacknleft;
    202      1.30       kre 	sstrnleft = mark->sstrnleft;
    203       1.1       cgd 	INTON;
    204       1.1       cgd }
    205       1.1       cgd 
    206       1.1       cgd 
    207       1.1       cgd /*
    208       1.1       cgd  * When the parser reads in a string, it wants to stick the string on the
    209       1.1       cgd  * stack and only adjust the stack pointer when it knows how big the
    210       1.1       cgd  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
    211       1.1       cgd  * of space on top of the stack and stackblocklen returns the length of
    212       1.1       cgd  * this block.  Growstackblock will grow this space by at least one byte,
    213       1.1       cgd  * possibly moving it (like realloc).  Grabstackblock actually allocates the
    214       1.1       cgd  * part of the block that has been used.
    215       1.1       cgd  */
    216       1.1       cgd 
    217       1.1       cgd void
    218      1.26  christos growstackblock(void)
    219      1.25  christos {
    220      1.24  christos 	int newlen = SHELL_ALIGN(stacknleft * 2 + 100);
    221       1.1       cgd 
    222  1.30.6.1  christos 	INTOFF;
    223       1.1       cgd 	if (stacknxt == stackp->space && stackp != &stackbase) {
    224      1.25  christos 		struct stack_block *oldstackp;
    225      1.25  christos 		struct stackmark *xmark;
    226      1.25  christos 		struct stack_block *sp;
    227      1.25  christos 
    228      1.23  christos 		oldstackp = stackp;
    229       1.1       cgd 		sp = stackp;
    230       1.1       cgd 		stackp = sp->prev;
    231      1.25  christos 		sp = ckrealloc((pointer)sp,
    232      1.25  christos 		    sizeof(struct stack_block) - MINSIZE + newlen);
    233       1.1       cgd 		sp->prev = stackp;
    234       1.1       cgd 		stackp = sp;
    235       1.1       cgd 		stacknxt = sp->space;
    236  1.30.6.1  christos 		sstrnleft += newlen - stacknleft;
    237       1.1       cgd 		stacknleft = newlen;
    238      1.25  christos 
    239      1.25  christos 		/*
    240      1.25  christos 		 * Stack marks pointing to the start of the old block
    241      1.25  christos 		 * must be relocated to point to the new block
    242      1.25  christos 		 */
    243      1.25  christos 		xmark = markp;
    244      1.25  christos 		while (xmark != NULL && xmark->stackp == oldstackp) {
    245      1.25  christos 			xmark->stackp = stackp;
    246      1.25  christos 			xmark->stacknxt = stacknxt;
    247  1.30.6.1  christos 			xmark->sstrnleft += stacknleft - xmark->stacknleft;
    248      1.25  christos 			xmark->stacknleft = stacknleft;
    249      1.25  christos 			xmark = xmark->marknext;
    250      1.23  christos 		}
    251       1.1       cgd 	} else {
    252      1.25  christos 		char *oldspace = stacknxt;
    253      1.25  christos 		int oldlen = stacknleft;
    254      1.25  christos 		char *p = stalloc(newlen);
    255      1.25  christos 
    256      1.25  christos 		(void)memcpy(p, oldspace, oldlen);
    257       1.1       cgd 		stacknxt = p;			/* free the space */
    258      1.17       cgd 		stacknleft += newlen;		/* we just allocated */
    259       1.1       cgd 	}
    260  1.30.6.1  christos 	INTON;
    261       1.1       cgd }
    262       1.1       cgd 
    263       1.1       cgd void
    264      1.26  christos grabstackblock(int len)
    265      1.10       cgd {
    266      1.24  christos 	len = SHELL_ALIGN(len);
    267  1.30.6.1  christos 	INTOFF;
    268       1.1       cgd 	stacknxt += len;
    269       1.1       cgd 	stacknleft -= len;
    270  1.30.6.1  christos 	INTON;
    271       1.1       cgd }
    272       1.1       cgd 
    273       1.1       cgd /*
    274      1.26  christos  * The following routines are somewhat easier to use than the above.
    275       1.1       cgd  * The user declares a variable of type STACKSTR, which may be declared
    276       1.1       cgd  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
    277       1.1       cgd  * the user uses the macro STPUTC to add characters to the string.  In
    278       1.1       cgd  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
    279       1.1       cgd  * grown as necessary.  When the user is done, she can just leave the
    280       1.1       cgd  * string there and refer to it using stackblock().  Or she can allocate
    281       1.1       cgd  * the space for it using grabstackstr().  If it is necessary to allow
    282       1.1       cgd  * someone else to use the stack temporarily and then continue to grow
    283       1.1       cgd  * the string, the user should use grabstack to allocate the space, and
    284       1.1       cgd  * then call ungrabstr(p) to return to the previous mode of operation.
    285       1.1       cgd  *
    286       1.1       cgd  * USTPUTC is like STPUTC except that it doesn't check for overflow.
    287       1.1       cgd  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
    288       1.1       cgd  * is space for at least one character.
    289       1.1       cgd  */
    290       1.1       cgd 
    291       1.1       cgd char *
    292      1.26  christos growstackstr(void)
    293      1.25  christos {
    294       1.1       cgd 	int len = stackblocksize();
    295       1.1       cgd 	if (herefd >= 0 && len >= 1024) {
    296       1.1       cgd 		xwrite(herefd, stackblock(), len);
    297       1.1       cgd 		sstrnleft = len - 1;
    298       1.1       cgd 		return stackblock();
    299       1.1       cgd 	}
    300       1.1       cgd 	growstackblock();
    301       1.1       cgd 	sstrnleft = stackblocksize() - len - 1;
    302       1.1       cgd 	return stackblock() + len;
    303       1.1       cgd }
    304       1.1       cgd 
    305       1.1       cgd /*
    306       1.1       cgd  * Called from CHECKSTRSPACE.
    307       1.1       cgd  */
    308       1.1       cgd 
    309       1.1       cgd char *
    310      1.26  christos makestrspace(void)
    311      1.25  christos {
    312       1.1       cgd 	int len = stackblocksize() - sstrnleft;
    313       1.1       cgd 	growstackblock();
    314       1.1       cgd 	sstrnleft = stackblocksize() - len;
    315       1.1       cgd 	return stackblock() + len;
    316       1.1       cgd }
    317       1.1       cgd 
    318      1.30       kre /*
    319      1.30       kre  * Note that this only works to release stack space for reuse
    320      1.30       kre  * if nothing else has allocated space on the stack since the grabstackstr()
    321      1.30       kre  *
    322      1.30       kre  * "s" is the start of the area to be released, and "p" represents the end
    323      1.30       kre  * of the string we have stored beyond there and are now releasing.
    324      1.30       kre  * (ie: "p" should be the same as in the call to grabstackstr()).
    325      1.30       kre  *
    326      1.30       kre  * stunalloc(s) and ungrabstackstr(s, p) are almost interchangable after
    327      1.30       kre  * a grabstackstr(), however the latter also returns string space so we
    328      1.30       kre  * can just continue with STPUTC() etc without needing a new STARTSTACKSTR(s)
    329      1.30       kre  */
    330       1.1       cgd void
    331      1.26  christos ungrabstackstr(char *s, char *p)
    332      1.25  christos {
    333      1.30       kre #ifdef DEBUG
    334      1.30       kre 	if (s < stacknxt || stacknxt + stacknleft < s)
    335      1.30       kre 		abort();
    336      1.30       kre #endif
    337       1.1       cgd 	stacknleft += stacknxt - s;
    338       1.1       cgd 	stacknxt = s;
    339       1.1       cgd 	sstrnleft = stacknleft - (p - s);
    340       1.1       cgd }
    341