Update mmap to add an insert helper function to allocate and add new mmap entries to the hash table

This commit is contained in:
sBubshait
2024-12-04 15:26:00 +00:00
parent 6e838aa06a
commit 6b0f708d8f
2 changed files with 30 additions and 8 deletions

View File

@@ -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);
}

View File

@@ -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 */