spz3/Kernel.cpp

46 lines
1.4 KiB
C++
Raw Normal View History

2025-03-31 17:19:43 +03:00
#include "Kernel.h"
using std::next;
2025-03-31 17:19:43 +03:00
2025-04-01 14:10:01 +03:00
Kernel::Kernel(const unsigned int RunQ_max_length, const List<PhysicalPage> &free_pages, const List<PhysicalPage> &busy_pages)
: free_pages(free_pages), busy_pages(busy_pages), swapped_page(nullptr) {
RunQ.reserve(RunQ_max_length);
}
2025-03-31 17:19:43 +03:00
2025-04-01 14:10:01 +03:00
void Kernel::page_fault(PageTable *page_table, const unsigned int idx) {
PhysicalPage *page = nullptr;
2025-03-31 17:19:43 +03:00
if (!this->free_pages.is_empty()) {
page = free_pages.get_head()->prev;
2025-03-31 17:19:43 +03:00
free_pages.remove(page);
busy_pages.insert_tail(page);
} else {
for (PhysicalPage *curr = busy_pages.get_head();; curr = curr->next) {
if ((*curr->PT)[curr->idx].R)
(*curr->PT)[curr->idx].R = false;
else {
curr->PT->entries.erase(next(curr->PT->entries.begin(), curr->idx));
page = curr;
busy_pages.set_head(curr->next);
break;
}
}
2025-03-31 17:19:43 +03:00
}
2025-03-31 17:19:43 +03:00
if (page) {
2025-04-01 15:22:38 +03:00
page->take_up(page_table, idx);
2025-04-01 14:10:01 +03:00
(*page_table)[idx].PPN = page->PPN;
(*page_table)[idx].P = true;
(*page_table)[idx].R = false;
(*page_table)[idx].M = false;
2025-03-31 17:19:43 +03:00
}
}
void Kernel::stat_update() {
int i;
PhysicalPage *curr;
for (i = 0, curr = busy_pages.get_head(); i < 10; ++i, curr = curr->next) {
if ((*curr->PT)[curr->idx].R)
(*curr->PT)[curr->idx].R = false;
2025-03-31 17:19:43 +03:00
}
}