-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
127 lines (100 loc) · 2.78 KB
/
utils.cpp
File metadata and controls
127 lines (100 loc) · 2.78 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
#include "utils.h"
#include <algorithm>
#include <sstream>
#include <fstream>
#include <iostream>
#include <set>
#include <cstdlib>
using namespace std;
void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
if(trim(item) != "")
elems.push_back(item);
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
long long hex2l(string s) {
long long ans = 0;
long long p = 1;
for(int i = s.length() - 1; i >= 0; --i) {
if(s[i] == 'x')
break;
int k;
if(s[i] <= '9' && s[i] >= '0')
k = s[i] - '0';
else if(s[i] >= 'a' && s[i] <= 'f')
k = s[i] - 'a' + 10;
else
k = 0;
ans += p * k;
p *= 16;
}
return ans;
}
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
void get_memory_references(string file_name, vector< MemoryReference > &references) {
ifstream fin(file_name.c_str());
references.clear();
// vector< pair<long long, int> > references;
string s;
while(getline(fin, s)) {
if(trim(s)[0] == '#')
continue;
vector<string> tokens = split(s, ' ');
long long addr = hex2l(tokens[2]);
int count = atoi(tokens[3].c_str());
MemoryReference::AccessType accessType = tokens[1] == "W" ? MemoryReference::WRITE : MemoryReference::READ;
MemoryReference mr(addr, count, accessType);
references.push_back( mr );
}
}
int needed_pages(int page_size, const vector< MemoryReference > &references) {
set<long long> bytes;
for(int i = 0; i < references.size(); ++i) {
long long addr = references[i].address;
int count = references[i].bytes;
for(int i = 0; i < count; ++i)
bytes.insert((addr + count) / page_size);
}
return bytes.size();
}
int get_page_size(string s) {
string temp = s.substr(0, s.length() - 1);
int ans = atoi(temp.c_str());
if(s[s.length() - 1] == 'k')
ans *= 1024;
else
ans *= 1024 * 1024;
return ans;
}
int get_number_of_frames(int number_of_pages, string number_of_frames) {
int ans;
if(number_of_frames[ number_of_frames.length() - 1 ] == '%') {
string tmp = number_of_frames.substr(0, number_of_frames.length() - 1);
ans = atoi(tmp.c_str());
ans = number_of_pages * ans / 100;
} else
ans = atoi(number_of_frames.c_str());
return ans;
}