[core] Refactor EventGarbageCollector

This commit is contained in:
Peter D. Barnes, Jr.
2017-09-17 21:32:48 -07:00
parent acba76fba1
commit 19d1ef51d2
2 changed files with 43 additions and 31 deletions

View File

@@ -19,15 +19,19 @@
*/
#include "event-garbage-collector.h"
#define CLEANUP_CHUNK_MIN_SIZE 8
#define CLEANUP_CHUNK_MAX_SIZE 128
/**
* \file
* \ingroup core-helpers
* \ingroup events
* ns3::EventGarbageCollector implementation.
*/
namespace ns3 {
EventGarbageCollector::EventGarbageCollector () :
m_nextCleanupSize (CLEANUP_CHUNK_MIN_SIZE)
EventGarbageCollector::EventGarbageCollector ()
: m_nextCleanupSize (CHUNK_INIT_SIZE),
m_events ()
{
}
@@ -42,8 +46,8 @@ EventGarbageCollector::Track (EventId event)
void
EventGarbageCollector::Grow ()
{
m_nextCleanupSize += (m_nextCleanupSize < CLEANUP_CHUNK_MAX_SIZE ?
m_nextCleanupSize : CLEANUP_CHUNK_MAX_SIZE);
m_nextCleanupSize += (m_nextCleanupSize < CHUNK_MAX_SIZE ?
m_nextCleanupSize : CHUNK_MAX_SIZE);
}
void
@@ -54,7 +58,8 @@ EventGarbageCollector::Shrink ()
Grow ();
}
// Called when a new event was added and the cleanup limit was exceeded in consequence.
// Called when a new event was added and
// the cleanup limit was exceeded in consequence.
void
EventGarbageCollector::Cleanup ()
{
@@ -78,10 +83,9 @@ EventGarbageCollector::Cleanup ()
EventGarbageCollector::~EventGarbageCollector ()
{
for (EventList::iterator event = m_events.begin ();
event != m_events.end (); event++)
for (auto event : m_events)
{
Simulator::Cancel (*event);
Simulator::Cancel (event);
}
}

View File

@@ -24,10 +24,18 @@
#include "ns3/event-id.h"
#include "ns3/simulator.h"
/**
* \file
* \ingroup events
* \ingroup core-helpers
* ns3::EventGarbageCollector declaration.
*/
namespace ns3 {
/**
* \ingroup events
* \ingroup core-helpers
*
* \brief An object that tracks scheduled events and automatically
* cancels them when it is destroyed. It is useful in situations
@@ -43,44 +51,44 @@ public:
/**
* \brief Tracks a new event
* \param [in] event the Event to track
*/
void Track (EventId event);
~EventGarbageCollector ();
private:
/**
* \brief comparison operator for std::multiset
*/
struct EventIdLessThanTs
{
/**
* \brief comparison operator for std::multiset
*/
bool operator () (const EventId &a, const EventId &b) const
{
return (a.GetTs () < b.GetTs ());
}
};
/** Event list container */
typedef std::multiset<EventId, EventIdLessThanTs> EventList;
typedef std::multiset<EventId> EventList;
EventList::size_type m_nextCleanupSize; //!< batch size for cleanup
EventList m_events; //!< the tracked event list
/** Initial threshold for cleaning the event list. */
const typename EventList::size_type CHUNK_INIT_SIZE = 8;
/**
* Threshold to switch from exponential to linear growth
* in the cleanup frequency.
*/
const typename EventList::size_type CHUNK_MAX_SIZE = 128;
EventList::size_type m_nextCleanupSize; //!< Batch size for cleanup
EventList m_events; //!< The tracked event list
/**
* \brief called when a new event was added and the cleanup limit was
* \brief Called when a new event was added and the cleanup limit was
* exceeded in consequence.
*/
void Cleanup ();
/**
* \brief grow the cleanup limit
* \brief Grow the cleanup limit.
* Increase the cleanup size by the smaller of
* the current cleanup size (exponential growth),
* or the CHUNK_MAX_SIZE (linear growth).
*/
void Grow ();
/**
* \brief shrink the cleanup limit
* \brief Shrink the cleanup limit
* Reduce the cleanup size by factors of two until less than the
* current event list, then Grow one step.
*/
void Shrink ();
};