// PageTable.cpp Dave Reed 11/20/02 #include #include "PageTable.h" using namespace std; PageTable::PageTable(int log, int phys): pageMap(log, -1), valid(log, false), timeStamp(log, 0) // constructor // Assumes: log is the number of pages in the page table // phys is the number of frames in physical memory // Results: creates and initializes the entries in the page table { currentTime = 0; for (int j = phys-1; j >= 0; j--) { freeFrames.push_back(j); } } bool PageTable::AccessPage(int pageNum) // Assumes: 0 <= pageNum < page table size // Returns: true if valid bit is set for that index, else false { return valid[pageNum]; } int PageTable::LocatePage(int pageNum) // Assumes: 0 <= pageNum < page table size // Returns: the frame number where pageNum is stored { return pageMap[pageNum]; } void PageTable::StorePage(int pageNum) // Assumes: 0 <= pageNum < page table size // Results: stores pageNum in a free frame, updating the table { currentTime++; int frameNum = freeFrames.back(); freeFrames.pop_back(); pageMap[pageNum] = frameNum; timeStamp[pageNum] = currentTime; valid[pageNum] = true; } void PageTable::ReplacePage(int pageNum, int swapPage) // Assumes: 0 <= pageNum, swapPage < page table size // Results: replaces swapPage with pageNum in the table { currentTime++; int frameNum = LocatePage(swapPage); valid[swapPage] = false; pageMap[pageNum] = frameNum; timeStamp[pageNum] = currentTime; valid[pageNum] = true; } int PageTable::SelectSwapPage() // Assumes: at least one page is stored in the table // Returns: index of the oldest page in the table { int oldFrame, oldTime = currentTime + 1; for (int i = 0; i < pageMap.size(); i++) { if (valid[i] && timeStamp[i] < oldTime) { oldTime = timeStamp[i]; oldFrame = i; } } return oldFrame; } bool PageTable::FreeFramesRemaining() // Returns: true if any free frames are available in physical memory { return freeFrames.size() > 0; }