Add helper functions to initialise the memory-mapped files table and counter

This commit is contained in:
sBubshait
2024-12-04 15:08:43 +00:00
parent b3042b5aa6
commit 1ce09a49a1
3 changed files with 41 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
#include "threads/synch.h" #include "threads/synch.h"
#include "threads/vaddr.h" #include "threads/vaddr.h"
#include "vm/page.h" #include "vm/page.h"
#include "vm/mmap.h"
#ifdef USERPROG #ifdef USERPROG
#include "userprog/process.h" #include "userprog/process.h"
#include "userprog/syscall.h" #include "userprog/syscall.h"
@@ -274,6 +275,8 @@ thread_create (const char *name, int priority,
} }
#endif #endif
mmap_init ();
/* Prepare thread for first run by initializing its stack. /* Prepare thread for first run by initializing its stack.
Do this atomically so intermediate values for the 'stack' Do this atomically so intermediate values for the 'stack'
member cannot be observed. */ member cannot be observed. */

View File

@@ -1 +1,35 @@
#include "mmap.h" #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;
}

View File

@@ -2,6 +2,7 @@
#define VM_MMAP_H #define VM_MMAP_H
#include <hash.h> #include <hash.h>
#include "threads/thread.h"
/* A mapping identifier type. */ /* A mapping identifier type. */
typedef unsigned mapid_t; typedef unsigned mapid_t;
@@ -15,4 +16,6 @@ struct mmap_entry {
struct hash_elem elem; /* An elem for the hash table. */ struct hash_elem elem; /* An elem for the hash table. */
}; };
bool mmap_init (void);
#endif /* vm/mmap.h */ #endif /* vm/mmap.h */