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