summaryrefslogtreecommitdiffstats
path: root/src/userprog/syscall.c
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-02-05 16:14:34 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-02-05 16:14:34 +0100
commit466145ce0bf0423fdd0c70f7232978cdb2c8aef7 (patch)
treef8e2a98a1ef035c6ee371cdc9e043a0097002ba5 /src/userprog/syscall.c
parent1bf4e6694adcb9e0e3ad6c950b61761fa05aac7f (diff)
downloadpintos-466145ce0bf0423fdd0c70f7232978cdb2c8aef7.tar.gz
remove unnecessary fields
Diffstat (limited to 'src/userprog/syscall.c')
-rw-r--r--src/userprog/syscall.c40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c
index e72ee0b..0dd48b0 100644
--- a/src/userprog/syscall.c
+++ b/src/userprog/syscall.c
@@ -34,16 +34,9 @@ open (const char *filename)
{
struct thread *t = thread_current ();
- if (t->max_files == 0) {
- t->max_files = 128;
- printf("[%s] open: allocating %d file descriptors\n", t->name, t->max_files);
- t->files = (struct fd *) calloc (t->max_files, sizeof (struct fd));
- }
-
- if (t->num_files == t->max_files) {
- // not enough space for more file descriptors
- printf("[%s] open: too many file descriptors\n", t->name);
- return -1;
+ if (!t->fds) {
+ printf("[%s] open: allocating %d file descriptors\n", t->name, MAX_FDS);
+ t->fds = (struct fd *) calloc (MAX_FDS, sizeof (struct fd));
}
struct file *file = filesys_open(filename);
@@ -52,8 +45,8 @@ open (const char *filename)
return -1;
}
- for (int i = 0; i < t->max_files; i++) {
- struct fd *fd = t->files + i;
+ for (int i = 0; i < MAX_FDS; i++) {
+ struct fd *fd = t->fds + i;
if (!fd->active) {
fd->active = true;
fd->file = file;
@@ -71,6 +64,26 @@ write (const char *buf)
printf ("printf: %s", buf);
}
+static void
+close (int i)
+{
+ struct thread *t = thread_current ();
+
+ if (!t->fds) {
+ printf("[%s] close: no files have been opened\n", t->name);
+ return;
+ }
+
+ struct fd *fd = t->fds + i - 2; // -2 since 0 and 1 are reserved and not present in the array
+ if (!fd->active) {
+ printf("[%s] close: tried to close inactive file descriptor %d\n", t->name, i);
+ return;
+ }
+
+ free(fd->file);
+ fd->active = false;
+}
+
// cast to TYPE and deref argument N from f->esp
#define INTR_ESP(N, TYPE) *(TYPE *)(f->esp+(4*(N)))
@@ -99,10 +112,11 @@ syscall_handler (struct intr_frame *f UNUSED)
break;
case 9:
// write
- write(INTR_ESP(2, char *));
+ write (INTR_ESP(2, char *));
break;
case 12:
// close
+ close (INTR_ESP(1, int));
break;
default:
printf ("kernel: unknown syscall '%d'\n", syscall_number);