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(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})
# required by Synchronized*.cc: # required by Synchronized*.cc, MemoryMap.* (needs icl, which came with 1.46):
find_package(Boost 1.42 COMPONENTS thread regex system REQUIRED) find_package(Boost 1.46 COMPONENTS thread regex system REQUIRED)
include_directories(${Boost_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS}) link_directories(${Boost_LIBRARY_DIRS})

View File

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

View File

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