source: trunk/src/kash/memalloc.c@ 3457

Last change on this file since 3457 was 3457, checked in by bird, 5 years ago

kash: New parser allocator for non-forked-mode.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 17.9 KB
Line 
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
37static 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
60pointer
61ckmalloc(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
76pointer
77ckrealloc(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
90char *
91savestr(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
126pointer
127stalloc(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
154char *
155stsavestr(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
166void
167stunalloc(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
179void
180setstackmark(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
193void
194popstackmark(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 assert(mark->pstacksize <= psh->pstacksize);
210 while (mark->pstacksize < psh->pstacksize) {
211 unsigned idx = --psh->pstacksize;
212 pstack_block *psk = psh->pstack[idx];
213 psh->pstack[idx] = NULL;
214 if (psh->curpstack == psk)
215 psh->curpstack = idx > 0 ? psh->pstack[idx - 1] : NULL;
216 pstackrelease(psh, psk);
217 }
218
219# ifndef NDEBUG
220 if (psh->curpstack) {
221 unsigned i;
222 for (i = 0; i < psh->pstacksize; i++)
223 if (psh->curpstack == psh->pstack[i])
224 break;
225 assert(i < psh->pstacksize);
226 }
227# endif
228#endif
229 INTON;
230}
231
232
233/*
234 * When the parser reads in a string, it wants to stick the string on the
235 * stack and only adjust the stack pointer when it knows how big the
236 * string is. Stackblock (defined in stack.h) returns a pointer to a block
237 * of space on top of the stack and stackblocklen returns the length of
238 * this block. Growstackblock will grow this space by at least one byte,
239 * possibly moving it (like realloc). Grabstackblock actually allocates the
240 * part of the block that has been used.
241 */
242
243void
244growstackblock(shinstance *psh)
245{
246 int newlen = SHELL_ALIGN(psh->stacknleft * 2 + 100);
247
248 if (psh->stacknxt == psh->stackp->space && psh->stackp != &psh->stackbase) {
249 struct stack_block *oldstackp;
250 struct stackmark *xmark;
251 struct stack_block *sp;
252
253 INTOFF;
254 oldstackp = psh->stackp;
255 sp = psh->stackp;
256 psh->stackp = sp->prev;
257 sp = ckrealloc(psh, (pointer)sp,
258 sizeof(struct stack_block) - MINSIZE + newlen);
259 sp->prev = psh->stackp;
260 psh->stackp = sp;
261 psh->stacknxt = sp->space;
262 psh->stacknleft = newlen;
263
264 /*
265 * Stack marks pointing to the start of the old block
266 * must be relocated to point to the new block
267 */
268 xmark = psh->markp;
269 while (xmark != NULL && xmark->stackp == oldstackp) {
270 xmark->stackp = psh->stackp;
271 xmark->stacknxt = psh->stacknxt;
272 xmark->stacknleft = psh->stacknleft;
273 xmark = xmark->marknext;
274 }
275 INTON;
276 } else {
277 char *oldspace = psh->stacknxt;
278 int oldlen = psh->stacknleft;
279 char *p = stalloc(psh, newlen);
280
281 (void)memcpy(p, oldspace, oldlen);
282 psh->stacknxt = p; /* free the space */
283 psh->stacknleft += newlen; /* we just allocated */
284 }
285}
286
287void
288grabstackblock(shinstance *psh, int len)
289{
290 len = SHELL_ALIGN(len);
291 psh->stacknxt += len;
292 psh->stacknleft -= len;
293}
294
295/*
296 * The following routines are somewhat easier to use than the above.
297 * The user declares a variable of type STACKSTR, which may be declared
298 * to be a register. The macro STARTSTACKSTR initializes things. Then
299 * the user uses the macro STPUTC to add characters to the string. In
300 * effect, STPUTC(psh, c, p) is the same as *p++ = c except that the stack is
301 * grown as necessary. When the user is done, she can just leave the
302 * string there and refer to it using stackblock(psh). Or she can allocate
303 * the space for it using grabstackstr(). If it is necessary to allow
304 * someone else to use the stack temporarily and then continue to grow
305 * the string, the user should use grabstack to allocate the space, and
306 * then call ungrabstr(p) to return to the previous mode of operation.
307 *
308 * USTPUTC is like STPUTC except that it doesn't check for overflow.
309 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
310 * is space for at least one character.
311 */
312
313char *
314growstackstr(shinstance *psh)
315{
316 int len = stackblocksize(psh);
317 if (psh->herefd >= 0 && len >= 1024) {
318 xwrite(psh, psh->herefd, stackblock(psh), len);
319 psh->sstrnleft = len - 1;
320 return stackblock(psh);
321 }
322 growstackblock(psh);
323 psh->sstrnleft = stackblocksize(psh) - len - 1;
324 return stackblock(psh) + len;
325}
326
327/*
328 * Called from CHECKSTRSPACE.
329 */
330
331char *
332makestrspace(shinstance *psh)
333{
334 int len = stackblocksize(psh) - psh->sstrnleft;
335 growstackblock(psh);
336 psh->sstrnleft = stackblocksize(psh) - len;
337 return stackblock(psh) + len;
338}
339
340
341/*
342 * Got better control having a dedicated function for this.
343 *
344 * Was: #define grabstackstr(psh, p) stalloc((psh), stackblocksize(psh) - (psh)->sstrnleft)
345 */
346char *
347grabstackstr(shinstance *psh, char *end)
348{
349 char * const pstart = stackblock(psh);
350 size_t nbytes = (size_t)(end - pstart);
351
352 assert((uintptr_t)end >= (uintptr_t)pstart);
353 /*assert(end[-1] == '\0'); - not if it's followed by ungrabstrackstr(), sigh. */
354 assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
355 assert(stackblocksize(psh) - psh->sstrnleft >= nbytes);
356
357 nbytes = SHELL_ALIGN(nbytes);
358 psh->stacknxt += nbytes;
359 psh->stacknleft -= (int)nbytes;
360 assert(psh->stacknleft >= 0);
361
362 return pstart;
363}
364
365void
366ungrabstackstr(shinstance *psh, char *s, char *p)
367{
368 assert((size_t)(psh->stacknxt - p) <= SHELL_SIZE);
369 assert((uintptr_t)s >= (uintptr_t)&psh->stackp->space[0]);
370 assert((uintptr_t)p >= (uintptr_t)s);
371
372 psh->stacknleft += (int)(psh->stacknxt - s);
373 psh->stacknxt = s;
374 psh->sstrnleft = (int)(psh->stacknleft - (p - s));
375
376}
377
378
379/*
380 * Parser stack allocator.
381 */
382#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
383
384unsigned pstackretain(pstack_block *pst)
385{
386 unsigned refs = sh_atomic_inc(&pst->refs);
387 assert(refs > 1);
388 assert(refs < 256 /* bogus, but useful */);
389 return refs;
390}
391
392unsigned pstackrelease(shinstance *psh, pstack_block *pst)
393{
394 unsigned refs;
395 if (pst) {
396 refs = sh_atomic_dec(&pst->refs);
397 if (refs == 0) {
398 shinstance * const psh = shthread_get_shell();
399 struct stack_block *top;
400 while ((top = pst->top) != &pst->first)
401 {
402 pst->top = top->prev;
403 assert(pst->top);
404 top->prev = NULL;
405 sh_free(psh, top);
406 }
407 pst->nextbyte = NULL;
408 pst->top = NULL;
409 sh_free(psh, pst);
410 }
411 } else
412 refs = 0;
413 return refs;
414}
415
416pstack_block *pstackpush(shinstance *psh)
417{
418 size_t const blocksize = offsetof(pstack_block, first.space) + MINSIZE;
419 pstack_block *pst;
420 unsigned i;
421
422 INTOFF;
423
424 /*
425 * Allocate and initialize it.
426 */
427 pst = (pstack_block *)ckmalloc(psh, blocksize);
428 pst->nextbyte = &pst->first.space[0];
429 pst->avail = blocksize - offsetof(pstack_block, first.space);
430 pst->topsize = blocksize - offsetof(pstack_block, first.space);
431 pst->strleft = 0;
432 pst->top = &pst->first;
433 pst->allocations = 0;
434 pst->bytesalloced = 0;
435 pst->nodesalloced = 0;
436 pst->entriesalloced = 0;
437 pst->strbytesalloced = 0;
438 pst->blocks = 0;
439 pst->fragmentation = 0;
440 pst->refs = 1;
441 pst->padding = 42;
442 pst->first.prev = NULL;
443
444 /*
445 * Push it onto the stack.
446 */
447 i = psh->pstacksize;
448 if (i + 1 < psh->pstackalloced) {
449 /* likely, except for the first time */
450 } else {
451 psh->pstack = (pstack_block **)ckrealloc(psh, psh->pstack, sizeof(psh->pstack[0]) * (i + 32));
452 memset(&psh->pstack[i], 0, sizeof(psh->pstack[0]) * 32);
453 }
454 psh->pstack[i] = pst;
455 psh->pstacksize = i + 1;
456 psh->curpstack = pst;
457
458 INTON;
459 return pst;
460}
461
462/**
463 * Allocates and pushes a new block onto the stack, min payload size @a nbytes.
464 */
465static void pstallocnewblock(shinstance *psh, pstack_block *pst, size_t nbytes)
466{
467 /* Allocate a new stack node. */
468 struct stack_block *sp;
469 size_t const blocksize = nbytes <= MINSIZE
470 ? offsetof(struct stack_block, space) + MINSIZE
471 : K_ALIGN_Z(nbytes + offsetof(struct stack_block, space), 1024);
472
473 INTOFF;
474 sp = ckmalloc(psh, blocksize);
475 sp->prev = pst->top;
476 pst->fragmentation += pst->avail;
477 pst->topsize = blocksize - offsetof(struct stack_block, space);
478 pst->avail = blocksize - offsetof(struct stack_block, space);
479 pst->nextbyte = sp->space;
480 pst->top = sp;
481 pst->blocks += 1;
482 INTON;
483}
484
485/**
486 * Tries to grow the current stack block to hold a minimum of @a nbytes,
487 * will allocate a new block and copy over pending string bytes if that's not
488 * possible.
489 */
490static void pstgrowblock(shinstance *psh, pstack_block *pst, size_t nbytes, size_t tocopy)
491{
492 struct stack_block *top = pst->top;
493 size_t blocksize;
494
495 assert(pst->avail < nbytes); /* only called when we need more space */
496 assert(tocopy <= pst->avail);
497
498 /* Double the size used thus far and add some fudge and alignment. Make
499 sure to at least allocate MINSIZE. */
500 blocksize = K_MAX(K_ALIGN_Z(pst->avail * 2 + 100 + offsetof(struct stack_block, space), 64), MINSIZE);
501
502 /* If that isn't sufficient, do request size w/ some fudge and alignment. */
503 if (blocksize < nbytes + offsetof(struct stack_block, space))
504 blocksize = K_ALIGN_Z(nbytes + offsetof(struct stack_block, space) + 100, 1024);
505
506 /*
507 * Reallocate the current stack node if we can.
508 */
509 if ( pst->nextbyte == &top->space[0] /* can't have anything else in the block */
510 && top->prev != NULL /* first block is embedded in pst and cannot be reallocated */ ) {
511 top = (struct stack_block *)ckrealloc(psh, top, blocksize);
512 pst->top = top;
513 pst->topsize = blocksize - offsetof(struct stack_block, space);
514 pst->avail = blocksize - offsetof(struct stack_block, space);
515 pst->nextbyte = top->space;
516 }
517 /*
518 * Otherwise allocate a new node and copy over the avail bytes
519 * from the old one.
520 */
521 else {
522 char const * const copysrc = pst->nextbyte;
523 pstallocnewblock(psh, pst, nbytes);
524 assert(pst->avail >= nbytes);
525 assert(pst->avail >= tocopy);
526 memcpy(pst->nextbyte, copysrc, tocopy);
527 }
528}
529
530K_INLINE void *pstallocint(shinstance *psh, pstack_block *pst, size_t nbytes)
531{
532 void *ret;
533
534 /*
535 * Align the size and make sure we've got sufficient bytes available:
536 */
537 nbytes = SHELL_ALIGN(nbytes);
538 if (pst->avail >= nbytes && (ssize_t)pst->avail >= 0) { /* likely*/ }
539 else pstallocnewblock(psh, pst, nbytes);
540
541 /*
542 * Carve out the return block.
543 */
544 ret = pst->nextbyte;
545 pst->nextbyte += nbytes;
546 pst->avail -= nbytes;
547 pst->bytesalloced += nbytes;
548 pst->allocations += 1;
549 return ret;
550}
551
552#endif KASH_SEPARATE_PARSER_ALLOCATOR
553
554
555void *pstalloc(struct shinstance *psh, size_t nbytes)
556{
557#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
558 return pstallocint(psh, psh->curpstack, nbytes);
559#else
560 return stalloc(psh, nbytes);
561#endif
562}
563
564union node *pstallocnode(struct shinstance *psh, size_t nbytes)
565{
566#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
567 pstack_block *pst = psh->curpstack;
568 pst->nodesalloced++;
569 return (union node *)pstallocint(psh, pst, nbytes);
570#else
571 return (union node *)pstalloc(psh, nbytes);
572#endif
573}
574
575struct nodelist *pstalloclist(struct shinstance *psh)
576{
577#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
578 pstack_block *pst = psh->curpstack;
579 pst->entriesalloced++;
580 return (struct nodelist *)pstallocint(psh, pst, sizeof(struct nodelist));
581#endif
582 return (struct nodelist *)pstalloc(psh, sizeof(struct nodelist));
583}
584
585char *pstsavestr(struct shinstance *psh, const char *str)
586{
587 if (str) {
588 size_t const nbytes = strlen(str) + 1;
589#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
590 pstack_block *pst = psh->curpstack;
591 pst->strbytesalloced += SHELL_ALIGN(nbytes);
592 return (char *)memcpy(pstallocint(psh, pst, nbytes), str, nbytes);
593#else
594 return (char *)memcpy(pstalloc(psh, nbytes), str, nbytes);
595#endif
596 }
597 return NULL;
598}
599
600char *pstmakestrspace(struct shinstance *psh, size_t minbytes, char *end)
601{
602#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
603 pstack_block *pst = psh->curpstack;
604 size_t const len = end - pst->nextbyte;
605
606 assert(pst->avail - pst->strleft == len);
607 TRACE2((psh, "pstmakestrspace: len=%u minbytes=%u (=> %u)\n", len, minbytes, len + minbytes));
608
609 pstgrowblock(psh, pst, minbytes + len, len);
610
611 pst->strleft = pst->avail - len;
612 return pst->nextbyte + len;
613
614#else
615 size_t const len = end - stackblock(psh);
616
617 assert(stackblocksize(psh) - psh->sstrnleft == len);
618 TRACE2((psh, "pstmakestrspace: len=%u minbytes=%u (=> %u)\n", len, minbytes, len + minbytes));
619
620 minbytes += len;
621 while (stackblocksize(psh) < minbytes)
622 growstackblock(psh);
623
624 psh->sstrnleft = (int)(stackblocksize(psh) - len);
625 return (char *)stackblock(psh) + len;
626#endif
627}
628
629/* PSTPUTC helper */
630char *pstputcgrow(shinstance *psh, char *end, char c)
631{
632#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
633 pstack_block *pst = psh->curpstack;
634 pst->strleft++; /* PSTPUTC() already incremented it. */
635 end = pstmakestrspace(psh, 1, end);
636 assert(pst->strleft > 0);
637 pst->strleft--;
638#else
639 psh->sstrnleft++; /* PSTPUTC() already incremented it. */
640 end = pstmakestrspace(psh, 1, end);
641 assert(psh->sstrnleft > 0);
642 psh->sstrnleft--;
643#endif
644 *end++ = c;
645 return end;
646}
647
648
649char *pstgrabstr(struct shinstance *psh, char *end)
650{
651#ifdef KASH_SEPARATE_PARSER_ALLOCATOR
652 pstack_block *pst = psh->curpstack;
653 char * const pstart = pst->nextbyte;
654 size_t nbytes = (size_t)(end - pstart);
655
656 assert((uintptr_t)end > (uintptr_t)pstart);
657 assert(end[-1] == '\0');
658 assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
659 assert(pst->avail - pst->strleft >= nbytes);
660
661 nbytes = SHELL_ALIGN(nbytes); /** @todo don't align strings, align the other allocations. */
662 pst->nextbyte += nbytes;
663 pst->avail -= nbytes;
664 pst->strbytesalloced += nbytes;
665
666 return pstart;
667
668#else
669 char * const pstart = stackblock(psh);
670 size_t nbytes = (size_t)(end - pstart);
671
672 assert((uintptr_t)end > (uintptr_t)pstart);
673 assert(end[-1] == '\0');
674 assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
675 assert(stackblocksize(psh) - psh->sstrnleft >= nbytes);
676
677 nbytes = SHELL_ALIGN(nbytes); /** @todo don't align strings, align the other allocations. */
678 psh->stacknxt += nbytes;
679 psh->stacknleft -= (int)nbytes;
680
681 return pstart;
682#endif
683}
684
Note: See TracBrowser for help on using the repository browser.