include Windows include iostream include functions using namespace std

  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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <Windows.h>
#include <iostream>
#include "functions.h"
using namespace std;
const int PAGELIMIT = 30000; // max pages count
char* lpPtr; // current using page
DWORD dwPtr; // address in page
char* lpNextPage; // next page to be used
char* lpBase; //
int dwPages = 0; // current pages count < pagelimit
DWORD dwPageSize; //
DWORD rPhysMem = 0; //
INT PageFaultExceptionFilter(DWORD dwCode) {
LPVOID lpvResult;
// If the exception is not a page fault, exit.
if (dwCode != EXCEPTION_ACCESS_VIOLATION) {
cout << "Exception code = " << dwCode << endl;
return EXCEPTION_EXECUTE_HANDLER;
}
if (dwPages >= PAGELIMIT) {
cout << ("Exception: out of pages.\n");
cin.get();
return EXCEPTION_EXECUTE_HANDLER;
}
// Otherwise, commit another page.
lpvResult = VirtualAlloc(
(LPVOID) lpNextPage, // Next page to commit
dwPageSize, // Page size, in bytes
MEM_COMMIT, // Allocate a committed page
PAGE_READWRITE); // Read/write access
if (lpvResult == NULL) {
cout << ("VirtualAlloc failed") << endl;
return EXCEPTION_EXECUTE_HANDLER;
} // else allocate another page
// Increment the page count, and advance lpNxtPage to the next page.
dwPages++;
VirtualLock(lpNextPage, dwPageSize);
lpNextPage = (char*) (lpNextPage + dwPageSize);
// Continue execution where the page fault occurred.
return EXCEPTION_CONTINUE_EXECUTION;
}
VOID ErrorExit(LPTSTR lpMsg) {
cout << "Error! " << lpMsg << " with error code of " << GetLastError() << endl;
exit (0);
}
void memoryinit(bool Physical) {
// Take page size from system info stuct
SYSTEM_INFO a;
GetSystemInfo(&a);
dwPageSize = a.dwPageSize;
// Init
dwPages = 0;
lpNextPage = 0;
// 10.3 Task
rPhysMem = Physical;
lpBase = (char*) VirtualAlloc(NULL, PAGELIMIT * dwPageSize, MEM_RESERVE, PAGE_NOACCESS);
if (lpBase == NULL) ErrorExit(TEXT("VirtualAlloc reserve failed."));
// Set new values
lpNextPage = lpBase;
lpPtr = lpBase;
dwPtr = 0;
}
void* memoryget(int size) {
// Try fill zero in mem
__try {
for (int i = 0; i < size; i++) {
lpPtr[dwPtr + i] = 0;
}
}
// If there's a page fault, commit another page and try again
__except (PageFaultExceptionFilter(GetExceptionCode())) {
cout << ("Exiting process.\n");
ExitProcess( GetLastError() );
}
// Set new values
DWORD k = dwPtr;
dwPtr += size;
return (void*)(lpPtr + k);
}
void memoryfree(void* address) {
// 10.3 part
VirtualUnlock(lpBase, dwPages * dwPageSize);
// Free
BOOL bSuccess = VirtualFree(lpBase, 0, MEM_RELEASE);
cout << "Release " << (bSuccess ? "succeeded" : "failed") << endl;
}
List::List(bool PhysicalMem) {
First = 0;
Last = 0;
memoryinit(PhysicalMem);
}
List::~List() {
listItem *k = First;
memoryfree(k);
}
listItem* List::getFirst() {
return First;
}
listItem* List::getLast() {
return Last;
}
listItem* List::insertFirst(int val) {
listItem *k = (listItem*) memoryget(sizeof(listItem));
if (!k) {
cout << "Error allocating memory." << endl;
return k;
}
if (First) {
k->next = First;
k->prev = 0;
k->val = val;
First->prev = k;
First = k;
} else {
k->next = 0;
k->prev = 0;
k->val = val;
First = k;
Last = k;
}
return k;
}
listItem* List::insertLast(int val) {
listItem *k = (listItem*) memoryget(sizeof(listItem));
if (!k) {
cout << "Error allocating memory." << endl;
return k;
}
if (Last) {
k->prev = Last;
k->next = 0;
k->val = val;
Last->next = k;
Last = k;
} else {
k->next = 0;
k->prev = 0;
k->val = val;
Last = k;
First = k;
}
return k;
}
listItem* List::insertAfter(listItem* lWhere, int val) {
listItem *k = (listItem*) memoryget(sizeof(listItem));
if (!k) {
cout << "Error allocating memory." << endl;
return k;
}
k->prev = lWhere;
k->next = lWhere->next;
k->val = val;
lWhere->next->prev = k;
lWhere->next = k;
return k;
}
void List::removeFirst() {
if (!First) return;
listItem* k = First;
if (k->next) {
First = k->next;
First->prev = 0;
} else {
First = 0;
Last = 0;
}
}
void List::removeLast() {
if (!Last) return;
listItem* k = Last;
if (k->prev) {
Last = k->prev;
Last->next = 0;
} else {
First = 0;
Last = 0;
}
}
void List::print() {
cout << endl << "List:" << endl;
cout << "[Begin]" << endl;
listItem *k = First;
while (k) {
cout << k->val << " ";
k = k->next;
}
cout << endl << "[End]" << endl;
}