| 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 | #include <stddef.h>
|
|---|
| 45 | #include <assert.h>
|
|---|
| 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 "shinstance.h"
|
|---|
| 54 | #include "nodes.h"
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Like malloc, but returns an error when out of space.
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | pointer
|
|---|
| 61 | ckmalloc(shinstance *psh, size_t nbytes)
|
|---|
| 62 | {
|
|---|
| 63 | pointer p;
|
|---|
| 64 |
|
|---|
| 65 | p = sh_malloc(psh, nbytes);
|
|---|
| 66 | if (p == NULL)
|
|---|
| 67 | error(psh, "Out of space");
|
|---|
| 68 | return p;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | /*
|
|---|
| 73 | * Same for realloc.
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 | pointer
|
|---|
| 77 | ckrealloc(struct shinstance *psh, pointer p, size_t nbytes)
|
|---|
| 78 | {
|
|---|
| 79 | p = sh_realloc(psh, p, nbytes);
|
|---|
| 80 | if (p == NULL)
|
|---|
| 81 | error(psh, "Out of space");
|
|---|
| 82 | return p;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | /*
|
|---|
| 87 | * Make a copy of a string in safe storage.
|
|---|
| 88 | */
|
|---|
| 89 |
|
|---|
| 90 | char *
|
|---|
| 91 | savestr(struct shinstance *psh, const char *s)
|
|---|
| 92 | {
|
|---|
| 93 | char *p;
|
|---|
| 94 | size_t len = strlen(s);
|
|---|
| 95 |
|
|---|
| 96 | p = ckmalloc(psh, len + 1);
|
|---|
| 97 | memcpy(p, s, len + 1);
|
|---|
| 98 | return p;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | * Parse trees for commands are allocated in lifo order, so we use a stack
|
|---|
| 104 | * to make this more efficient, and also to avoid all sorts of exception
|
|---|
| 105 | * handling code to handle interrupts in the middle of a parse.
|
|---|
| 106 | *
|
|---|
| 107 | * The size 504 was chosen because the Ultrix malloc handles that size
|
|---|
| 108 | * well.
|
|---|
| 109 | */
|
|---|
| 110 |
|
|---|
| 111 | //#define MINSIZE 504 /* minimum size of a block */
|
|---|
| 112 |
|
|---|
| 113 | //struct stack_block {
|
|---|
| 114 | // struct stack_block *prev;
|
|---|
| 115 | // char space[MINSIZE];
|
|---|
| 116 | //};
|
|---|
| 117 |
|
|---|
| 118 | //struct stack_block stackbase;
|
|---|
| 119 | //struct stack_block *stackp = &stackbase;
|
|---|
| 120 | //struct stackmark *markp;
|
|---|
| 121 | //char *stacknxt = stackbase.space;
|
|---|
| 122 | //int stacknleft = MINSIZE;
|
|---|
| 123 | //int sstrnleft;
|
|---|
| 124 | //int herefd = -1;
|
|---|
| 125 |
|
|---|
| 126 | pointer
|
|---|
| 127 | stalloc(shinstance *psh, size_t nbytes)
|
|---|
| 128 | {
|
|---|
| 129 | char *p;
|
|---|
| 130 |
|
|---|
| 131 | nbytes = SHELL_ALIGN(nbytes);
|
|---|
| 132 | if (nbytes > (size_t)psh->stacknleft || psh->stacknleft < 0) {
|
|---|
| 133 | size_t blocksize;
|
|---|
| 134 | struct stack_block *sp;
|
|---|
| 135 |
|
|---|
| 136 | blocksize = nbytes;
|
|---|
| 137 | if (blocksize < MINSIZE)
|
|---|
| 138 | blocksize = MINSIZE;
|
|---|
| 139 | INTOFF;
|
|---|
| 140 | sp = ckmalloc(psh, sizeof(struct stack_block) - MINSIZE + blocksize);
|
|---|
| 141 | sp->prev = psh->stackp;
|
|---|
| 142 | psh->stacknxt = sp->space;
|
|---|
| 143 | psh->stacknleft = (int)blocksize;
|
|---|
| 144 | psh->stackp = sp;
|
|---|
| 145 | INTON;
|
|---|
| 146 | }
|
|---|
| 147 | p = psh->stacknxt;
|
|---|
| 148 | psh->stacknxt += nbytes;
|
|---|
| 149 | psh->stacknleft -= (int)nbytes;
|
|---|
| 150 | return p;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | char *
|
|---|
| 155 | stsavestr(struct shinstance *psh, const char *src)
|
|---|
| 156 | {
|
|---|
| 157 | if (src) {
|
|---|
| 158 | size_t size = strlen(src) + 1;
|
|---|
| 159 | char *dst = (char *)stalloc(psh, size);
|
|---|
| 160 | return (char *)memcpy(dst, src, size);
|
|---|
| 161 | }
|
|---|
| 162 | return NULL;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | void
|
|---|
| 167 | stunalloc(shinstance *psh, pointer p)
|
|---|
| 168 | {
|
|---|
| 169 | if (p == NULL) { /*DEBUG */
|
|---|
| 170 | shfile_write(&psh->fdtab, 2, "stunalloc\n", 10);
|
|---|
| 171 | sh_abort(psh);
|
|---|
| 172 | }
|
|---|
| 173 | psh->stacknleft += (int)(psh->stacknxt - (char *)p);
|
|---|
| 174 | psh->stacknxt = p;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 | void
|
|---|
| 180 | setstackmark(shinstance *psh, struct stackmark *mark)
|
|---|
| 181 | {
|
|---|
| 182 | mark->stackp = psh->stackp;
|
|---|
| 183 | mark->stacknxt = psh->stacknxt;
|
|---|
| 184 | mark->stacknleft = psh->stacknleft;
|
|---|
| 185 | mark->marknext = psh->markp;
|
|---|
| 186 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 187 | mark->pstacksize = psh->pstacksize;
|
|---|
| 188 | #endif
|
|---|
| 189 | psh->markp = mark;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | void
|
|---|
| 194 | popstackmark(shinstance *psh, struct stackmark *mark)
|
|---|
| 195 | {
|
|---|
| 196 | struct stack_block *sp;
|
|---|
| 197 |
|
|---|
| 198 | INTOFF;
|
|---|
| 199 | psh->markp = mark->marknext;
|
|---|
| 200 | while (psh->stackp != mark->stackp) {
|
|---|
| 201 | sp = psh->stackp;
|
|---|
| 202 | psh->stackp = sp->prev;
|
|---|
| 203 | ckfree(psh, sp);
|
|---|
| 204 | }
|
|---|
| 205 | psh->stacknxt = mark->stacknxt;
|
|---|
| 206 | psh->stacknleft = mark->stacknleft;
|
|---|
| 207 |
|
|---|
| 208 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 209 | pstackpop(psh, mark->pstacksize);
|
|---|
| 210 | #endif
|
|---|
| 211 | INTON;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | /*
|
|---|
| 216 | * When the parser reads in a string, it wants to stick the string on the
|
|---|
| 217 | * stack and only adjust the stack pointer when it knows how big the
|
|---|
| 218 | * string is. Stackblock (defined in stack.h) returns a pointer to a block
|
|---|
| 219 | * of space on top of the stack and stackblocklen returns the length of
|
|---|
| 220 | * this block. Growstackblock will grow this space by at least one byte,
|
|---|
| 221 | * possibly moving it (like realloc). Grabstackblock actually allocates the
|
|---|
| 222 | * part of the block that has been used.
|
|---|
| 223 | */
|
|---|
| 224 |
|
|---|
| 225 | void
|
|---|
| 226 | growstackblock(shinstance *psh)
|
|---|
| 227 | {
|
|---|
| 228 | int newlen = SHELL_ALIGN(psh->stacknleft * 2 + 100);
|
|---|
| 229 |
|
|---|
| 230 | if (psh->stacknxt == psh->stackp->space && psh->stackp != &psh->stackbase) {
|
|---|
| 231 | struct stack_block *oldstackp;
|
|---|
| 232 | struct stackmark *xmark;
|
|---|
| 233 | struct stack_block *sp;
|
|---|
| 234 |
|
|---|
| 235 | INTOFF;
|
|---|
| 236 | oldstackp = psh->stackp;
|
|---|
| 237 | sp = psh->stackp;
|
|---|
| 238 | psh->stackp = sp->prev;
|
|---|
| 239 | sp = ckrealloc(psh, (pointer)sp,
|
|---|
| 240 | sizeof(struct stack_block) - MINSIZE + newlen);
|
|---|
| 241 | sp->prev = psh->stackp;
|
|---|
| 242 | psh->stackp = sp;
|
|---|
| 243 | psh->stacknxt = sp->space;
|
|---|
| 244 | psh->stacknleft = newlen;
|
|---|
| 245 |
|
|---|
| 246 | /*
|
|---|
| 247 | * Stack marks pointing to the start of the old block
|
|---|
| 248 | * must be relocated to point to the new block
|
|---|
| 249 | */
|
|---|
| 250 | xmark = psh->markp;
|
|---|
| 251 | while (xmark != NULL && xmark->stackp == oldstackp) {
|
|---|
| 252 | xmark->stackp = psh->stackp;
|
|---|
| 253 | xmark->stacknxt = psh->stacknxt;
|
|---|
| 254 | xmark->stacknleft = psh->stacknleft;
|
|---|
| 255 | xmark = xmark->marknext;
|
|---|
| 256 | }
|
|---|
| 257 | INTON;
|
|---|
| 258 | } else {
|
|---|
| 259 | char *oldspace = psh->stacknxt;
|
|---|
| 260 | int oldlen = psh->stacknleft;
|
|---|
| 261 | char *p = stalloc(psh, newlen);
|
|---|
| 262 |
|
|---|
| 263 | (void)memcpy(p, oldspace, oldlen);
|
|---|
| 264 | psh->stacknxt = p; /* free the space */
|
|---|
| 265 | psh->stacknleft += newlen; /* we just allocated */
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | void
|
|---|
| 270 | grabstackblock(shinstance *psh, int len)
|
|---|
| 271 | {
|
|---|
| 272 | len = SHELL_ALIGN(len);
|
|---|
| 273 | psh->stacknxt += len;
|
|---|
| 274 | psh->stacknleft -= len;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /*
|
|---|
| 278 | * The following routines are somewhat easier to use than the above.
|
|---|
| 279 | * The user declares a variable of type STACKSTR, which may be declared
|
|---|
| 280 | * to be a register. The macro STARTSTACKSTR initializes things. Then
|
|---|
| 281 | * the user uses the macro STPUTC to add characters to the string. In
|
|---|
| 282 | * effect, STPUTC(psh, c, p) is the same as *p++ = c except that the stack is
|
|---|
| 283 | * grown as necessary. When the user is done, she can just leave the
|
|---|
| 284 | * string there and refer to it using stackblock(psh). Or she can allocate
|
|---|
| 285 | * the space for it using grabstackstr(). If it is necessary to allow
|
|---|
| 286 | * someone else to use the stack temporarily and then continue to grow
|
|---|
| 287 | * the string, the user should use grabstack to allocate the space, and
|
|---|
| 288 | * then call ungrabstr(p) to return to the previous mode of operation.
|
|---|
| 289 | *
|
|---|
| 290 | * USTPUTC is like STPUTC except that it doesn't check for overflow.
|
|---|
| 291 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
|
|---|
| 292 | * is space for at least one character.
|
|---|
| 293 | */
|
|---|
| 294 |
|
|---|
| 295 | char *
|
|---|
| 296 | growstackstr(shinstance *psh)
|
|---|
| 297 | {
|
|---|
| 298 | int len = stackblocksize(psh);
|
|---|
| 299 | if (psh->herefd >= 0 && len >= 1024) {
|
|---|
| 300 | xwrite(psh, psh->herefd, stackblock(psh), len);
|
|---|
| 301 | psh->sstrnleft = len - 1;
|
|---|
| 302 | return stackblock(psh);
|
|---|
| 303 | }
|
|---|
| 304 | growstackblock(psh);
|
|---|
| 305 | psh->sstrnleft = stackblocksize(psh) - len - 1;
|
|---|
| 306 | return stackblock(psh) + len;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /*
|
|---|
| 310 | * Called from CHECKSTRSPACE.
|
|---|
| 311 | */
|
|---|
| 312 |
|
|---|
| 313 | char *
|
|---|
| 314 | makestrspace(shinstance *psh)
|
|---|
| 315 | {
|
|---|
| 316 | int len = stackblocksize(psh) - psh->sstrnleft;
|
|---|
| 317 | growstackblock(psh);
|
|---|
| 318 | psh->sstrnleft = stackblocksize(psh) - len;
|
|---|
| 319 | return stackblock(psh) + len;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 | /*
|
|---|
| 324 | * Got better control having a dedicated function for this.
|
|---|
| 325 | *
|
|---|
| 326 | * Was: #define grabstackstr(psh, p) stalloc((psh), stackblocksize(psh) - (psh)->sstrnleft)
|
|---|
| 327 | */
|
|---|
| 328 | char *
|
|---|
| 329 | grabstackstr(shinstance *psh, char *end)
|
|---|
| 330 | {
|
|---|
| 331 | char * const pstart = stackblock(psh);
|
|---|
| 332 | size_t nbytes = (size_t)(end - pstart);
|
|---|
| 333 |
|
|---|
| 334 | assert((uintptr_t)end >= (uintptr_t)pstart);
|
|---|
| 335 | /*assert(end[-1] == '\0'); - not if it's followed by ungrabstrackstr(), sigh. */
|
|---|
| 336 | assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
|
|---|
| 337 | assert(stackblocksize(psh) - psh->sstrnleft >= nbytes);
|
|---|
| 338 |
|
|---|
| 339 | nbytes = SHELL_ALIGN(nbytes);
|
|---|
| 340 | psh->stacknxt += nbytes;
|
|---|
| 341 | psh->stacknleft -= (int)nbytes;
|
|---|
| 342 | assert(psh->stacknleft >= 0);
|
|---|
| 343 |
|
|---|
| 344 | return pstart;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | void
|
|---|
| 348 | ungrabstackstr(shinstance *psh, char *s, char *p)
|
|---|
| 349 | {
|
|---|
| 350 | assert((size_t)(psh->stacknxt - p) <= SHELL_SIZE);
|
|---|
| 351 | assert((uintptr_t)s >= (uintptr_t)&psh->stackp->space[0]);
|
|---|
| 352 | assert((uintptr_t)p >= (uintptr_t)s);
|
|---|
| 353 |
|
|---|
| 354 | psh->stacknleft += (int)(psh->stacknxt - s);
|
|---|
| 355 | psh->stacknxt = s;
|
|---|
| 356 | psh->sstrnleft = (int)(psh->stacknleft - (p - s));
|
|---|
| 357 |
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 | /*
|
|---|
| 362 | * Parser stack allocator.
|
|---|
| 363 | */
|
|---|
| 364 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 365 |
|
|---|
| 366 | unsigned pstackrelease(shinstance *psh, pstack_block *pst, const char *caller)
|
|---|
| 367 | {
|
|---|
| 368 | unsigned refs;
|
|---|
| 369 | if (pst) {
|
|---|
| 370 | refs = sh_atomic_dec(&pst->refs);
|
|---|
| 371 | TRACE2((NULL, "pstackrelease: %p - %u refs (%s)\n", pst, refs, caller)); K_NOREF(caller);
|
|---|
| 372 | if (refs == 0) {
|
|---|
| 373 | shinstance * const psh = shthread_get_shell();
|
|---|
| 374 | struct stack_block *top;
|
|---|
| 375 | while ((top = pst->top) != &pst->first)
|
|---|
| 376 | {
|
|---|
| 377 | pst->top = top->prev;
|
|---|
| 378 | assert(pst->top);
|
|---|
| 379 | top->prev = NULL;
|
|---|
| 380 | sh_free(psh, top);
|
|---|
| 381 | }
|
|---|
| 382 | pst->nextbyte = NULL;
|
|---|
| 383 | pst->top = NULL;
|
|---|
| 384 | /** @todo push into an alloc cache rather than freeing it */
|
|---|
| 385 | sh_free(psh, pst);
|
|---|
| 386 | }
|
|---|
| 387 | } else
|
|---|
| 388 | refs = 0;
|
|---|
| 389 | return refs;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | void pstackpop(shinstance *psh, unsigned target)
|
|---|
| 393 | {
|
|---|
| 394 | assert(target <= psh->pstacksize);
|
|---|
| 395 | while (target < psh->pstacksize) {
|
|---|
| 396 | unsigned idx = --psh->pstacksize;
|
|---|
| 397 | pstack_block *psk = psh->pstack[idx];
|
|---|
| 398 | psh->pstack[idx] = NULL;
|
|---|
| 399 | if (psh->curpstack == psk)
|
|---|
| 400 | psh->curpstack = idx > 0 ? psh->pstack[idx - 1] : NULL;
|
|---|
| 401 | pstackrelease(psh, psk, "popstackmark");
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | # ifndef NDEBUG
|
|---|
| 405 | if (psh->curpstack) {
|
|---|
| 406 | unsigned i;
|
|---|
| 407 | for (i = 0; i < psh->pstacksize; i++)
|
|---|
| 408 | if (psh->curpstack == psh->pstack[i])
|
|---|
| 409 | break;
|
|---|
| 410 | assert(i < psh->pstacksize);
|
|---|
| 411 | }
|
|---|
| 412 | # endif
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 | unsigned pstackretain(pstack_block *pst)
|
|---|
| 417 | {
|
|---|
| 418 | unsigned refs = sh_atomic_inc(&pst->refs);
|
|---|
| 419 | assert(refs > 1);
|
|---|
| 420 | assert(refs < 256 /* bogus, but useful */);
|
|---|
| 421 | return refs;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | K_INLINE void pstackpush(shinstance *psh, pstack_block *pst)
|
|---|
| 425 | {
|
|---|
| 426 | unsigned i = psh->pstacksize;
|
|---|
| 427 | if (i + 1 < psh->pstackalloced) {
|
|---|
| 428 | /* likely, except for the first time */
|
|---|
| 429 | } else {
|
|---|
| 430 | psh->pstack = (pstack_block **)ckrealloc(psh, psh->pstack, sizeof(psh->pstack[0]) * (i + 32));
|
|---|
| 431 | memset(&psh->pstack[i], 0, sizeof(psh->pstack[0]) * 32);
|
|---|
| 432 | }
|
|---|
| 433 | psh->pstack[i] = pst;
|
|---|
| 434 | psh->pstacksize = i + 1;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | /* Does not make it current! */
|
|---|
| 438 | unsigned pstackretainpush(shinstance *psh, pstack_block *pst)
|
|---|
| 439 | {
|
|---|
| 440 | unsigned refs = pstackretain(pst);
|
|---|
| 441 | pstackpush(psh, pst);
|
|---|
| 442 | TRACE2((psh, "pstackretainpush: %p - entry %u - %u refs\n", pst, psh->pstacksize - 1, refs));
|
|---|
| 443 | return refs;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | pstack_block *pstackallocpush(shinstance *psh)
|
|---|
| 447 | {
|
|---|
| 448 | size_t const blocksize = offsetof(pstack_block, first.space) + MINSIZE;
|
|---|
| 449 | pstack_block *pst;
|
|---|
| 450 |
|
|---|
| 451 | INTOFF;
|
|---|
| 452 |
|
|---|
| 453 | /*
|
|---|
| 454 | * Allocate and initialize it.
|
|---|
| 455 | */
|
|---|
| 456 | pst = (pstack_block *)ckmalloc(psh, blocksize);
|
|---|
| 457 | pst->nextbyte = &pst->first.space[0];
|
|---|
| 458 | pst->avail = blocksize - offsetof(pstack_block, first.space);
|
|---|
| 459 | pst->topsize = blocksize - offsetof(pstack_block, first.space);
|
|---|
| 460 | pst->strleft = 0;
|
|---|
| 461 | pst->top = &pst->first;
|
|---|
| 462 | pst->allocations = 0;
|
|---|
| 463 | pst->bytesalloced = 0;
|
|---|
| 464 | pst->nodesalloced = 0;
|
|---|
| 465 | pst->entriesalloced = 0;
|
|---|
| 466 | pst->strbytesalloced = 0;
|
|---|
| 467 | pst->blocks = 0;
|
|---|
| 468 | pst->fragmentation = 0;
|
|---|
| 469 | pst->refs = 1;
|
|---|
| 470 | pst->padding = 42;
|
|---|
| 471 | pst->first.prev = NULL;
|
|---|
| 472 |
|
|---|
| 473 | /*
|
|---|
| 474 | * Push it onto the stack and make it current.
|
|---|
| 475 | */
|
|---|
| 476 | pstackpush(psh, pst);
|
|---|
| 477 | psh->curpstack = pst;
|
|---|
| 478 |
|
|---|
| 479 | INTON;
|
|---|
| 480 | TRACE2((psh, "pstackallocpush: %p - entry %u\n", pst, psh->pstacksize - 1));
|
|---|
| 481 | return pst;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | /**
|
|---|
| 485 | * Allocates and pushes a new block onto the stack, min payload size @a nbytes.
|
|---|
| 486 | */
|
|---|
| 487 | static void pstallocnewblock(shinstance *psh, pstack_block *pst, size_t nbytes)
|
|---|
| 488 | {
|
|---|
| 489 | /* Allocate a new stack node. */
|
|---|
| 490 | struct stack_block *sp;
|
|---|
| 491 | size_t const blocksize = nbytes <= MINSIZE
|
|---|
| 492 | ? offsetof(struct stack_block, space) + MINSIZE
|
|---|
| 493 | : K_ALIGN_Z(nbytes + offsetof(struct stack_block, space), 1024);
|
|---|
| 494 |
|
|---|
| 495 | INTOFF;
|
|---|
| 496 | sp = ckmalloc(psh, blocksize);
|
|---|
| 497 | sp->prev = pst->top;
|
|---|
| 498 | pst->fragmentation += pst->avail;
|
|---|
| 499 | pst->topsize = blocksize - offsetof(struct stack_block, space);
|
|---|
| 500 | pst->avail = blocksize - offsetof(struct stack_block, space);
|
|---|
| 501 | pst->nextbyte = sp->space;
|
|---|
| 502 | pst->top = sp;
|
|---|
| 503 | pst->blocks += 1;
|
|---|
| 504 | INTON;
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | /**
|
|---|
| 508 | * Tries to grow the current stack block to hold a minimum of @a nbytes,
|
|---|
| 509 | * will allocate a new block and copy over pending string bytes if that's not
|
|---|
| 510 | * possible.
|
|---|
| 511 | */
|
|---|
| 512 | static void pstgrowblock(shinstance *psh, pstack_block *pst, size_t nbytes, size_t tocopy)
|
|---|
| 513 | {
|
|---|
| 514 | struct stack_block *top = pst->top;
|
|---|
| 515 | size_t blocksize;
|
|---|
| 516 |
|
|---|
| 517 | assert(pst->avail < nbytes); /* only called when we need more space */
|
|---|
| 518 | assert(tocopy <= pst->avail);
|
|---|
| 519 |
|
|---|
| 520 | /* Double the size used thus far and add some fudge and alignment. Make
|
|---|
| 521 | sure to at least allocate MINSIZE. */
|
|---|
| 522 | blocksize = K_MAX(K_ALIGN_Z(pst->avail * 2 + 100 + offsetof(struct stack_block, space), 64), MINSIZE);
|
|---|
| 523 |
|
|---|
| 524 | /* If that isn't sufficient, do request size w/ some fudge and alignment. */
|
|---|
| 525 | if (blocksize < nbytes + offsetof(struct stack_block, space))
|
|---|
| 526 | blocksize = K_ALIGN_Z(nbytes + offsetof(struct stack_block, space) + 100, 1024);
|
|---|
| 527 |
|
|---|
| 528 | /*
|
|---|
| 529 | * Reallocate the current stack node if we can.
|
|---|
| 530 | */
|
|---|
| 531 | if ( pst->nextbyte == &top->space[0] /* can't have anything else in the block */
|
|---|
| 532 | && top->prev != NULL /* first block is embedded in pst and cannot be reallocated */ ) {
|
|---|
| 533 | top = (struct stack_block *)ckrealloc(psh, top, blocksize);
|
|---|
| 534 | pst->top = top;
|
|---|
| 535 | pst->topsize = blocksize - offsetof(struct stack_block, space);
|
|---|
| 536 | pst->avail = blocksize - offsetof(struct stack_block, space);
|
|---|
| 537 | pst->nextbyte = top->space;
|
|---|
| 538 | }
|
|---|
| 539 | /*
|
|---|
| 540 | * Otherwise allocate a new node and copy over the avail bytes
|
|---|
| 541 | * from the old one.
|
|---|
| 542 | */
|
|---|
| 543 | else {
|
|---|
| 544 | char const * const copysrc = pst->nextbyte;
|
|---|
| 545 | pstallocnewblock(psh, pst, nbytes);
|
|---|
| 546 | assert(pst->avail >= nbytes);
|
|---|
| 547 | assert(pst->avail >= tocopy);
|
|---|
| 548 | memcpy(pst->nextbyte, copysrc, tocopy);
|
|---|
| 549 | }
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | K_INLINE void *pstallocint(shinstance *psh, pstack_block *pst, size_t nbytes)
|
|---|
| 553 | {
|
|---|
| 554 | void *ret;
|
|---|
| 555 |
|
|---|
| 556 | /*
|
|---|
| 557 | * Align the size and make sure we've got sufficient bytes available:
|
|---|
| 558 | */
|
|---|
| 559 | nbytes = SHELL_ALIGN(nbytes);
|
|---|
| 560 | if (pst->avail >= nbytes && (ssize_t)pst->avail >= 0) { /* likely*/ }
|
|---|
| 561 | else pstallocnewblock(psh, pst, nbytes);
|
|---|
| 562 |
|
|---|
| 563 | /*
|
|---|
| 564 | * Carve out the return block.
|
|---|
| 565 | */
|
|---|
| 566 | ret = pst->nextbyte;
|
|---|
| 567 | pst->nextbyte += nbytes;
|
|---|
| 568 | pst->avail -= nbytes;
|
|---|
| 569 | pst->bytesalloced += nbytes;
|
|---|
| 570 | pst->allocations += 1;
|
|---|
| 571 | return ret;
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | #endif KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 | void *pstalloc(struct shinstance *psh, size_t nbytes)
|
|---|
| 578 | {
|
|---|
| 579 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 580 | return pstallocint(psh, psh->curpstack, nbytes);
|
|---|
| 581 | #else
|
|---|
| 582 | return stalloc(psh, nbytes);
|
|---|
| 583 | #endif
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | union node *pstallocnode(struct shinstance *psh, size_t nbytes)
|
|---|
| 587 | {
|
|---|
| 588 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 589 | pstack_block * const pst = psh->curpstack;
|
|---|
| 590 | union node * const ret = (union node *)pstallocint(psh, pst, nbytes);
|
|---|
| 591 | pst->nodesalloced++;
|
|---|
| 592 | ret->pblock = pst;
|
|---|
| 593 | return ret;
|
|---|
| 594 | #else
|
|---|
| 595 | return (union node *)pstalloc(psh, nbytes);
|
|---|
| 596 | #endif
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | struct nodelist *pstalloclist(struct shinstance *psh)
|
|---|
| 600 | {
|
|---|
| 601 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 602 | pstack_block *pst = psh->curpstack;
|
|---|
| 603 | pst->entriesalloced++;
|
|---|
| 604 | return (struct nodelist *)pstallocint(psh, pst, sizeof(struct nodelist));
|
|---|
| 605 | #endif
|
|---|
| 606 | return (struct nodelist *)pstalloc(psh, sizeof(struct nodelist));
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | char *pstsavestr(struct shinstance *psh, const char *str)
|
|---|
| 610 | {
|
|---|
| 611 | if (str) {
|
|---|
| 612 | size_t const nbytes = strlen(str) + 1;
|
|---|
| 613 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 614 | pstack_block *pst = psh->curpstack;
|
|---|
| 615 | pst->strbytesalloced += SHELL_ALIGN(nbytes);
|
|---|
| 616 | return (char *)memcpy(pstallocint(psh, pst, nbytes), str, nbytes);
|
|---|
| 617 | #else
|
|---|
| 618 | return (char *)memcpy(pstalloc(psh, nbytes), str, nbytes);
|
|---|
| 619 | #endif
|
|---|
| 620 | }
|
|---|
| 621 | return NULL;
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | char *pstmakestrspace(struct shinstance *psh, size_t minbytes, char *end)
|
|---|
| 625 | {
|
|---|
| 626 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 627 | pstack_block *pst = psh->curpstack;
|
|---|
| 628 | size_t const len = end - pst->nextbyte;
|
|---|
| 629 |
|
|---|
| 630 | assert(pst->avail - pst->strleft == len);
|
|---|
| 631 | TRACE2((psh, "pstmakestrspace: len=%u minbytes=%u (=> %u)\n", len, minbytes, len + minbytes));
|
|---|
| 632 |
|
|---|
| 633 | pstgrowblock(psh, pst, minbytes + len, len);
|
|---|
| 634 |
|
|---|
| 635 | pst->strleft = pst->avail - len;
|
|---|
| 636 | return pst->nextbyte + len;
|
|---|
| 637 |
|
|---|
| 638 | #else
|
|---|
| 639 | size_t const len = end - stackblock(psh);
|
|---|
| 640 |
|
|---|
| 641 | assert(stackblocksize(psh) - psh->sstrnleft == len);
|
|---|
| 642 | TRACE2((psh, "pstmakestrspace: len=%u minbytes=%u (=> %u)\n", len, minbytes, len + minbytes));
|
|---|
| 643 |
|
|---|
| 644 | minbytes += len;
|
|---|
| 645 | while (stackblocksize(psh) < minbytes)
|
|---|
| 646 | growstackblock(psh);
|
|---|
| 647 |
|
|---|
| 648 | psh->sstrnleft = (int)(stackblocksize(psh) - len);
|
|---|
| 649 | return (char *)stackblock(psh) + len;
|
|---|
| 650 | #endif
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | /* PSTPUTC helper */
|
|---|
| 654 | char *pstputcgrow(shinstance *psh, char *end, char c)
|
|---|
| 655 | {
|
|---|
| 656 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 657 | pstack_block *pst = psh->curpstack;
|
|---|
| 658 | pst->strleft++; /* PSTPUTC() already incremented it. */
|
|---|
| 659 | end = pstmakestrspace(psh, 1, end);
|
|---|
| 660 | assert(pst->strleft > 0);
|
|---|
| 661 | pst->strleft--;
|
|---|
| 662 | #else
|
|---|
| 663 | psh->sstrnleft++; /* PSTPUTC() already incremented it. */
|
|---|
| 664 | end = pstmakestrspace(psh, 1, end);
|
|---|
| 665 | assert(psh->sstrnleft > 0);
|
|---|
| 666 | psh->sstrnleft--;
|
|---|
| 667 | #endif
|
|---|
| 668 | *end++ = c;
|
|---|
| 669 | return end;
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 | char *pstgrabstr(struct shinstance *psh, char *end)
|
|---|
| 674 | {
|
|---|
| 675 | #ifdef KASH_SEPARATE_PARSER_ALLOCATOR
|
|---|
| 676 | pstack_block *pst = psh->curpstack;
|
|---|
| 677 | char * const pstart = pst->nextbyte;
|
|---|
| 678 | size_t nbytes = (size_t)(end - pstart);
|
|---|
| 679 |
|
|---|
| 680 | assert((uintptr_t)end > (uintptr_t)pstart);
|
|---|
| 681 | assert(end[-1] == '\0');
|
|---|
| 682 | assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
|
|---|
| 683 | assert(pst->avail - pst->strleft >= nbytes);
|
|---|
| 684 |
|
|---|
| 685 | nbytes = SHELL_ALIGN(nbytes); /** @todo don't align strings, align the other allocations. */
|
|---|
| 686 | pst->nextbyte += nbytes;
|
|---|
| 687 | pst->avail -= nbytes;
|
|---|
| 688 | pst->strbytesalloced += nbytes;
|
|---|
| 689 |
|
|---|
| 690 | return pstart;
|
|---|
| 691 |
|
|---|
| 692 | #else
|
|---|
| 693 | char * const pstart = stackblock(psh);
|
|---|
| 694 | size_t nbytes = (size_t)(end - pstart);
|
|---|
| 695 |
|
|---|
| 696 | assert((uintptr_t)end > (uintptr_t)pstart);
|
|---|
| 697 | assert(end[-1] == '\0');
|
|---|
| 698 | assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
|
|---|
| 699 | assert(stackblocksize(psh) - psh->sstrnleft >= nbytes);
|
|---|
| 700 |
|
|---|
| 701 | nbytes = SHELL_ALIGN(nbytes); /** @todo don't align strings, align the other allocations. */
|
|---|
| 702 | psh->stacknxt += nbytes;
|
|---|
| 703 | psh->stacknleft -= (int)nbytes;
|
|---|
| 704 |
|
|---|
| 705 | return pstart;
|
|---|
| 706 | #endif
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|