Ignore:
Timestamp:
Sep 14, 2020, 2:03:45 PM (5 years ago)
Author:
bird
Message:

kash: parser.c,memalloc.c/.h,expand.c: Prepared the parser for using a separate allocator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/memalloc.c

    r3437 r3456  
    4242
    4343#include <stdlib.h>
     44#include <assert.h>
    4445
    4546#include "shell.h"
     
    5051#include "mystring.h"
    5152#include "shinstance.h"
     53#include "nodes.h"
    5254
    5355/*
     
    310312}
    311313
     314
     315/*
     316 * Got better control having a dedicated function for this.
     317 *
     318 * Was: #define grabstackstr(psh, p)   stalloc((psh), stackblocksize(psh) - (psh)->sstrnleft)
     319 */
     320char *
     321grabstackstr(shinstance *psh, char *end)
     322{
     323        char * const pstart = stackblock(psh);
     324        size_t nbytes = (size_t)(end - pstart);
     325
     326        assert((uintptr_t)end >= (uintptr_t)pstart);
     327        /*assert(end[-1] == '\0'); - not if it's followed by ungrabstrackstr(), sigh. */
     328        assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
     329        assert(stackblocksize(psh) - psh->sstrnleft >= nbytes);
     330
     331        nbytes = SHELL_ALIGN(nbytes);
     332        psh->stacknxt += nbytes;
     333        psh->stacknleft -= (int)nbytes;
     334        assert(psh->stacknleft >= 0);
     335
     336        return pstart;
     337}
     338
    312339void
    313340ungrabstackstr(shinstance *psh, char *s, char *p)
    314341{
     342        assert((size_t)(psh->stacknxt - p) <= SHELL_SIZE);
     343        assert((uintptr_t)s >= (uintptr_t)&psh->stackp->space[0]);
     344        assert((uintptr_t)p >= (uintptr_t)s);
     345
    315346        psh->stacknleft += (int)(psh->stacknxt - s);
    316347        psh->stacknxt = s;
     
    318349
    319350}
     351
     352
     353/*
     354 * Parser stack allocator.
     355 */
     356void *pstalloc(struct shinstance *psh, size_t nbytes)
     357{
     358        return stalloc(psh, nbytes);
     359}
     360
     361union node *pstallocnode(struct shinstance *psh, size_t nbytes)
     362{
     363        return (union node *)pstalloc(psh, nbytes);
     364}
     365
     366struct nodelist *pstalloclist(struct shinstance *psh)
     367{
     368        return (struct nodelist *)pstalloc(psh, sizeof(struct nodelist));
     369}
     370
     371char *pstsavestr(struct shinstance *psh, const char *str)
     372{
     373        if (str) {
     374                size_t nbytes = strlen(str) + 1;
     375                return (char *)memcpy(pstalloc(psh, nbytes), str, nbytes);
     376        }
     377        return NULL;
     378}
     379
     380char *pstmakestrspace(struct shinstance *psh, size_t minbytes, char *end)
     381{
     382        size_t const len = end - stackblock(psh);
     383        assert(stackblocksize(psh) - psh->sstrnleft == len);
     384TRACE2((psh, "pstmakestrspace: len=%u minbytes=%u (=> %u)\n", len, minbytes, len + minbytes));
     385        minbytes += len;
     386        while (stackblocksize(psh) < minbytes)
     387                growstackblock(psh);
     388        psh->sstrnleft = (int)(stackblocksize(psh) - len);
     389        return (char *)stackblock(psh) + len;
     390}
     391
     392/* PSTPUTC helper */
     393char *pstputcgrow(shinstance *psh, char *end, char c)
     394{
     395        psh->sstrnleft++; /* PSTPUTC() already incremented it. */
     396        end = pstmakestrspace(psh, 1, end);
     397        assert(psh->sstrnleft > 0);
     398        psh->sstrnleft--;
     399        *end++ = c;
     400        return end;
     401}
     402
     403
     404char *pstgrabstr(struct shinstance *psh, char *end)
     405{
     406        char * const pstart = stackblock(psh);
     407        size_t nbytes = (size_t)(end - pstart);
     408
     409        assert((uintptr_t)end > (uintptr_t)pstart);
     410        assert(end[-1] == '\0');
     411        assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart);
     412        assert(stackblocksize(psh) - psh->sstrnleft >= nbytes);
     413
     414        nbytes = SHELL_ALIGN(nbytes); /** @todo don't align strings, align the other allocations. */
     415        psh->stacknxt += nbytes;
     416        psh->stacknleft -= (int)nbytes;
     417
     418        return pstart;
     419}
     420
Note: See TracChangeset for help on using the changeset viewer.