diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-02-15 16:00:40 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-02-15 16:00:40 +0100 |
| commit | 6c0df53eacd6fa9b03ea3f6cfbae1bf69bf3f474 (patch) | |
| tree | 5b08aca0705d436e4ea0d2807d5dc86dbd845754 | |
| parent | d83fcc30717f4e687c78bbc7896244643a2e8e4d (diff) | |
| download | pintos-6c0df53eacd6fa9b03ea3f6cfbae1bf69bf3f474.tar.gz | |
use struct **file instead of struct *fdlab1
Storing the null pointer to mark active == false accomplishes the same
thing with less storage.
| -rw-r--r-- | src/filesys/filesys.h | 8 | ||||
| -rw-r--r-- | src/threads/thread.h | 2 | ||||
| -rw-r--r-- | src/userprog/syscall.c | 57 |
3 files changed, 22 insertions, 45 deletions
diff --git a/src/filesys/filesys.h b/src/filesys/filesys.h index 2db5b60..caef83c 100644 --- a/src/filesys/filesys.h +++ b/src/filesys/filesys.h @@ -17,12 +17,4 @@ bool filesys_create (const char *name, off_t initial_size); struct file *filesys_open (const char *name); bool filesys_remove (const char *name); -#ifdef USERPROG -struct fd - { - bool active; - struct file *file; - }; -#endif /* ifdef USERPROG */ - #endif /* filesys/filesys.h */ diff --git a/src/threads/thread.h b/src/threads/thread.h index fc2029b..e3c426d 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -98,7 +98,7 @@ struct thread #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ - struct fd *fds; /* Pointer to array of file descriptors. */ + struct file **fds; /* Pointer to array of file descriptors. */ #endif /* Owned by thread.c. */ diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index dc1f067..921ed44 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -37,7 +37,7 @@ open (const char *filename) struct thread *t = thread_current (); if (!t->fds) { - t->fds = (struct fd *) calloc (MAX_FDS, sizeof (struct fd)); + t->fds = (struct file **) calloc (MAX_FDS, sizeof (struct file *)); } struct file *file = filesys_open (filename); @@ -46,10 +46,9 @@ open (const char *filename) } for (int i = 0; i < MAX_FDS; i++) { - struct fd *fd = t->fds + i; - if (!fd->active) { - fd->active = true; - fd->file = file; + struct file **fd = t->fds + i; + if (*fd == NULL) { + *fd = file; return i + 2; // 0 and 1 are reserved for stdin and stdout } } @@ -57,7 +56,7 @@ open (const char *filename) return -1; } -static struct fd * +static struct file ** get_fd (struct thread *thread, int fd_i) { if (!thread->fds) { @@ -67,18 +66,6 @@ get_fd (struct thread *thread, int fd_i) return thread->fds + fd_i - 2; // -2 since 0 and 1 are reserved and not present in the array } -static struct file * -get_file (struct thread *thread, int fd_i) -{ - struct fd *fd = get_fd (thread, fd_i); - - if (!fd || !fd->active) { - return NULL; - } - - return fd->file; -} - static void exit (int status) { @@ -86,10 +73,10 @@ exit (int status) if (thread->fds) { for (int i = 0; i < MAX_FDS; i++) { - struct fd *fd = thread->fds + i; - if (fd && fd->active) { - file_close (fd->file); - fd->active = false; + struct file **fd = thread->fds + i; + if (fd && *fd) { + file_close (*fd); + *fd = NULL; } } free (thread->fds); @@ -113,12 +100,12 @@ read (int fd_i, void *buf, unsigned size) return -1; } - struct file *file = get_file (thread, fd_i); - if (!file) { + struct file **fd = get_fd (thread, fd_i); + if (fd && *fd) { + return file_read (*fd, buf, size); + } else { return -1; } - - return file_read (file, buf, size); } static int @@ -135,12 +122,12 @@ write (int fd_i, const void *buf, unsigned size) return -1; } - struct file *file = get_file (thread, fd_i); - if (!file) { + struct file **fd = get_fd (thread, fd_i); + if (fd && *fd) { + return file_write (*fd, buf, size); + } else { return -1; } - - return file_write (file, buf, size); } static void @@ -148,13 +135,11 @@ close (int fd_i) { struct thread *thread = thread_current (); - struct fd *fd = get_fd (thread, fd_i); - if (!fd || !fd->active) { - return; + struct file **fd = get_fd (thread, fd_i); + if (fd && *fd) { + file_close (*fd); + *fd = NULL; } - - file_close (fd->file); - fd->active = false; } // cast to TYPE and deref argument N from f->esp |
