diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-06 12:56:28 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-06 12:56:28 +0000 |
commit | 34e573d2cee139f1b694737a66be394e0676d0d4 (patch) | |
tree | 27de8106eaf198464eff3849c11e1923f41a7901 | |
parent | 3c7167b508977997bb4ae43f12b34e60e9d3ba91 (diff) | |
download | busybox-w32-34e573d2cee139f1b694737a66be394e0676d0d4.tar.gz busybox-w32-34e573d2cee139f1b694737a66be394e0676d0d4.tar.bz2 busybox-w32-34e573d2cee139f1b694737a66be394e0676d0d4.zip |
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
-rw-r--r-- | shell/hush.c | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/shell/hush.c b/shell/hush.c index 4f4012db8..dfbd457a2 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -2306,26 +2306,28 @@ static void reset_traps_to_defaults(void) | |||
2306 | static void re_execute_shell(const char *s) NORETURN; | 2306 | static void re_execute_shell(const char *s) NORETURN; |
2307 | static void re_execute_shell(const char *s) | 2307 | static void re_execute_shell(const char *s) |
2308 | { | 2308 | { |
2309 | char param_buf[sizeof("-$%x:%x:%x:%x") + sizeof(unsigned) * 4]; | ||
2309 | struct variable *cur; | 2310 | struct variable *cur; |
2310 | char **argv, **pp, **pp2; | 2311 | char **argv, **pp, **pp2; |
2311 | unsigned cnt; | 2312 | unsigned cnt; |
2312 | 2313 | ||
2313 | /* 1:hush 2:-$<pid> 3:-!<pid> 4:-?<exitcode> 5:-D<depth> <vars...> | 2314 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<depth> <vars...> |
2314 | * 6:-c 7:<cmd> <argN...> 8:NULL | 2315 | * 3:-c 4:<cmd> <argN...> 5:NULL |
2315 | */ | 2316 | */ |
2316 | cnt = 8 + G.global_argc; | 2317 | cnt = 5 + G.global_argc; |
2317 | for (cur = G.top_var; cur; cur = cur->next) { | 2318 | for (cur = G.top_var; cur; cur = cur->next) { |
2318 | if (!cur->flg_export || cur->flg_read_only) | 2319 | if (!cur->flg_export || cur->flg_read_only) |
2319 | cnt += 2; | 2320 | cnt += 2; |
2320 | } | 2321 | } |
2321 | G.argv_from_re_execing = pp = xzalloc(sizeof(argv[0]) * cnt); | 2322 | G.argv_from_re_execing = pp = xzalloc(sizeof(argv[0]) * cnt); |
2322 | *pp++ = (char *) G.argv0_for_re_execing; | 2323 | *pp++ = (char *) G.argv0_for_re_execing; |
2323 | *pp++ = xasprintf("-$%u", (unsigned) G.root_pid); | 2324 | sprintf(param_buf, "-$%x:%x:%x" USE_HUSH_LOOPS(":%x") |
2324 | *pp++ = xasprintf("-!%u", (unsigned) G.last_bg_pid); | 2325 | , (unsigned) G.root_pid |
2325 | *pp++ = xasprintf("-?%u", (unsigned) G.last_return_code); | 2326 | , (unsigned) G.last_bg_pid |
2326 | #if ENABLE_HUSH_LOOPS | 2327 | , (unsigned) G.last_return_code |
2327 | *pp++ = xasprintf("-D%u", G.depth_of_loop); | 2328 | USE_HUSH_LOOPS(, G.depth_of_loop) |
2328 | #endif | 2329 | ); |
2330 | *pp++ = param_buf; | ||
2329 | for (cur = G.top_var; cur; cur = cur->next) { | 2331 | for (cur = G.top_var; cur; cur = cur->next) { |
2330 | if (cur->varstr == hush_version_str) | 2332 | if (cur->varstr == hush_version_str) |
2331 | continue; | 2333 | continue; |
@@ -2378,13 +2380,7 @@ static void clean_up_after_re_execute(void) | |||
2378 | { | 2380 | { |
2379 | char **pp = G.argv_from_re_execing; | 2381 | char **pp = G.argv_from_re_execing; |
2380 | if (pp) { | 2382 | if (pp) { |
2381 | /* Must match re_execute_shell's allocations */ | 2383 | /* Must match re_execute_shell's allocations (if any) */ |
2382 | free(pp[1]); | ||
2383 | free(pp[2]); | ||
2384 | free(pp[3]); | ||
2385 | #if ENABLE_HUSH_LOOPS | ||
2386 | free(pp[4]); | ||
2387 | #endif | ||
2388 | free(pp); | 2384 | free(pp); |
2389 | G.argv_from_re_execing = NULL; | 2385 | G.argv_from_re_execing = NULL; |
2390 | } | 2386 | } |
@@ -5112,19 +5108,16 @@ int hush_main(int argc, char **argv) | |||
5112 | break; | 5108 | break; |
5113 | #if !BB_MMU | 5109 | #if !BB_MMU |
5114 | case '$': | 5110 | case '$': |
5115 | G.root_pid = xatoi_u(optarg); | 5111 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
5116 | break; | 5112 | optarg++; |
5117 | case '!': | 5113 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
5118 | G.last_bg_pid = xatoi_u(optarg); | 5114 | optarg++; |
5119 | break; | 5115 | G.last_return_code = bb_strtou(optarg, &optarg, 16); |
5120 | case '?': | ||
5121 | G.last_return_code = xatoi_u(optarg); | ||
5122 | break; | ||
5123 | # if ENABLE_HUSH_LOOPS | 5116 | # if ENABLE_HUSH_LOOPS |
5124 | case 'D': | 5117 | optarg++; |
5125 | G.depth_of_loop = xatoi_u(optarg); | 5118 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
5126 | break; | ||
5127 | # endif | 5119 | # endif |
5120 | break; | ||
5128 | case 'R': | 5121 | case 'R': |
5129 | case 'V': | 5122 | case 'V': |
5130 | set_local_var(xstrdup(optarg), 0, opt == 'R'); | 5123 | set_local_var(xstrdup(optarg), 0, opt == 'R'); |