-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapper.h
More file actions
44 lines (37 loc) · 1.29 KB
/
Copy pathSwapper.h
File metadata and controls
44 lines (37 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma once
#include <fstream>
#include <vector>
#include <iostream>
#include <climits>
#include "VFS.h"
#include "MemoryAllocator.h"
class Swapper {
private:
std::string swap_file = "swap.sys";
public:
void pageOut(std::vector<VirtualFile*>& all_files, MemoryAllocator& allocator) {
VirtualFile* oldest_file = nullptr;
long long oldest_time = LLONG_MAX;
for (auto* file : all_files) {
if (file->in_memory && file->last_accessed < oldest_time) {
oldest_time = file->last_accessed;
oldest_file = file;
}
}
if (oldest_file) {
std::ofstream out(swap_file, std::ios::app | std::ios::binary);
if (oldest_file->data_ptr) {
out.write(oldest_file->data_ptr, 4096);
}
out.close();
allocator.freePage(oldest_file->data_ptr);
oldest_file->in_memory = false;
oldest_file->data_ptr = nullptr;
}
}
void pageIn(VirtualFile* file, MemoryAllocator& allocator) {
std::cout << "[PAGE FAULT] File '" << file->name << "' not in RAM. Fetching from disk...\n";
file->data_ptr = allocator.allocatePage();
file->in_memory = true;
}
};