Example #1
0
static unsigned take_phys_page()
{
	unsigned		page;	/* Page to be replaced. */
	page = (*replace)(); //calls either fifo_page_replace or second_chance_replace, can be found in main

	coremap_entry_t* e = &coremap[page]; //fetch physical page address
	if (e->owner==NULL) //no owner, free space - just use it!
		return page;

	page_table_entry_t* pt = e->owner;
	// printf("has owner!\r\n");

	if (pt->ondisk) {
		//is located on disk, check if modified
		// printf("on disk!\r\n");
		if (pt->modified)
			write_page(page,e->page);
		pt->page = e->page;
	} else {
		// printf("not on disk!\r\n");
		unsigned sw = new_swap_page();
		pt->page = sw;
		write_page(page,sw);
	}

	pt->ondisk = 1;
	pt->modified = 0;
	pt->inmemory = 0;
	pt->referenced = 0;

	return page;
}
Example #2
0
static unsigned take_phys_page(unsigned virt_page)
{
	unsigned            page;	/* Page to be replaced. */
    coremap_entry_t     *coremap_entry;
    page_table_entry_t  *swap_entry;

	page = (*replace)();

    /*printf("\t%x\n", page);*/
    
    // I renamed ram_entry to coremap_entry:
    coremap_entry = &coremap[page]; 	 // "page" is the page in RAM we want to replace, 
										 // and coremap_entry->page is the same page but on swap.
										 
    swap_entry = &page_table[virt_page]; // swap_entry->page is the page we want to get, 
										 // now (probably) on swap. We want to write this page to "page".
    

    if (coremap_entry->owner != NULL) {
        
        if (coremap_entry->owner->modified) {
            if (!coremap_entry->owner->ondisk) {
                coremap_entry->page = new_swap_page();
                coremap_entry->owner->ondisk = 1;
            }

            write_page(page, coremap_entry->page);
            coremap_entry->owner->modified = 0;
        } 

        coremap_entry->owner->inmemory = 0;
        coremap_entry->owner->page = coremap_entry->page;
        coremap_entry->owner->ondisk = 1;
    }
    
    // after this you can write over page, without loosing the old page.
    
    if(swap_entry->ondisk){
		read_page(page, swap_entry->page);
		coremap_entry->page = swap_entry->page;
	}
	
	swap_entry->inmemory = 1;
	swap_entry->page = page;
	coremap_entry->owner = swap_entry;
	
	return page;
}