Add helper functions to initialise the memory-mapped files table and counter
This commit is contained in:
@@ -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. */
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define VM_MMAP_H
|
||||
|
||||
#include <hash.h>
|
||||
#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 */
|
||||
|
||||
Reference in New Issue
Block a user