-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpages.cpp
More file actions
206 lines (138 loc) · 4.81 KB
/
pages.cpp
File metadata and controls
206 lines (138 loc) · 4.81 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "pages.h"
#include <cstdlib>
PageListNode *PageList::searchForSpace() {
if (this->startNode == nullptr) return nullptr; // no nodes inserted yet
PageListNode *current = this->startNode;
while (! current->isLast()){ // traverse the list
if (current->hasSpace()){
return current;
}
current = current->getNext();
}
if (current->hasSpace()) { // also check the last one
return current;
}else{
return nullptr;
}
}
PageListNode *PageList::addPageNode(Address startAddr, int pageLength) {
// a normal add-to-front function
PageListNode *copyNode = this->startNode;
PageListNode *newNode = new PageListNode(startAddr, pageLength, this);
newNode->setNext(copyNode);
if (copyNode) copyNode->setPrev(newNode);
this->startNode = newNode;
return newNode;
}
PageList::~PageList() { //--DOESNT MATTER - NEVER USED
if (this->startNode == nullptr) return;
PageListNode *current = this->startNode;
// go to last
while (! current->isLast()){
current = current->getNext();
}
if (current == this->startNode){
delete current;
return;
}
current = current->getPrev();
// remove in backwards
while (! current->isFirst()){
current = current->getPrev();
delete current->getNext();
}
delete current;
}
void PageList::movePageNodeToBeginning(PageListNode *node) {
// link your left node with your right
if (! node->isFirst()) node->getPrev()->setNext( node->getNext() );
if (! node->isLast() ) node->getNext()->setPrev( node->getPrev() );
// link node with previously first node
node->setNext( this->startNode );
if (this->startNode != nullptr) this->startNode->setPrev( node );
// set node as starting node
this->startNode = node;
}
PageListNode::PageListNode(Address startAddr, int pageLength, PageList *pageListBelonging){
this->prev = nullptr;
this->next = nullptr;
this->startingAddr = startAddr;
this->endingAddr = startAddr + pageLength;
this->nObjects = pageLength / pageListBelonging->getObjectsSize();
this->myPageList = pageListBelonging;
}
bool PageListNode::belongsToMe(Address addr) {
return (addr >= this->startingAddr && addr < this->endingAddr);
}
bool PageListNode::markFree(Address addr) {
// which object (ie 1st, 2nd, ...) of the node this pointer actually is
int index = (int) (addr - this->startingAddr) / this->getPageListBelonging()->getObjectsSize();
if (this->bitVector.isFree(index)){
return false; // double free error
}
this->bitVector.markFree(index);
return true;
}
bool PageListNode::hasSpace() {
for (int i=0; i<this->nObjects; i++){
if (this->bitVector.isFree(i)){
return true;
}
}
return false;
}
Address PageListNode::occupySpace() {
for (int i=0; i<this->nObjects; i++){
if (this->bitVector.isFree(i)){
this->bitVector.markOccupied(i);
return (Address) (startingAddr + i*this->getPageListBelonging()->getObjectsSize());
}
}
#pragma GCC diagnostic ignored "-Wconversion-null"
return NULL;
}
bool PageListNode::isCompletelyFree() {
for (int i=0; i<this->nObjects; i++){
if (this->bitVector.isOccupied(i)){
return false;
}
}
return true;
}
bool PageListNode::isValidObjectAddress(Address addr) {
int offset = (int) (addr - this->startingAddr);
return (offset < 4096) && (offset % this->getPageListBelonging()->getObjectsSize() == 0);
}
int PageListNode::findpositionOfObjectInPage(Address addr){
int offset = (int) (addr - this->startingAddr);
return offset / this->getPageListBelonging()->getObjectsSize();
}
BitVector::BitVector() {
int i;
for (i=0; i<16; i++){
this->vector[i] = 0b00000000;
}
}
bool BitVector::isFree(int index) {
int cellIndex = index/8;
_8bit cell = this->vector[cellIndex];
int exactPosition = index % 8;
_8bit mask = 1 << exactPosition; // mask: 00001000 (if exactPosition = 3)
_8bit result = cell & mask; // xxxxYxxx AND 00001000 = 0000Y000
return result == 0b00000000;
}
void BitVector::markFree(int index) {
int cellIndex = index/8;
_8bit cell = this->vector[cellIndex];
int exactPosition = index % 8;
_8bit mask = 1 << exactPosition; // mask: (ie) 00100000
mask = 0b11111111 ^ mask; // 11111111 XOR 00100000 = 11011111
this->vector[cellIndex] = cell & mask; // xxYxxxxx AND 11011111 = xx0xxxxx
}
void BitVector::markOccupied(int index) {
int cellIndex = index/8;
_8bit cell = this->vector[cellIndex];
int exactPosition = index % 8;
_8bit mask = 1 << exactPosition; // mask: (ie) 00010000
this->vector[cellIndex] = cell | mask; // xxxYxxxx OR 00010000 = xxx1xxxx
}