-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
48 lines (39 loc) · 1.95 KB
/
config.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
/*
Parses the config file, creates and returns a Cache object.
*/
#include "config.hpp"
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include "replacement_policies.h"
using namespace Config;
/*Helper function, used to get the replacement policy. */
static ReplacementPolicy Config::getRP(std::string RP){
std::map<std::string, ReplacementPolicy> ReplacementPolicyMap;
ReplacementPolicyMap["RANDOM"]=RANDOM;
ReplacementPolicyMap["FIFO"]=FIFO;
ReplacementPolicyMap["LRU"]=LRU;
ReplacementPolicy policy = ReplacementPolicyMap[RP];
return policy;
}
/*Parses config file, creates and returns a Cache object.*/
Cache Config::configureCache(){
std::map<std::string, std::string> cacheSettings;
std::string line="",property="", value;
std::ifstream file;
file.open("cache.conf");
while(!file.eof()){
getline(file,line); //Gets one instruction line, consisting of cache property and value.
line.erase(remove_if(line.begin(), line.end(), (int(*)(int))isspace), line.end());
std::istringstream ss(line);
if(line[0]!='\n' && line[0]!='#' && getline(ss,property,'=')){ //Splits line into 2 tokens, property and value.
if(getline(ss,value)) cacheSettings[property]=value;//Creates a map of cache properties and their corresponding values.
}
}
/*Create cache object as specified by the config file.*/
//Cache cache = Cache(stoi((cacheSettings["CACHE_SIZE"])), stoi((cacheSettings["ASSOCIATIVITY"])), stoi((cacheSettings["BANKS"])), stoi((cacheSettings["NUMBER_CACHE_LINES"])),stoi((cacheSettings["NUMER_DATA_BLOCKS"])), Config::getRP(cacheSettings["REPLACEMENT_POLICY"]));
Cache cache = Cache(stoi((cacheSettings["CACHE_SIZE"])), stoi((cacheSettings["ASSOCIATIVITY"])), stoi((cacheSettings["BANKS"])),stoi((cacheSettings["NUMER_DATA_BLOCKS"])), Config::getRP(cacheSettings["REPLACEMENT_POLICY"]));
return cache;
}