From 6b0f708d8f6e997dad234109a18f57f99a444108 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Wed, 4 Dec 2024 15:26:00 +0000 Subject: [PATCH] Update mmap to add an insert helper function to allocate and add new mmap entries to the hash table --- src/vm/mmap.c | 37 +++++++++++++++++++++++++++++-------- src/vm/mmap.h | 1 + 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/vm/mmap.c b/src/vm/mmap.c index 50a0cdf..1f78040 100644 --- a/src/vm/mmap.c +++ b/src/vm/mmap.c @@ -4,6 +4,7 @@ static unsigned mmap_hash (const struct hash_elem *e, void *aux); static bool mmap_less (const struct hash_elem *a_, const struct hash_elem *b_, void *aux); +static void mmap_cleanup(struct hash_elem *e, void *aux); /* Initializes the mmap table for the current thread. Sets the counter to 0. */ bool @@ -14,6 +15,34 @@ mmap_init (void) return hash_init (&t->mmap_files, mmap_hash, mmap_less, NULL); } +/* Inserts a new mmap entry into the mmap table for the current thread. Upage + is the start address of the file data in the user VM. */ +struct mmap_entry * +mmap_insert (struct file *file, void *upage) +{ + if (file == NULL || upage == NULL) + return NULL; + + struct mmap_entry *mmap = malloc (sizeof (struct mmap_entry)); + if (mmap == NULL) + return NULL; + + mmap->mapping = thread_current ()->mmap_counter++; + mmap->file = file; + mmap->upage = upage; + + hash_insert (&thread_current ()->mmap_files, &mmap->elem); + return mmap; +} + +/* Destroys the mmap table for the current thread. Frees all the memory + allocated for the mmap entries. */ +void +mmap_destroy (void) +{ + hash_destroy (&thread_current ()->mmap_files, mmap_cleanup); +} + /* A hash function for the mmap table. Returns a hash for an entry, based on its mapping. */ static unsigned @@ -43,11 +72,3 @@ mmap_cleanup (struct hash_elem *e, void *aux UNUSED) file_close (mmap->file); free (mmap); } - -/* Destroys the mmap table for the current thread. Frees all the memory - allocated for the mmap entries. */ -void -mmap_destroy (void) -{ - hash_destroy (&thread_current ()->mmap_files, mmap_cleanup); -} \ No newline at end of file diff --git a/src/vm/mmap.h b/src/vm/mmap.h index de71ada..c4a6208 100644 --- a/src/vm/mmap.h +++ b/src/vm/mmap.h @@ -18,6 +18,7 @@ struct mmap_entry { }; bool mmap_init (void); +struct mmap_entry *mmap_insert (struct file *file, void *upage); void mmap_destroy (void); #endif /* vm/mmap.h */