I am trying to understand virtual memory paging. I have the following code snippet that represents the first step in the process. Here search_tbl
is called from the main program for each logical address in order to check if the page table already has an entry that maps the provided logical address to a location in physical memory. vfn
is the virtual frame number.
EDITED: Does this implementation make any sense? Or am I going down the wrong road?
Any help/suggestion would be greatly appreciated. Thank you.
uint vfn_bits;//virtual frame number
static tbl_entry **tbl;
uint page_bits = log_2(pagesize);
vfn_bits = addr_space_bits - page_bits;
tbl = calloc(pow_2(vfn_bits), sizeof (tbl_entry*));
tbl_entry *search_tbl(uint vfn) {
uint index = vfn;
if (tbl[index] == NULL) {
/* Initial miss */
tbl[index] = create_tbl_entry(vfn);
}
return tbl[index];
}
tbl_entry *create_tbl_entry(uint vfn) {
tbl_entry *te;
te = (tbl_entry*) (malloc(sizeof (tbl_entry)));
te->vfn = vfn;
te->pfn = -1;
te->valid = FALSE;
te->modified = FALSE;
te->reference = 0;
return te;
}