refactor: extract init_pages
This commit is contained in:
@@ -265,11 +265,24 @@ thread_create (const char *name, int priority,
|
||||
#ifdef USERPROG
|
||||
/* Initialize the thread's file descriptor table. */
|
||||
t->fd_counter = MINIMUM_USER_FD;
|
||||
bool success = hash_init (&t->open_files, fd_hash, fd_less, NULL);
|
||||
if (success)
|
||||
{
|
||||
success = hash_init (&t->child_results, process_result_hash,
|
||||
process_result_less, t);
|
||||
if (!success)
|
||||
hash_destroy (&t->open_files, NULL);
|
||||
#ifdef VM
|
||||
else
|
||||
{
|
||||
success = init_pages (&t->pages);
|
||||
if (!success)
|
||||
hash_destroy (&t->child_results, NULL);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!hash_init (&t->open_files, fd_hash, fd_less, NULL)
|
||||
|| !hash_init (&t->child_results, process_result_hash,
|
||||
process_result_less, t)
|
||||
|| !hash_init (&t->pages, page_hash, page_less, NULL))
|
||||
if (!success)
|
||||
{
|
||||
palloc_free_page (t);
|
||||
free (t->result);
|
||||
|
||||
@@ -14,10 +14,22 @@
|
||||
#define SWAP_FLAG_BIT 9
|
||||
#define ADDR_START_BIT 12
|
||||
|
||||
static unsigned page_hash (const struct hash_elem *e, void *aux UNUSED);
|
||||
static bool page_less (const struct hash_elem *a_, const struct hash_elem *b_,
|
||||
void *aux UNUSED);
|
||||
|
||||
/* Initialise a supplementary page table. */
|
||||
bool
|
||||
init_pages (struct hash *pages)
|
||||
{
|
||||
ASSERT (pages != NULL);
|
||||
return hash_init (pages, page_hash, page_less, NULL);
|
||||
}
|
||||
|
||||
/* Hashing function needed for the SPT table. Returns a hash for an entry,
|
||||
based on its upage. */
|
||||
unsigned
|
||||
page_hash (const struct hash_elem *e, UNUSED void *aux)
|
||||
static unsigned
|
||||
page_hash (const struct hash_elem *e, void *aux UNUSED)
|
||||
{
|
||||
struct page_entry *page = hash_entry (e, struct page_entry, elem);
|
||||
return hash_ptr (page->upage);
|
||||
@@ -25,7 +37,7 @@ page_hash (const struct hash_elem *e, UNUSED void *aux)
|
||||
|
||||
/* Comparator function for the SPT table. Compares two entries based on their
|
||||
upages. */
|
||||
bool
|
||||
static bool
|
||||
page_less (const struct hash_elem *a_, const struct hash_elem *b_,
|
||||
void *aux UNUSED)
|
||||
{
|
||||
@@ -116,7 +128,7 @@ page_get (void *upage)
|
||||
}
|
||||
|
||||
bool
|
||||
page_load_file (struct page_entry *page, bool writable)
|
||||
page_load_file (struct page_entry *page)
|
||||
{
|
||||
/* Allocate a frame for the page. If a frame allocation fails, then
|
||||
frame_alloc should try to evict a page. If it is still NULL, the OS
|
||||
@@ -128,7 +140,7 @@ page_load_file (struct page_entry *page, bool writable)
|
||||
PANIC ("Could not allocate a frame to load page into memory.");
|
||||
|
||||
/* Map the page to the frame. */
|
||||
if (!install_page (page->upage, frame, writable))
|
||||
if (!install_page (page->upage, frame, page->writable))
|
||||
{
|
||||
frame_free (frame);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user