core/util: MemoryMap learns to materialize in files
Change-Id: I003168d3718491524db91b9f7d855d6b3428961c
This commit is contained in:
@ -17,6 +17,7 @@ set(SRCS
|
|||||||
gzstream/gzstream.h
|
gzstream/gzstream.h
|
||||||
Logger.cc
|
Logger.cc
|
||||||
Logger.hpp
|
Logger.hpp
|
||||||
|
MemoryMap.cc
|
||||||
MemoryMap.hpp
|
MemoryMap.hpp
|
||||||
ProtoStream.cc
|
ProtoStream.cc
|
||||||
ProtoStream.hpp
|
ProtoStream.hpp
|
||||||
|
|||||||
45
src/core/util/MemoryMap.cc
Normal file
45
src/core/util/MemoryMap.cc
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -73,6 +73,24 @@ public:
|
|||||||
* structure.
|
* structure.
|
||||||
*/
|
*/
|
||||||
iterator end() { return as.end(); }
|
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
|
} // end-of-namespace: fail
|
||||||
|
|||||||
Reference in New Issue
Block a user