core/util: MemoryMap learns to materialize in files

Change-Id: I003168d3718491524db91b9f7d855d6b3428961c
This commit is contained in:
Horst Schirmeier
2013-04-06 22:05:51 +02:00
parent 467ad88577
commit 4d03b7ce5c
3 changed files with 64 additions and 0 deletions

View File

@ -17,6 +17,7 @@ set(SRCS
gzstream/gzstream.h
Logger.cc
Logger.hpp
MemoryMap.cc
MemoryMap.hpp
ProtoStream.cc
ProtoStream.hpp

View File

@ -0,0 +1,45 @@
#include <fstream>
#include <string>
#include <sstream>
#include "MemoryMap.hpp"
namespace fail {
bool MemoryMap::readFromFile(char const * const filename)
{
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}
std::string buf;
unsigned guest_addr, guest_len;
unsigned count = 0;
while (getline(file, buf)) {
std::stringstream ss(buf, std::ios::in);
ss >> guest_addr >> guest_len;
add(guest_addr, guest_len);
count++;
}
// assertion kept from original code; usually something's fishy if the file
// contains no entries
assert(count > 0);
return true;
}
bool MemoryMap::writeToFile(char const * const filename)
{
std::ofstream file(filename);
if (!file.is_open()) {
return false;
}
for (iterator it = begin(); it != end(); ++it) {
file << *it << "\t1\n";
}
return true;
}
}

View File

@ -73,6 +73,24 @@ public:
* structure.
*/
iterator end() { return as.end(); }
/**
* Loads a memory map from a file and merges it with the current state.
*
* File format (addresses and sizes in decimal):
* \code
* address1<tab>size1
* address2<tab>size2
* ...
* \endcode
*/
bool readFromFile(char const * const filename);
/**
* Saves the map to a file.
*
* Currently all access size information is lost; the map is flattened out
* to a long list of single-byte addresses.
*/
bool writeToFile(char const * const filename);
};
} // end-of-namespace: fail