ash: get rid of two global data variables

function                                             old     new   delta
calcsize                                             126     147     +21
funcstring_end                                         -       4      +4
sizenodelist                                          28      24      -4
funcstringsize                                         4       -      -4
funcstring                                             4       -      -4
funcblocksize                                          4       -      -4
nodeckstrdup                                          48      39      -9
evaltree                                             828     788     -40
------------------------------------------------------------------------------
(add/remove: 1/3 grow/shrink: 1/4 up/down: 25/-105)           Total: -40 bytes
   text	   data	    bss	    dec	    hex	filename
 943376	    916	  14292	 958584	  ea078	busybox_old
 943344	    916	  14284	 958544	  ea050	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2016-10-07 04:05:15 +02:00
parent f37e1155aa
commit 4c438b5ba4

View File

@ -8159,10 +8159,10 @@ commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
#endif #endif
static int funcblocksize; /* size of structures in function */ /*static int funcblocksize; // size of structures in function */
static int funcstringsize; /* size of strings in node */ /*static int funcstringsize; // size of strings in node */
static void *funcblock; /* block to allocate function from */ static void *funcblock; /* block to allocate function from */
static char *funcstring; /* block to allocate strings from */ static char *funcstring_end; /* end of block to allocate strings from */
/* flags in argument to evaltree */ /* flags in argument to evaltree */
#define EV_EXIT 01 /* exit after evaluating tree */ #define EV_EXIT 01 /* exit after evaluating tree */
@ -8200,71 +8200,72 @@ static const uint8_t nodesize[N_NUMBER] ALIGN1 = {
[NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)),
}; };
static void calcsize(union node *n); static int calcsize(int funcblocksize, union node *n);
static void static int
sizenodelist(struct nodelist *lp) sizenodelist(int funcblocksize, struct nodelist *lp)
{ {
while (lp) { while (lp) {
funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
calcsize(lp->n); funcblocksize = calcsize(funcblocksize, lp->n);
lp = lp->next; lp = lp->next;
} }
return funcblocksize;
} }
static void static int
calcsize(union node *n) calcsize(int funcblocksize, union node *n)
{ {
if (n == NULL) if (n == NULL)
return; return funcblocksize;
funcblocksize += nodesize[n->type]; funcblocksize += nodesize[n->type];
switch (n->type) { switch (n->type) {
case NCMD: case NCMD:
calcsize(n->ncmd.redirect); funcblocksize = calcsize(funcblocksize, n->ncmd.redirect);
calcsize(n->ncmd.args); funcblocksize = calcsize(funcblocksize, n->ncmd.args);
calcsize(n->ncmd.assign); funcblocksize = calcsize(funcblocksize, n->ncmd.assign);
break; break;
case NPIPE: case NPIPE:
sizenodelist(n->npipe.cmdlist); funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist);
break; break;
case NREDIR: case NREDIR:
case NBACKGND: case NBACKGND:
case NSUBSHELL: case NSUBSHELL:
calcsize(n->nredir.redirect); funcblocksize = calcsize(funcblocksize, n->nredir.redirect);
calcsize(n->nredir.n); funcblocksize = calcsize(funcblocksize, n->nredir.n);
break; break;
case NAND: case NAND:
case NOR: case NOR:
case NSEMI: case NSEMI:
case NWHILE: case NWHILE:
case NUNTIL: case NUNTIL:
calcsize(n->nbinary.ch2); funcblocksize = calcsize(funcblocksize, n->nbinary.ch2);
calcsize(n->nbinary.ch1); funcblocksize = calcsize(funcblocksize, n->nbinary.ch1);
break; break;
case NIF: case NIF:
calcsize(n->nif.elsepart); funcblocksize = calcsize(funcblocksize, n->nif.elsepart);
calcsize(n->nif.ifpart); funcblocksize = calcsize(funcblocksize, n->nif.ifpart);
calcsize(n->nif.test); funcblocksize = calcsize(funcblocksize, n->nif.test);
break; break;
case NFOR: case NFOR:
funcstringsize += strlen(n->nfor.var) + 1; funcblocksize += strlen(n->nfor.var) + 1; /* was funcstringsize += ... */
calcsize(n->nfor.body); funcblocksize = calcsize(funcblocksize, n->nfor.body);
calcsize(n->nfor.args); funcblocksize = calcsize(funcblocksize, n->nfor.args);
break; break;
case NCASE: case NCASE:
calcsize(n->ncase.cases); funcblocksize = calcsize(funcblocksize, n->ncase.cases);
calcsize(n->ncase.expr); funcblocksize = calcsize(funcblocksize, n->ncase.expr);
break; break;
case NCLIST: case NCLIST:
calcsize(n->nclist.body); funcblocksize = calcsize(funcblocksize, n->nclist.body);
calcsize(n->nclist.pattern); funcblocksize = calcsize(funcblocksize, n->nclist.pattern);
calcsize(n->nclist.next); funcblocksize = calcsize(funcblocksize, n->nclist.next);
break; break;
case NDEFUN: case NDEFUN:
case NARG: case NARG:
sizenodelist(n->narg.backquote); funcblocksize = sizenodelist(funcblocksize, n->narg.backquote);
funcstringsize += strlen(n->narg.text) + 1; funcblocksize += strlen(n->narg.text) + 1; /* was funcstringsize += ... */
calcsize(n->narg.next); funcblocksize = calcsize(funcblocksize, n->narg.next);
break; break;
case NTO: case NTO:
#if ENABLE_ASH_BASH_COMPAT #if ENABLE_ASH_BASH_COMPAT
@ -8274,33 +8275,31 @@ calcsize(union node *n)
case NFROM: case NFROM:
case NFROMTO: case NFROMTO:
case NAPPEND: case NAPPEND:
calcsize(n->nfile.fname); funcblocksize = calcsize(funcblocksize, n->nfile.fname);
calcsize(n->nfile.next); funcblocksize = calcsize(funcblocksize, n->nfile.next);
break; break;
case NTOFD: case NTOFD:
case NFROMFD: case NFROMFD:
calcsize(n->ndup.vname); funcblocksize = calcsize(funcblocksize, n->ndup.vname);
calcsize(n->ndup.next); funcblocksize = calcsize(funcblocksize, n->ndup.next);
break; break;
case NHERE: case NHERE:
case NXHERE: case NXHERE:
calcsize(n->nhere.doc); funcblocksize = calcsize(funcblocksize, n->nhere.doc);
calcsize(n->nhere.next); funcblocksize = calcsize(funcblocksize, n->nhere.next);
break; break;
case NNOT: case NNOT:
calcsize(n->nnot.com); funcblocksize = calcsize(funcblocksize, n->nnot.com);
break; break;
}; };
return funcblocksize;
} }
static char * static char *
nodeckstrdup(char *s) nodeckstrdup(char *s)
{ {
char *rtn = funcstring; funcstring_end -= strlen(s) + 1;
return strcpy(funcstring_end, s);
strcpy(funcstring, s);
funcstring += strlen(s) + 1;
return rtn;
} }
static union node *copynode(union node *); static union node *copynode(union node *);
@ -8424,15 +8423,13 @@ copyfunc(union node *n)
struct funcnode *f; struct funcnode *f;
size_t blocksize; size_t blocksize;
funcblocksize = offsetof(struct funcnode, n); /*funcstringsize = 0;*/
funcstringsize = 0; blocksize = offsetof(struct funcnode, n) + calcsize(0, n);
calcsize(n); f = ckzalloc(blocksize /* + funcstringsize */);
blocksize = funcblocksize;
f = ckmalloc(blocksize + funcstringsize);
funcblock = (char *) f + offsetof(struct funcnode, n); funcblock = (char *) f + offsetof(struct funcnode, n);
funcstring = (char *) f + blocksize; funcstring_end = (char *) f + blocksize;
copynode(n); copynode(n);
f->count = 0; /* f->count = 0; - ckzalloc did it */
return f; return f;
} }