From 640e1a8727f53da53771b3dc608ab4ccce93d309 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 10 Sep 2020 22:32:47 -0500 Subject: [PATCH] Update: Improve execution functions to use fewer allocations. While the project is allocation heavy, I do try to avoid allocations where I reasonably can without breaking the design. In this case, I can see no reason for allocation this memory in this way. All that needs to be done here is just to point to the existing strings! --- level_2/fll_execute/c/execute.c | 132 ++-------------------------------------- 1 file changed, 4 insertions(+), 128 deletions(-) diff --git a/level_2/fll_execute/c/execute.c b/level_2/fll_execute/c/execute.c index b46debd..5ec39ec 100644 --- a/level_2/fll_execute/c/execute.c +++ b/level_2/fll_execute/c/execute.c @@ -245,20 +245,7 @@ extern "C" { } for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_new(status, fixed_arguments[i + 1], arguments.array[i].used + 1); - - if (F_status_is_error(status)) { - if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - - for (f_string_length_t j = 0; j < i; j++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[j].used + 1); - } // for - - return status; - } - - memcpy(fixed_arguments[i + 1], arguments.array[i].string, arguments.array[i].used); - fixed_arguments[i + 1][arguments.array[i].used] = 0; + fixed_arguments[i + 1] = arguments.array[i].string; } // for // insert the required array terminated @@ -268,19 +255,11 @@ extern "C" { if (F_status_is_error(status)) { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return status; } else if (status == F_false) { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return F_status_set_error(F_file_found_not); } @@ -291,10 +270,6 @@ extern "C" { if (process_id < 0) { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return F_status_set_error(F_fork); } @@ -311,10 +286,6 @@ extern "C" { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - if (result != 0 && *result != 0) return F_status_set_error(F_failure); return F_none; @@ -384,20 +355,7 @@ extern "C" { } for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_new(status, fixed_arguments[i + 1], arguments.array[i].used + 1); - - if (F_status_is_error(status)) { - if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - - for (f_string_length_t j = 0; j < i; j++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[j].used + 1); - } // for - - return status; - } - - memcpy(fixed_arguments[i + 1], arguments.array[i].string, arguments.array[i].used); - fixed_arguments[i + 1][arguments.array[i].used] = 0; + fixed_arguments[i + 1] = arguments.array[i].string; } // for // insert the required array terminated @@ -408,19 +366,11 @@ extern "C" { if (F_status_is_error(status)) { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return status; } else if (status == F_false) { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return F_status_set_error(F_file_found_not); } @@ -431,10 +381,6 @@ extern "C" { if (process_id < 0) { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return F_status_set_error(F_fork); } @@ -457,10 +403,6 @@ extern "C" { if (name_size > 0) f_macro_string_t_delete_simple(program_name, name_size); - for (f_string_length_t i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - if (result != 0 && *result != 0) return F_status_set_error(F_failure); return F_none; @@ -484,18 +426,7 @@ extern "C" { f_array_length_t i = 0; for (; i < arguments.used; i++) { - f_macro_string_t_new(status, fixed_arguments[i + 1], arguments.array[i].used + 1); - - if (F_status_is_error(status)) { - for (f_array_length_t j = 0; j < i; j++) { - f_macro_string_t_delete_simple(fixed_arguments[j + 1], arguments.array[j].used + 1); - } // for - - return status; - } - - memcpy(fixed_arguments[i + 1], arguments.array[i].string, arguments.array[i].used); - fixed_arguments[i + 1][arguments.array[i].used] = 0; + fixed_arguments[i + 1] = arguments.array[i].string; } // for // insert the required array terminated @@ -506,10 +437,6 @@ extern "C" { process_id = fork(); if (process_id < 0) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return F_status_set_error(F_fork); } @@ -517,18 +444,10 @@ extern "C" { if (process_id == 0) { execvp(program_name, fixed_arguments); - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - // according to manpages, calling _exit() is safer and should be called here instead of exit() _exit(-1); } - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - // have the parent wait for the child process to finish waitpid(process_id, result, 0); @@ -558,18 +477,7 @@ extern "C" { f_array_length_t i = 0; for (; i < arguments.used; i++) { - f_macro_string_t_new(status, fixed_arguments[i + 1], arguments.array[i].used + 1); - - if (F_status_is_error(status)) { - for (f_array_length_t j = 0; j < i; j++) { - f_macro_string_t_delete_simple(fixed_arguments[j + 1], arguments.array[j].used + 1); - } // for - - return status; - } - - memcpy(fixed_arguments[i + 1], arguments.array[i].string, arguments.array[i].used); - fixed_arguments[i + 1][arguments.array[i].used] = 0; + fixed_arguments[i + 1] = arguments.array[i].string; } // for // insert the required array terminated @@ -591,10 +499,6 @@ extern "C" { } if (F_status_is_error(status)) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - f_macro_string_dynamic_t_delete_simple(path); f_macro_string_dynamics_t_delete_simple(paths); return status; @@ -602,10 +506,6 @@ extern "C" { f_macro_string_dynamic_t_delete(status, path); if (F_status_is_error(status)) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - f_macro_string_dynamics_t_delete_simple(paths); return status; } @@ -645,20 +545,12 @@ extern "C" { } if (F_status_is_error(status)) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - f_macro_string_dynamics_t_delete_simple(paths); return status; } } // for if (found == 0) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - f_macro_string_dynamics_t_delete_simple(paths); return F_status_set_error(F_file_found_not); } @@ -669,10 +561,6 @@ extern "C" { f_macro_string_dynamics_t_delete(status, paths); if (F_status_is_error(status)) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return status; } @@ -681,10 +569,6 @@ extern "C" { process_id = fork(); if (process_id < 0) { - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - return F_status_set_error(F_fork); } @@ -698,18 +582,10 @@ extern "C" { execvp(program_path, fixed_arguments); - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - // according to manpages, calling _exit() is safer and should be called here instead of exit() _exit(-1); } - for (i = 0; i < arguments.used; i++) { - f_macro_string_t_delete_simple(fixed_arguments[i + 1], arguments.array[i].used + 1); - } // for - // have the parent wait for the child process to finish waitpid(process_id, result, 0); -- 1.8.3.1