util: space-efficient MemoryMap

We now use boost::icl::interval_set internally, consuming extremely
lower amounts of memory.  boost::icl was introduced with Boost 1.46;
Debian 7.0 comes with 1.49, so this dependency should be no problem
anymore.

Both the class interface and the memory-map file format stay the same.

Change-Id: I38e8148384c90aa493984d0f6280817df00f1702
This commit is contained in:
Horst Schirmeier
2014-01-30 13:40:12 +01:00
parent 119ae40be9
commit 4bcce14659
3 changed files with 61 additions and 33 deletions

View File

@ -37,8 +37,8 @@ find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# required by Synchronized*.cc:
find_package(Boost 1.42 COMPONENTS thread regex system REQUIRED)
# required by Synchronized*.cc, MemoryMap.* (needs icl, which came with 1.46):
find_package(Boost 1.46 COMPONENTS thread regex system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

View File

@ -37,9 +37,12 @@ bool MemoryMap::writeToFile(char const * const filename)
return false;
}
for (iterator it = begin(); it != end(); ++it) {
file << *it << "\t1\n";
#ifndef __puma
for (address_set::iterator it = as.begin();
it != as.end(); ++it) {
file << it->lower() << " " << (it->upper() - it->lower() + 1) << "\n";
}
#endif
return true;
}

View File

@ -1,12 +1,13 @@
#ifndef __MEMORYMAP_HPP__
#define __MEMORYMAP_HPP__
#ifdef BOOST_1_46_OR_NEWER
#ifndef __puma
#include <boost/icl/discrete_interval.hpp>
#include <boost/icl/closed_interval.hpp>
#include <boost/icl/interval_set.hpp>
using namespace boost::icl;
#endif
#include <set>
#include <ostream>
#include "sal/SALConfig.hpp"
@ -17,62 +18,78 @@ namespace fail {
* An efficient container for memory maps with holes.
*/
class MemoryMap {
#ifndef __puma
private:
#ifdef BOOST_1_46_OR_NEWER
typedef interval<address_t>::type address_interval;
typedef interval_set<address_t>::type address_set;
typedef boost::icl::discrete_interval<address_t>::type address_interval;
typedef boost::icl::interval_set<address_t>::type address_set;
address_set as;
public:
MemoryMap() { }
void clear() { as.clear(); }
void add(address_t addr, int size) { as.add(address_interval(addr, addr+size-1)); }
void isMatching(address_t addr, int size) { return intersects(as, address_interval(addr, addr+size-1)); }
/**
* The (STL-style) iterator of this class used to iterate over all
* addresses in this map.
*/
typedef address_set::element_iterator iterator;
#else
public:
typedef int const* iterator;
#endif
std::set<address_t> as;
public:
MemoryMap() { }
/**
* Clears the map.
*/
void clear() { as.clear(); }
void clear()
{
#ifndef __puma
as.clear();
#endif
}
/**
* Adds one or a sequence of addresses to the map.
*/
void add(address_t addr, int size = 1)
{
for (int i = 0; i < size; ++i) {
as.insert(addr + i);
}
#ifndef __puma
as.add(boost::icl::construct<address_interval>(addr, addr + size - 1, boost::icl::interval_bounds::closed()));
#endif
}
/**
* Determines whether a given memory access at address \a addr with width
* \a size hits the map.
*/
bool isMatching(address_t addr, int size = 1)
{
for (int i = 0; i < size; ++i) {
if (as.find(addr + i) != as.end()) {
return true;
}
}
return false;
#ifndef __puma
return boost::icl::intersects(as, boost::icl::construct<address_interval>(addr, addr + size - 1, boost::icl::interval_bounds::closed()));
#endif
}
/**
* The (STL-style) iterator of this class used to iterate over all
* addresses in this map.
*/
typedef std::set<address_t>::iterator iterator;
/**
* Returns an (STL-style) iterator to the beginning of the internal data
* structure.
*/
iterator begin() { return as.begin(); }
iterator begin()
{
#ifndef __puma
return boost::icl::elements_begin(as);
#endif
}
/**
* Returns an (STL-style) iterator to the end of the interal data
* structure.
*/
iterator end() { return as.end(); }
iterator end()
{
#ifndef __puma
return boost::icl::elements_end(as);
#endif
}
/**
* Loads a memory map from a file and merges it with the current state.
*
@ -91,6 +108,14 @@ public:
* to a long list of single-byte addresses.
*/
bool writeToFile(char const * const filename);
// debugging
void dump(std::ostream& os)
{
#ifndef __puma
os << as << std::endl;
#endif
}
};
} // end-of-namespace: fail