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