hush: drop allocations in re_execute_shell

function                                             old     new   delta
hush_main                                           1127    1138     +11
re_execute_shell                                     286     256     -30
clean_up_after_re_execute                             66      30     -36
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 11/-66)            Total: -55 bytes
This commit is contained in:
Denis Vlasenko 2009-04-06 12:56:28 +00:00
parent 3c7167b508
commit 34e573d2ce

View File

@ -2306,26 +2306,28 @@ static void reset_traps_to_defaults(void)
static void re_execute_shell(const char *s) NORETURN; static void re_execute_shell(const char *s) NORETURN;
static void re_execute_shell(const char *s) static void re_execute_shell(const char *s)
{ {
char param_buf[sizeof("-$%x:%x:%x:%x") + sizeof(unsigned) * 4];
struct variable *cur; struct variable *cur;
char **argv, **pp, **pp2; char **argv, **pp, **pp2;
unsigned cnt; unsigned cnt;
/* 1:hush 2:-$<pid> 3:-!<pid> 4:-?<exitcode> 5:-D<depth> <vars...> /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...>
* 6:-c 7:<cmd> <argN...> 8:NULL * 3:-c 4:<cmd> <argN...> 5:NULL
*/ */
cnt = 8 + G.global_argc; cnt = 5 + G.global_argc;
for (cur = G.top_var; cur; cur = cur->next) { for (cur = G.top_var; cur; cur = cur->next) {
if (!cur->flg_export || cur->flg_read_only) if (!cur->flg_export || cur->flg_read_only)
cnt += 2; cnt += 2;
} }
G.argv_from_re_execing = pp = xzalloc(sizeof(argv[0]) * cnt); G.argv_from_re_execing = pp = xzalloc(sizeof(argv[0]) * cnt);
*pp++ = (char *) G.argv0_for_re_execing; *pp++ = (char *) G.argv0_for_re_execing;
*pp++ = xasprintf("-$%u", (unsigned) G.root_pid); sprintf(param_buf, "-$%x:%x:%x" USE_HUSH_LOOPS(":%x")
*pp++ = xasprintf("-!%u", (unsigned) G.last_bg_pid); , (unsigned) G.root_pid
*pp++ = xasprintf("-?%u", (unsigned) G.last_return_code); , (unsigned) G.last_bg_pid
#if ENABLE_HUSH_LOOPS , (unsigned) G.last_return_code
*pp++ = xasprintf("-D%u", G.depth_of_loop); USE_HUSH_LOOPS(, G.depth_of_loop)
#endif );
*pp++ = param_buf;
for (cur = G.top_var; cur; cur = cur->next) { for (cur = G.top_var; cur; cur = cur->next) {
if (cur->varstr == hush_version_str) if (cur->varstr == hush_version_str)
continue; continue;
@ -2378,13 +2380,7 @@ static void clean_up_after_re_execute(void)
{ {
char **pp = G.argv_from_re_execing; char **pp = G.argv_from_re_execing;
if (pp) { if (pp) {
/* Must match re_execute_shell's allocations */ /* Must match re_execute_shell's allocations (if any) */
free(pp[1]);
free(pp[2]);
free(pp[3]);
#if ENABLE_HUSH_LOOPS
free(pp[4]);
#endif
free(pp); free(pp);
G.argv_from_re_execing = NULL; G.argv_from_re_execing = NULL;
} }
@ -5112,19 +5108,16 @@ int hush_main(int argc, char **argv)
break; break;
#if !BB_MMU #if !BB_MMU
case '$': case '$':
G.root_pid = xatoi_u(optarg); G.root_pid = bb_strtou(optarg, &optarg, 16);
break; optarg++;
case '!': G.last_bg_pid = bb_strtou(optarg, &optarg, 16);
G.last_bg_pid = xatoi_u(optarg); optarg++;
break; G.last_return_code = bb_strtou(optarg, &optarg, 16);
case '?':
G.last_return_code = xatoi_u(optarg);
break;
# if ENABLE_HUSH_LOOPS # if ENABLE_HUSH_LOOPS
case 'D': optarg++;
G.depth_of_loop = xatoi_u(optarg); G.depth_of_loop = bb_strtou(optarg, &optarg, 16);
break;
# endif # endif
break;
case 'R': case 'R':
case 'V': case 'V':
set_local_var(xstrdup(optarg), 0, opt == 'R'); set_local_var(xstrdup(optarg), 0, opt == 'R');