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