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