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