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