From 1ce09a49a11315d4159e47e30448b8c662f37724 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Wed, 4 Dec 2024 15:08:43 +0000 Subject: [PATCH] Add helper functions to initialise the memory-mapped files table and counter --- src/threads/thread.c | 3 +++ src/vm/mmap.c | 36 +++++++++++++++++++++++++++++++++++- src/vm/mmap.h | 3 +++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index c2944cc..cda9f49 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -16,6 +16,7 @@ #include "threads/synch.h" #include "threads/vaddr.h" #include "vm/page.h" +#include "vm/mmap.h" #ifdef USERPROG #include "userprog/process.h" #include "userprog/syscall.h" @@ -274,6 +275,8 @@ thread_create (const char *name, int priority, } #endif + mmap_init (); + /* Prepare thread for first run by initializing its stack. Do this atomically so intermediate values for the 'stack' member cannot be observed. */ diff --git a/src/vm/mmap.c b/src/vm/mmap.c index 328f37b..48bbf39 100644 --- a/src/vm/mmap.c +++ b/src/vm/mmap.c @@ -1 +1,35 @@ -#include "mmap.h" \ No newline at end of file +#include "mmap.h" +#include "threads/malloc.h" + +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); + +/* Initializes the mmap table for the current thread. Sets the counter to 0. */ +bool +mmap_init (void) +{ + struct thread *t = thread_current (); + t->mmap_counter = 0; + return hash_init (&t->mmap_files, mmap_hash, mmap_less, NULL); +} + +/* A hash function for the mmap table. Returns a hash for an entry, based on its + mapping. */ +static unsigned +mmap_hash (const struct hash_elem *e, void *aux UNUSED) +{ + return hash_entry (e, struct mmap_entry, elem)->mapping; +} + +/* A comparator function for the mmap table. Compares two entries based on their + mappings. */ +static bool +mmap_less (const struct hash_elem *a_, const struct hash_elem *b_, + void *aux UNUSED) +{ + const struct mmap_entry *a = hash_entry (a_, struct mmap_entry, elem); + const struct mmap_entry *b = hash_entry (b_, struct mmap_entry, elem); + + return a->mapping < b->mapping; +} diff --git a/src/vm/mmap.h b/src/vm/mmap.h index 8fd64ee..30bf76a 100644 --- a/src/vm/mmap.h +++ b/src/vm/mmap.h @@ -2,6 +2,7 @@ #define VM_MMAP_H #include +#include "threads/thread.h" /* A mapping identifier type. */ typedef unsigned mapid_t; @@ -15,4 +16,6 @@ struct mmap_entry { struct hash_elem elem; /* An elem for the hash table. */ }; +bool mmap_init (void); + #endif /* vm/mmap.h */