LRU Implementation
#include "lru.h"
LRUReplacementAlg::LRUReplacementAlg(unsigned long num_frame, int page_bits):
ReplacementAlg(num_frame, page_bits), m_count(0)
{
m_counts = new unsigned long [num_frame];
for(unsigned int i = 0; i < num_frame; ++i) m_counts[i] = 0;
}
void LRUReplacementAlg::ref(const Ref &r)
{
const PTE *pte = get_PTE_by_page(pageno(r));
if(pte->is_valid()) m_counts[pte->get_frame()] = m_count++;
ReplacementAlg::ref(r);
}
// Return the least-recently-used page: the one with the smallest count.
unsigned long LRUReplacementAlg::fault(const Ref &r)
{
unsigned long ret = 0;
unsigned long mincount = m_counts[0];
for(unsigned long i = 1; i < get_num_frames(); ++i) {
if(m_counts[i] < mincount) {
ret = i;
m_counts[i] = mincount;
}
}
m_counts[ret] = m_count++;
return ret;
}