Implement VM #63
@@ -4,6 +4,7 @@
|
|||||||
static unsigned mmap_hash (const struct hash_elem *e, void *aux);
|
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_,
|
static bool mmap_less (const struct hash_elem *a_, const struct hash_elem *b_,
|
||||||
void *aux);
|
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. */
|
/* Initializes the mmap table for the current thread. Sets the counter to 0. */
|
||||||
bool
|
bool
|
||||||
@@ -14,6 +15,34 @@ mmap_init (void)
|
|||||||
return hash_init (&t->mmap_files, mmap_hash, mmap_less, NULL);
|
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
|
/* A hash function for the mmap table. Returns a hash for an entry, based on its
|
||||||
mapping. */
|
mapping. */
|
||||||
static unsigned
|
static unsigned
|
||||||
@@ -43,11 +72,3 @@ mmap_cleanup (struct hash_elem *e, void *aux UNUSED)
|
|||||||
file_close (mmap->file);
|
file_close (mmap->file);
|
||||||
free (mmap);
|
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);
|
|
||||||
}
|
|
||||||
@@ -18,6 +18,7 @@ struct mmap_entry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool mmap_init (void);
|
bool mmap_init (void);
|
||||||
|
struct mmap_entry *mmap_insert (struct file *file, void *upage);
|
||||||
void mmap_destroy (void);
|
void mmap_destroy (void);
|
||||||
|
|
||||||
#endif /* vm/mmap.h */
|
#endif /* vm/mmap.h */
|
||||||
|
|||||||
Reference in New Issue
Block a user