Refactor process_execute to pass process start data as a local reference rather than perform memory allocation on the heap

This commit is contained in:
Themis Demetriades
2024-11-14 14:32:48 +00:00
parent 79b3b8fda7
commit 1a2ff35231

View File

@@ -65,13 +65,10 @@ process_execute (const char *cmd)
char *cmd_copy; char *cmd_copy;
tid_t tid; tid_t tid;
struct process_start_data *data = malloc (sizeof (struct process_start_data)); struct process_start_data data;
if (data == NULL)
{ sema_init (&data.loaded, 0);
return TID_ERROR; data.success = false;
}
sema_init (&data->loaded, 0);
data->success = false;
/* Make a copy of command. /* Make a copy of command.
Otherwise there's a race between the caller and load(). */ Otherwise there's a race between the caller and load(). */
@@ -85,7 +82,7 @@ process_execute (const char *cmd)
/* Retrieve first argument of command, which is the file name /* Retrieve first argument of command, which is the file name
of the process. */ of the process. */
char *file_name = strtok_r (cmd_copy, " ", &data->cmd_saveptr); char *file_name = strtok_r (cmd_copy, " ", &data.cmd_saveptr);
/* NOTE: Currently, the file being executed is closed in load () and then /* NOTE: Currently, the file being executed is closed in load () and then
reopened here. Because load is an exported public function, this reopened here. Because load is an exported public function, this
@@ -100,19 +97,18 @@ process_execute (const char *cmd)
/* Create a new thread to execute the command, by initializing /* Create a new thread to execute the command, by initializing
it running the function 'start_process' with the appropriate it running the function 'start_process' with the appropriate
arguments. For details of arguments, see 'start_process'. */ arguments. For details of arguments, see 'start_process'. */
data->cmd = cmd_copy; data.cmd = cmd_copy;
strlcpy (data->file_name, file_name, FNAME_MAX_LEN + 1); strlcpy (data.file_name, file_name, FNAME_MAX_LEN + 1);
tid = thread_create (file_name, PRI_DEFAULT, start_process, data); tid = thread_create (file_name, PRI_DEFAULT, start_process, &data);
if (tid == TID_ERROR) if (tid == TID_ERROR)
palloc_free_page (cmd_copy); palloc_free_page (cmd_copy);
else else
{ {
sema_down (&data->loaded); sema_down (&data.loaded);
if (!data->success) if (!data.success)
tid = TID_ERROR; tid = TID_ERROR;
} }
free (data);
return tid; return tid;
} }