diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-29 14:33:04 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-29 14:33:04 +0200 |
commit | 21fbee2e87ddf7b47bb501b6529b63ac2b3af0bd (patch) | |
tree | 99a8f35ecfa79483086f7aff8632ac54eee5b899 | |
parent | 6872c193a935df47facf717c15a32f93b43c6bcf (diff) | |
download | busybox-w32-21fbee2e87ddf7b47bb501b6529b63ac2b3af0bd.tar.gz busybox-w32-21fbee2e87ddf7b47bb501b6529b63ac2b3af0bd.tar.bz2 busybox-w32-21fbee2e87ddf7b47bb501b6529b63ac2b3af0bd.zip |
awk: document which hashes are used at what state (parse/execute)
We can free them after they are no longer needed.
(Currently, being a NOEXEC applet is much larger waste of memory
for the case of long-running awk script).
function old new delta
awk_main 831 827 -4
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/awk.c | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/editors/awk.c b/editors/awk.c index ce860dc04..6142144bb 100644 --- a/editors/awk.c +++ b/editors/awk.c | |||
@@ -527,7 +527,10 @@ struct globals { | |||
527 | chain *seq; | 527 | chain *seq; |
528 | node *break_ptr, *continue_ptr; | 528 | node *break_ptr, *continue_ptr; |
529 | rstream *iF; | 529 | rstream *iF; |
530 | xhash *vhash, *ahash, *fdhash, *fnhash; | 530 | xhash *ahash; /* argument names, used only while parsing function bodies */ |
531 | xhash *fnhash; /* function names, used only in parsing stage */ | ||
532 | xhash *vhash; /* variables and arrays */ | ||
533 | xhash *fdhash; /* file objects, used only in execution stage */ | ||
531 | const char *g_progname; | 534 | const char *g_progname; |
532 | int g_lineno; | 535 | int g_lineno; |
533 | int nfields; | 536 | int nfields; |
@@ -1719,6 +1722,7 @@ static void parse_program(char *p) | |||
1719 | debug_printf_parse("%s: TC_FUNCDECL\n", __func__); | 1722 | debug_printf_parse("%s: TC_FUNCDECL\n", __func__); |
1720 | next_token(TC_FUNCTION); | 1723 | next_token(TC_FUNCTION); |
1721 | f = newfunc(t_string); | 1724 | f = newfunc(t_string); |
1725 | //FIXME: dup check: functions can't be redefined, this is not ok: awk 'func f(){}; func f(){}' | ||
1722 | f->body.first = NULL; | 1726 | f->body.first = NULL; |
1723 | f->nargs = 0; | 1727 | f->nargs = 0; |
1724 | /* func arg list: comma sep list of args, and a close paren */ | 1728 | /* func arg list: comma sep list of args, and a close paren */ |
@@ -3389,12 +3393,8 @@ int awk_main(int argc UNUSED_PARAM, char **argv) | |||
3389 | if (ENABLE_LOCALE_SUPPORT) | 3393 | if (ENABLE_LOCALE_SUPPORT) |
3390 | setlocale(LC_NUMERIC, "C"); | 3394 | setlocale(LC_NUMERIC, "C"); |
3391 | 3395 | ||
3392 | vhash = hash_init(); | ||
3393 | ahash = hash_init(); | ||
3394 | fdhash = hash_init(); | ||
3395 | fnhash = hash_init(); | ||
3396 | |||
3397 | /* initialize variables */ | 3396 | /* initialize variables */ |
3397 | vhash = hash_init(); | ||
3398 | { | 3398 | { |
3399 | char *vnames = (char *)vNames; /* cheat */ | 3399 | char *vnames = (char *)vNames; /* cheat */ |
3400 | char *vvalues = (char *)vValues; | 3400 | char *vvalues = (char *)vValues; |
@@ -3416,10 +3416,6 @@ int awk_main(int argc UNUSED_PARAM, char **argv) | |||
3416 | handle_special(intvar[FS]); | 3416 | handle_special(intvar[FS]); |
3417 | handle_special(intvar[RS]); | 3417 | handle_special(intvar[RS]); |
3418 | 3418 | ||
3419 | newfile("/dev/stdin")->F = stdin; | ||
3420 | newfile("/dev/stdout")->F = stdout; | ||
3421 | newfile("/dev/stderr")->F = stderr; | ||
3422 | |||
3423 | /* Huh, people report that sometimes environ is NULL. Oh well. */ | 3419 | /* Huh, people report that sometimes environ is NULL. Oh well. */ |
3424 | if (environ) { | 3420 | if (environ) { |
3425 | char **envp; | 3421 | char **envp; |
@@ -3449,6 +3445,10 @@ int awk_main(int argc UNUSED_PARAM, char **argv) | |||
3449 | if (!is_assignment(llist_pop(&list_v))) | 3445 | if (!is_assignment(llist_pop(&list_v))) |
3450 | bb_show_usage(); | 3446 | bb_show_usage(); |
3451 | } | 3447 | } |
3448 | |||
3449 | /* Parse all supplied programs */ | ||
3450 | fnhash = hash_init(); | ||
3451 | ahash = hash_init(); | ||
3452 | while (list_f) { | 3452 | while (list_f) { |
3453 | int fd; | 3453 | int fd; |
3454 | char *s; | 3454 | char *s; |
@@ -3471,6 +3471,11 @@ int awk_main(int argc UNUSED_PARAM, char **argv) | |||
3471 | bb_show_usage(); | 3471 | bb_show_usage(); |
3472 | parse_program(*argv++); | 3472 | parse_program(*argv++); |
3473 | } | 3473 | } |
3474 | //free_hash(ahash) // ~250 bytes, arg names, used only during parse of function bodies | ||
3475 | //ahash = NULL; // debug | ||
3476 | //free_hash(fnhash) // ~250 bytes, used only for function names | ||
3477 | //fnhash = NULL; // debug | ||
3478 | /* parsing done, on to executing */ | ||
3474 | 3479 | ||
3475 | /* fill in ARGV array */ | 3480 | /* fill in ARGV array */ |
3476 | setari_u(intvar[ARGV], 0, "awk"); | 3481 | setari_u(intvar[ARGV], 0, "awk"); |
@@ -3479,6 +3484,11 @@ int awk_main(int argc UNUSED_PARAM, char **argv) | |||
3479 | setari_u(intvar[ARGV], ++i, *argv++); | 3484 | setari_u(intvar[ARGV], ++i, *argv++); |
3480 | setvar_i(intvar[ARGC], i + 1); | 3485 | setvar_i(intvar[ARGC], i + 1); |
3481 | 3486 | ||
3487 | fdhash = hash_init(); | ||
3488 | newfile("/dev/stdin")->F = stdin; | ||
3489 | newfile("/dev/stdout")->F = stdout; | ||
3490 | newfile("/dev/stderr")->F = stderr; | ||
3491 | |||
3482 | zero_out_var(&tv); | 3492 | zero_out_var(&tv); |
3483 | evaluate(beginseq.first, &tv); | 3493 | evaluate(beginseq.first, &tv); |
3484 | if (!mainseq.first && !endseq.first) | 3494 | if (!mainseq.first && !endseq.first) |