Home | History | Annotate | Line # | Download | only in sh
memalloc.c revision 1.21
      1 /*	$NetBSD: memalloc.c,v 1.21 1998/01/31 12:36:17 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)memalloc.c	8.3 (Berkeley) 5/4/95";
     43 #else
     44 __RCSID("$NetBSD: memalloc.c,v 1.21 1998/01/31 12:36:17 christos Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <stdlib.h>
     49 #include <unistd.h>
     50 
     51 #include "shell.h"
     52 #include "output.h"
     53 #include "memalloc.h"
     54 #include "error.h"
     55 #include "machdep.h"
     56 #include "mystring.h"
     57 
     58 /*
     59  * Like malloc, but returns an error when out of space.
     60  */
     61 
     62 pointer
     63 ckmalloc(nbytes)
     64 	int nbytes;
     65 {
     66 	pointer p;
     67 
     68 	if ((p = malloc(nbytes)) == NULL)
     69 		error("Out of space");
     70 	return p;
     71 }
     72 
     73 
     74 /*
     75  * Same for realloc.
     76  */
     77 
     78 pointer
     79 ckrealloc(p, nbytes)
     80 	pointer p;
     81 	int nbytes;
     82 {
     83 
     84 	if ((p = realloc(p, nbytes)) == NULL)
     85 		error("Out of space");
     86 	return p;
     87 }
     88 
     89 
     90 /*
     91  * Make a copy of a string in safe storage.
     92  */
     93 
     94 char *
     95 savestr(s)
     96 	char *s;
     97 	{
     98 	char *p;
     99 
    100 	p = ckmalloc(strlen(s) + 1);
    101 	scopy(s, p);
    102 	return p;
    103 }
    104 
    105 
    106 /*
    107  * Parse trees for commands are allocated in lifo order, so we use a stack
    108  * to make this more efficient, and also to avoid all sorts of exception
    109  * handling code to handle interrupts in the middle of a parse.
    110  *
    111  * The size 504 was chosen because the Ultrix malloc handles that size
    112  * well.
    113  */
    114 
    115 #define MINSIZE 504		/* minimum size of a block */
    116 
    117 
    118 struct stack_block {
    119 	struct stack_block *prev;
    120 	char space[MINSIZE];
    121 };
    122 
    123 struct stack_block stackbase;
    124 struct stack_block *stackp = &stackbase;
    125 char *stacknxt = stackbase.space;
    126 int stacknleft = MINSIZE;
    127 int sstrnleft;
    128 int herefd = -1;
    129 
    130 
    131 
    132 pointer
    133 stalloc(nbytes)
    134 	int nbytes;
    135 {
    136 	char *p;
    137 
    138 	nbytes = ALIGN(nbytes);
    139 	if (nbytes > stacknleft) {
    140 		int blocksize;
    141 		struct stack_block *sp;
    142 
    143 		blocksize = nbytes;
    144 		if (blocksize < MINSIZE)
    145 			blocksize = MINSIZE;
    146 		INTOFF;
    147 		sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
    148 		sp->prev = stackp;
    149 		stacknxt = sp->space;
    150 		stacknleft = blocksize;
    151 		stackp = sp;
    152 		INTON;
    153 	}
    154 	p = stacknxt;
    155 	stacknxt += nbytes;
    156 	stacknleft -= nbytes;
    157 	return p;
    158 }
    159 
    160 
    161 void
    162 stunalloc(p)
    163 	pointer p;
    164 	{
    165 	if (p == NULL) {		/*DEBUG */
    166 		write(2, "stunalloc\n", 10);
    167 		abort();
    168 	}
    169 	stacknleft += stacknxt - (char *)p;
    170 	stacknxt = p;
    171 }
    172 
    173 
    174 
    175 void
    176 setstackmark(mark)
    177 	struct stackmark *mark;
    178 	{
    179 	mark->stackp = stackp;
    180 	mark->stacknxt = stacknxt;
    181 	mark->stacknleft = stacknleft;
    182 }
    183 
    184 
    185 void
    186 popstackmark(mark)
    187 	struct stackmark *mark;
    188 	{
    189 	struct stack_block *sp;
    190 
    191 	INTOFF;
    192 	while (stackp != mark->stackp) {
    193 		sp = stackp;
    194 		stackp = sp->prev;
    195 		ckfree(sp);
    196 	}
    197 	stacknxt = mark->stacknxt;
    198 	stacknleft = mark->stacknleft;
    199 	INTON;
    200 }
    201 
    202 
    203 /*
    204  * When the parser reads in a string, it wants to stick the string on the
    205  * stack and only adjust the stack pointer when it knows how big the
    206  * string is.  Stackblock (defined in stack.h) returns a pointer to a block
    207  * of space on top of the stack and stackblocklen returns the length of
    208  * this block.  Growstackblock will grow this space by at least one byte,
    209  * possibly moving it (like realloc).  Grabstackblock actually allocates the
    210  * part of the block that has been used.
    211  */
    212 
    213 void
    214 growstackblock() {
    215 	char *p;
    216 	int newlen = ALIGN(stacknleft * 2 + 100);
    217 	char *oldspace = stacknxt;
    218 	int oldlen = stacknleft;
    219 	struct stack_block *sp;
    220 
    221 	if (stacknxt == stackp->space && stackp != &stackbase) {
    222 		INTOFF;
    223 		sp = stackp;
    224 		stackp = sp->prev;
    225 		sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen);
    226 		sp->prev = stackp;
    227 		stackp = sp;
    228 		stacknxt = sp->space;
    229 		stacknleft = newlen;
    230 		INTON;
    231 	} else {
    232 		p = stalloc(newlen);
    233 		memcpy(p, oldspace, oldlen);
    234 		stacknxt = p;			/* free the space */
    235 		stacknleft += newlen;		/* we just allocated */
    236 	}
    237 }
    238 
    239 
    240 
    241 void
    242 grabstackblock(len)
    243 	int len;
    244 {
    245 	len = ALIGN(len);
    246 	stacknxt += len;
    247 	stacknleft -= len;
    248 }
    249 
    250 
    251 
    252 /*
    253  * The following routines are somewhat easier to use that the above.
    254  * The user declares a variable of type STACKSTR, which may be declared
    255  * to be a register.  The macro STARTSTACKSTR initializes things.  Then
    256  * the user uses the macro STPUTC to add characters to the string.  In
    257  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
    258  * grown as necessary.  When the user is done, she can just leave the
    259  * string there and refer to it using stackblock().  Or she can allocate
    260  * the space for it using grabstackstr().  If it is necessary to allow
    261  * someone else to use the stack temporarily and then continue to grow
    262  * the string, the user should use grabstack to allocate the space, and
    263  * then call ungrabstr(p) to return to the previous mode of operation.
    264  *
    265  * USTPUTC is like STPUTC except that it doesn't check for overflow.
    266  * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
    267  * is space for at least one character.
    268  */
    269 
    270 
    271 char *
    272 growstackstr() {
    273 	int len = stackblocksize();
    274 	if (herefd >= 0 && len >= 1024) {
    275 		xwrite(herefd, stackblock(), len);
    276 		sstrnleft = len - 1;
    277 		return stackblock();
    278 	}
    279 	growstackblock();
    280 	sstrnleft = stackblocksize() - len - 1;
    281 	return stackblock() + len;
    282 }
    283 
    284 
    285 /*
    286  * Called from CHECKSTRSPACE.
    287  */
    288 
    289 char *
    290 makestrspace() {
    291 	int len = stackblocksize() - sstrnleft;
    292 	growstackblock();
    293 	sstrnleft = stackblocksize() - len;
    294 	return stackblock() + len;
    295 }
    296 
    297 
    298 
    299 void
    300 ungrabstackstr(s, p)
    301 	char *s;
    302 	char *p;
    303 	{
    304 	stacknleft += stacknxt - s;
    305 	stacknxt = s;
    306 	sstrnleft = stacknleft - (p - s);
    307 }
    308