From 8b2fc86b516daa2422489622e88b5dcd7da7d76c Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Sun, 10 Nov 2024 14:34:38 +0000 Subject: [PATCH] Refactor process_init_stack to reduce code duplication --- src/userprog/process.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/userprog/process.c b/src/userprog/process.c index f8a0524..c935a07 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -90,6 +90,7 @@ process_execute (const char *cmd) static bool process_init_stack (char *cmd_saveptr, void **esp, char *file_name); static void *push_to_stack (void **esp, void *data, size_t data_size); +#define push_var_to_stack(esp, var) (push_to_stack (esp, &var, sizeof (var))) /* Make the current thread execute 'cmd', passing in a copy of the command string used for processing, the saveptr used by strtok_r @@ -196,11 +197,8 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name) *esp -= align_size; /* Push a null pointer sentinel inside argv. */ - *esp -= sizeof (char *); - *(char *) *esp = 0; - - /* Push pointer to the process file name to the stack. */ - char **argv; + char *null_sentinel = NULL; + push_var_to_stack (esp, null_sentinel); /* Push pointers to process arguments from argument linked list */ struct list_elem *e = list_begin (&arg_list); @@ -209,21 +207,21 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name) { struct arg_elem *arg_elem = list_entry (e, struct arg_elem, elem); - argv = push_to_stack (esp, &arg_elem->arg, sizeof (arg_elem->arg)); + push_var_to_stack(esp, arg_elem->arg); e = list_next (e); free (arg_elem); } /* Push pointer to the start of argv array. */ - push_to_stack (esp, &argv, sizeof(argv)); + char **argv = *esp; + push_var_to_stack(esp, argv); /* Push the number of arguments to the stack. */ - push_to_stack (esp, &arg_count, sizeof (arg_count)); + push_var_to_stack(esp, arg_count); /* Push fake return address (null pointer). */ - *esp -= sizeof (char *); - *(char *) *esp = 0; + push_var_to_stack (esp, null_sentinel); return true; }