-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicalMemory.cpp
More file actions
67 lines (51 loc) · 1.67 KB
/
PhysicalMemory.cpp
File metadata and controls
67 lines (51 loc) · 1.67 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "PhysicalMemory.h"
#include <vector>
#include <unordered_map>
#include <cassert>
typedef std::vector<word_t> page_t;
std::vector<page_t> RAM;
std::unordered_map<uint64_t, page_t> swapFile;
void initialize() {
RAM.resize(NUM_FRAMES, page_t(PAGE_SIZE));
}
void PMread(uint64_t physicalAddress, word_t* value) {
if (RAM.empty()) {
initialize();
}
assert(physicalAddress < RAM_SIZE);
uint64_t frameIndex = physicalAddress / PAGE_SIZE;
uint64_t frameOffset = physicalAddress % PAGE_SIZE;
*value = RAM[frameIndex][frameOffset];
}
void PMwrite(uint64_t physicalAddress, word_t value) {
if (RAM.empty()) {
initialize();
}
assert(physicalAddress < RAM_SIZE);
uint64_t frameIndex = physicalAddress / PAGE_SIZE;
uint64_t frameOffset = physicalAddress % PAGE_SIZE;
RAM[frameIndex][frameOffset] = value;
}
void PMevict(uint64_t frameIndex, uint64_t evictedPageIndex) {
if (RAM.empty()) {
initialize();
}
assert(frameIndex < NUM_FRAMES);
assert(evictedPageIndex < NUM_PAGES);
assert(swapFile.find(evictedPageIndex) == swapFile.end());
swapFile[evictedPageIndex] = RAM[frameIndex];
}
void PMrestore(uint64_t frameIndex, uint64_t restoredPageIndex) {
if (RAM.empty()) {
initialize();
}
assert(frameIndex < NUM_FRAMES);
// page is not in swap file, so this is essentially
// the first reference to this page. we can just return
// as it doesn't matter if the page contains garbage
if (swapFile.find(restoredPageIndex) == swapFile.end()) {
return;
}
RAM[frameIndex] = std::move(swapFile[restoredPageIndex]);
swapFile.erase(restoredPageIndex);
}