1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. 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 * $Id$ 37 */ 38 39 #ifndef lint 40 static char sccsid[] = "@(#)memalloc.c 8.1 (Berkeley) 5/31/93"; 41 #endif /* not lint */ 42 43 #include "shell.h" 44 #include "output.h" 45 #include "memalloc.h" 46 #include "error.h" 47 #include "machdep.h" 48 #include "mystring.h" 49 50 /* 51 * Like malloc, but returns an error when out of space. 52 */ 53 54 pointer 55 ckmalloc(nbytes) { 56 register pointer p; 57 pointer malloc(); 58 59 if ((p = malloc(nbytes)) == NULL) 60 error("Out of space"); 61 return p; 62 } 63 64 65 /* 66 * Same for realloc. 67 */ 68 69 pointer 70 ckrealloc(p, nbytes) 71 register pointer p; 72 { 73 pointer realloc(); 74 75 if ((p = realloc(p, nbytes)) == NULL) 76 error("Out of space"); 77 return p; 78 } 79 80 81 /* 82 * Make a copy of a string in safe storage. 83 */ 84 85 char * 86 savestr(s) 87 char *s; 88 { 89 register char *p; 90 91 p = ckmalloc(strlen(s) + 1); 92 scopy(s, p); 93 return p; 94 } 95 96 97 /* 98 * Parse trees for commands are allocated in lifo order, so we use a stack 99 * to make this more efficient, and also to avoid all sorts of exception 100 * handling code to handle interrupts in the middle of a parse. 101 * 102 * The size 504 was chosen because the Ultrix malloc handles that size 103 * well. 104 */ 105 106 #define MINSIZE 504 /* minimum size of a block */ 107 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 char *stacknxt = stackbase.space; 117 int stacknleft = MINSIZE; 118 int sstrnleft; 119 int herefd = -1; 120 121 122 123 pointer 124 stalloc(nbytes) { 125 register char *p; 126 127 nbytes = ALIGN(nbytes); 128 if (nbytes > stacknleft) { 129 int 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 = stackp; 138 stacknxt = sp->space; 139 stacknleft = blocksize; 140 stackp = sp; 141 INTON; 142 } 143 p = stacknxt; 144 stacknxt += nbytes; 145 stacknleft -= nbytes; 146 return p; 147 } 148 149 150 void 151 stunalloc(p) 152 pointer p; 153 { 154 if (p == NULL) { /*DEBUG */ 155 write(2, "stunalloc\n", 10); 156 abort(); 157 } 158 stacknleft += stacknxt - (char *)p; 159 stacknxt = p; 160 } 161 162 163 164 void 165 setstackmark(mark) 166 struct stackmark *mark; 167 { 168 mark->stackp = stackp; 169 mark->stacknxt = stacknxt; 170 mark->stacknleft = stacknleft; 171 } 172 173 174 void 175 popstackmark(mark) 176 struct stackmark *mark; 177 { 178 struct stack_block *sp; 179 180 INTOFF; 181 while (stackp != mark->stackp) { 182 sp = stackp; 183 stackp = sp->prev; 184 ckfree(sp); 185 } 186 stacknxt = mark->stacknxt; 187 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() { 204 char *p; 205 int newlen = stacknleft * 2 + 100; 206 char *oldspace = stacknxt; 207 int oldlen = stacknleft; 208 struct stack_block *sp; 209 210 if (stacknxt == stackp->space && stackp != &stackbase) { 211 INTOFF; 212 sp = stackp; 213 stackp = sp->prev; 214 sp = ckrealloc((pointer)sp, sizeof(struct stack_block) - MINSIZE + newlen); 215 sp->prev = stackp; 216 stackp = sp; 217 stacknxt = sp->space; 218 stacknleft = newlen; 219 INTON; 220 } else { 221 p = stalloc(newlen); 222 bcopy(oldspace, p, oldlen); 223 stacknxt = p; /* free the space */ 224 stacknleft += newlen; /* we just allocated */ 225 } 226 } 227 228 229 230 void 231 grabstackblock(len) { 232 len = ALIGN(len); 233 stacknxt += len; 234 stacknleft -= len; 235 } 236 237 238 239 /* 240 * The following routines are somewhat easier to use that the above. 241 * The user declares a variable of type STACKSTR, which may be declared 242 * to be a register. The macro STARTSTACKSTR initializes things. Then 243 * the user uses the macro STPUTC to add characters to the string. In 244 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is 245 * grown as necessary. When the user is done, she can just leave the 246 * string there and refer to it using stackblock(). Or she can allocate 247 * the space for it using grabstackstr(). If it is necessary to allow 248 * someone else to use the stack temporarily and then continue to grow 249 * the string, the user should use grabstack to allocate the space, and 250 * then call ungrabstr(p) to return to the previous mode of operation. 251 * 252 * USTPUTC is like STPUTC except that it doesn't check for overflow. 253 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there 254 * is space for at least one character. 255 */ 256 257 258 char * 259 growstackstr() { 260 int len = stackblocksize(); 261 if (herefd >= 0 && len >= 1024) { 262 xwrite(herefd, stackblock(), len); 263 sstrnleft = len - 1; 264 return stackblock(); 265 } 266 growstackblock(); 267 sstrnleft = stackblocksize() - len - 1; 268 return stackblock() + len; 269 } 270 271 272 /* 273 * Called from CHECKSTRSPACE. 274 */ 275 276 char * 277 makestrspace() { 278 int len = stackblocksize() - sstrnleft; 279 growstackblock(); 280 sstrnleft = stackblocksize() - len; 281 return stackblock() + len; 282 } 283 284 285 286 void 287 ungrabstackstr(s, p) 288 char *s; 289 char *p; 290 { 291 stacknleft += stacknxt - s; 292 stacknxt = s; 293 sstrnleft = stacknleft - (p - s); 294 } 295