-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
84 lines (78 loc) · 2.03 KB
/
main.cpp
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
#include "treeObject.h"
#include "tokenizer.h"
#include <map>
#include <vector>
#include <iostream>
#include <math.h>
#include <string>
#include <stdlib.h>
using std::map;
using std::vector;
using std::cout;
using std::endl;
using std::cin;
using std::string;
void execute(string line, MemoryManager &memManager) {
string funcName = parseFunction(line);
if (funcName.compare("alloc") == 0) {
int pid;
UNIT size = getAllocParam(line, &pid);
if (!size) {
cout << "Parse parameters error: " << line << endl;
} else {
if (!memManager.alloc(pid, size)) {
cout << "No space for process " << pid << endl;
}
}
} else if (funcName.compare("realloc") == 0) {
int pid;
UNIT size = getReallocParam(line, &pid);
if (!size) {
cout << "Parse parameters error: " << line << endl;
} else {
if (!memManager.realloc(pid, size)) {
cout << "No space for process " << pid << endl;
}
}
} else if (funcName.compare("free") == 0) {
int pid = getFreeParam(line);
if (pid == -1) {
cout << "Parse parameters error: " << line << endl;
} else {
if (!memManager.free(pid)) {
cout << "Free: couldn't find process " << pid << endl;
}
}
} else if (funcName.compare("dump") == 0) {
memManager.dump();
} else {
cout << "Can not parse command: " << line << endl;
}
}
int main() {
// Read input
vector<string> data;
string line;
while (getline(cin, line)) {
data.push_back(line);
}
// Parse first 2 lines to get memory and slab description
int portion;
UNIT total = getMemorySize(data[0]);
if (!total) {
cout << "Read total memory size error!" << endl;
exit(EXIT_FAILURE);
}
UNIT slabSize = getSlabInfo(&portion, data[1]);
if (!slabSize) {
cout << "Read slab size error!" << endl;
exit(EXIT_FAILURE);
}
// Initialize memory manager
MemoryManager memManager(total, portion, slabSize);
// Execute the instructions
for (size_t i = 2; i < data.size(); i++) {
execute(data[i], memManager);
}
return 0;
}