Ignore:
Timestamp:
Oct 6, 2007, 11:19:19 PM (18 years ago)
Author:
bird
Message:

moving globals into shinstance...

File:
1 edited

Legend:

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

    r809 r1198  
    5959
    6060pointer
    61 ckmalloc(int nbytes)
     61ckmalloc(size_t nbytes)
    6262{
    6363        pointer p;
     
    6565        p = malloc(nbytes);
    6666        if (p == NULL)
    67                 error("Out of space");
     67                error(NULL, "Out of space");
    6868        return p;
    6969}
     
    7575
    7676pointer
    77 ckrealloc(pointer p, int nbytes)
     77ckrealloc(pointer p, size_t nbytes)
    7878{
    7979        p = realloc(p, nbytes);
    8080        if (p == NULL)
    81                 error("Out of space");
     81                error(NULL, "Out of space");
    8282        return p;
    8383}
     
    108108 */
    109109
    110 #define MINSIZE 504             /* minimum size of a block */
    111 
    112 struct stack_block {
    113         struct stack_block *prev;
    114         char space[MINSIZE];
    115 };
    116 
    117 struct stack_block stackbase;
    118 struct stack_block *stackp = &stackbase;
    119 struct stackmark *markp;
    120 char *stacknxt = stackbase.space;
    121 int stacknleft = MINSIZE;
    122 int sstrnleft;
    123 int herefd = -1;
     110//#define MINSIZE 504           /* minimum size of a block */
     111
     112//struct stack_block {
     113//      struct stack_block *prev;
     114//      char space[MINSIZE];
     115//};
     116
     117//struct stack_block stackbase;
     118//struct stack_block *stackp = &stackbase;
     119//struct stackmark *markp;
     120//char *stacknxt = stackbase.space;
     121//int stacknleft = MINSIZE;
     122//int sstrnleft;
     123//int herefd = -1;
    124124
    125125pointer
    126 stalloc(int nbytes)
     126stalloc(shinstance *psh, size_t nbytes)
    127127{
    128128        char *p;
    129129
    130130        nbytes = SHELL_ALIGN(nbytes);
    131         if (nbytes > stacknleft) {
    132                 int blocksize;
     131        if (nbytes > psh->stacknleft) {
     132                size_t blocksize;
    133133                struct stack_block *sp;
    134134
     
    138138                INTOFF;
    139139                sp = ckmalloc(sizeof(struct stack_block) - MINSIZE + blocksize);
    140                 sp->prev = stackp;
    141                 stacknxt = sp->space;
    142                 stacknleft = blocksize;
    143                 stackp = sp;
     140                sp->prev = psh->stackp;
     141                psh->stacknxt = sp->space;
     142                psh->stacknleft = (int)blocksize;
     143                psh->stackp = sp;
    144144                INTON;
    145145        }
    146         p = stacknxt;
    147         stacknxt += nbytes;
    148         stacknleft -= nbytes;
     146        p = psh->stacknxt;
     147        psh->stacknxt += nbytes;
     148        psh->stacknleft -= (int)nbytes;
    149149        return p;
    150150}
     
    152152
    153153void
    154 stunalloc(pointer p)
     154stunalloc(shinstance *psh, pointer p)
    155155{
    156156        if (p == NULL) {                /*DEBUG */
    157                 write(2, "stunalloc\n", 10);
    158                 abort();
    159         }
    160         stacknleft += stacknxt - (char *)p;
    161         stacknxt = p;
    162 }
    163 
    164 
    165 
    166 void
    167 setstackmark(struct stackmark *mark)
    168 {
    169         mark->stackp = stackp;
    170         mark->stacknxt = stacknxt;
    171         mark->stacknleft = stacknleft;
    172         mark->marknext = markp;
    173         markp = mark;
    174 }
    175 
    176 
    177 void
    178 popstackmark(struct stackmark *mark)
     157                shfile_write(&psh->fdtab, 2, "stunalloc\n", 10);
     158                sh_abort(psh);
     159        }
     160        psh->stacknleft += (int)(psh->stacknxt - (char *)p);
     161        psh->stacknxt = p;
     162}
     163
     164
     165
     166void
     167setstackmark(shinstance *psh, struct stackmark *mark)
     168{
     169        mark->stackp = psh->stackp;
     170        mark->stacknxt = psh->stacknxt;
     171        mark->stacknleft = psh->stacknleft;
     172        mark->marknext = psh->markp;
     173        psh->markp = mark;
     174}
     175
     176
     177void
     178popstackmark(shinstance *psh, struct stackmark *mark)
    179179{
    180180        struct stack_block *sp;
    181181
    182182        INTOFF;
    183         markp = mark->marknext;
    184         while (stackp != mark->stackp) {
    185                 sp = stackp;
    186                 stackp = sp->prev;
     183        psh->markp = mark->marknext;
     184        while (psh->stackp != mark->stackp) {
     185                sp = psh->stackp;
     186                psh->stackp = sp->prev;
    187187                ckfree(sp);
    188188        }
    189         stacknxt = mark->stacknxt;
    190         stacknleft = mark->stacknleft;
     189        psh->stacknxt = mark->stacknxt;
     190        psh->stacknleft = mark->stacknleft;
    191191        INTON;
    192192}
     
    204204
    205205void
    206 growstackblock(void)
    207 {
    208         int newlen = SHELL_ALIGN(stacknleft * 2 + 100);
    209 
    210         if (stacknxt == stackp->space && stackp != &stackbase) {
     206growstackblock(shinstance *psh)
     207{
     208        int newlen = SHELL_ALIGN(psh->stacknleft * 2 + 100);
     209
     210        if (psh->stacknxt == psh->stackp->space && psh->stackp != &psh->stackbase) {
    211211                struct stack_block *oldstackp;
    212212                struct stackmark *xmark;
     
    214214
    215215                INTOFF;
    216                 oldstackp = stackp;
    217                 sp = stackp;
    218                 stackp = sp->prev;
     216                oldstackp = psh->stackp;
     217                sp = psh->stackp;
     218                psh->stackp = sp->prev;
    219219                sp = ckrealloc((pointer)sp,
    220220                    sizeof(struct stack_block) - MINSIZE + newlen);
    221                 sp->prev = stackp;
    222                 stackp = sp;
    223                 stacknxt = sp->space;
    224                 stacknleft = newlen;
     221                sp->prev = psh->stackp;
     222                psh->stackp = sp;
     223                psh->stacknxt = sp->space;
     224                psh->stacknleft = newlen;
    225225
    226226                /*
     
    228228                 * must be relocated to point to the new block
    229229                 */
    230                 xmark = markp;
     230                xmark = psh->markp;
    231231                while (xmark != NULL && xmark->stackp == oldstackp) {
    232                         xmark->stackp = stackp;
    233                         xmark->stacknxt = stacknxt;
    234                         xmark->stacknleft = stacknleft;
     232                        xmark->stackp = psh->stackp;
     233                        xmark->stacknxt = psh->stacknxt;
     234                        xmark->stacknleft = psh->stacknleft;
    235235                        xmark = xmark->marknext;
    236236                }
    237237                INTON;
    238238        } else {
    239                 char *oldspace = stacknxt;
    240                 int oldlen = stacknleft;
    241                 char *p = stalloc(newlen);
     239                char *oldspace = psh->stacknxt;
     240                int oldlen = psh->stacknleft;
     241                char *p = stalloc(psh, newlen);
    242242
    243243                (void)memcpy(p, oldspace, oldlen);
    244                 stacknxt = p;                   /* free the space */
    245                 stacknleft += newlen;           /* we just allocated */
    246         }
    247 }
    248 
    249 void
    250 grabstackblock(int len)
     244                psh->stacknxt = p;                      /* free the space */
     245                psh->stacknleft += newlen;              /* we just allocated */
     246        }
     247}
     248
     249void
     250grabstackblock(shinstance *psh, int len)
    251251{
    252252        len = SHELL_ALIGN(len);
    253         stacknxt += len;
    254         stacknleft -= len;
     253        psh->stacknxt += len;
     254        psh->stacknleft -= len;
    255255}
    256256
     
    260260 * to be a register.  The macro STARTSTACKSTR initializes things.  Then
    261261 * the user uses the macro STPUTC to add characters to the string.  In
    262  * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
     262 * effect, STPUTC(psh, c, p) is the same as *p++ = c except that the stack is
    263263 * grown as necessary.  When the user is done, she can just leave the
    264  * string there and refer to it using stackblock().  Or she can allocate
     264 * string there and refer to it using stackblock(psh).  Or she can allocate
    265265 * the space for it using grabstackstr().  If it is necessary to allow
    266266 * someone else to use the stack temporarily and then continue to grow
     
    274274
    275275char *
    276 growstackstr(void)
    277 {
    278         int len = stackblocksize();
    279         if (herefd >= 0 && len >= 1024) {
    280                 xwrite(herefd, stackblock(), len);
    281                 sstrnleft = len - 1;
    282                 return stackblock();
    283         }
    284         growstackblock();
    285         sstrnleft = stackblocksize() - len - 1;
    286         return stackblock() + len;
     276growstackstr(shinstance *psh)
     277{
     278        int len = stackblocksize(psh);
     279        if (psh->herefd >= 0 && len >= 1024) {
     280                xwrite(psh, psh->herefd, stackblock(psh), len);
     281                psh->sstrnleft = len - 1;
     282                return stackblock(psh);
     283        }
     284        growstackblock(psh);
     285        psh->sstrnleft = stackblocksize(psh) - len - 1;
     286        return stackblock(psh) + len;
    287287}
    288288
     
    292292
    293293char *
    294 makestrspace(void)
    295 {
    296         int len = stackblocksize() - sstrnleft;
    297         growstackblock();
    298         sstrnleft = stackblocksize() - len;
    299         return stackblock() + len;
    300 }
    301 
    302 void
    303 ungrabstackstr(char *s, char *p)
    304 {
    305         stacknleft += stacknxt - s;
    306         stacknxt = s;
    307         sstrnleft = stacknleft - (p - s);
    308 
    309 }
     294makestrspace(shinstance *psh)
     295{
     296        int len = stackblocksize(psh) - psh->sstrnleft;
     297        growstackblock(psh);
     298        psh->sstrnleft = stackblocksize(psh) - len;
     299        return stackblock(psh) + len;
     300}
     301
     302void
     303ungrabstackstr(shinstance *psh, char *s, char *p)
     304{
     305        psh->stacknleft += (int)(psh->stacknxt - s);
     306        psh->stacknxt = s;
     307        psh->sstrnleft = (int)(psh->stacknleft - (p - s));
     308
     309}
Note: See TracChangeset for help on using the changeset viewer.