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;
tid_t tid;
struct process_start_data *data = malloc (sizeof (struct process_start_data));
if (data == NULL)
{
return TID_ERROR;
}
sema_init (&data->loaded, 0);
data->success = false;
struct process_start_data data;
sema_init (&data.loaded, 0);
data.success = false;
/* Make a copy of command.
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
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
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
it running the function 'start_process' with the appropriate
arguments. For details of arguments, see 'start_process'. */
data->cmd = cmd_copy;
strlcpy (data->file_name, file_name, FNAME_MAX_LEN + 1);
data.cmd = cmd_copy;
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)
palloc_free_page (cmd_copy);
else
{
sema_down (&data->loaded);
if (!data->success)
sema_down (&data.loaded);
if (!data.success)
tid = TID_ERROR;
}
free (data);
return tid;
}