--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/helper/event-garbage-collector.cc Tue Jun 18 10:05:15 2013 -0700
@@ -0,0 +1,90 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INESC Porto
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
+ */
+#include "event-garbage-collector.h"
+
+#define CLEANUP_CHUNK_MIN_SIZE 8
+#define CLEANUP_CHUNK_MAX_SIZE 128
+
+
+namespace ns3 {
+
+
+EventGarbageCollector::EventGarbageCollector () :
+ m_nextCleanupSize (CLEANUP_CHUNK_MIN_SIZE)
+{
+}
+
+void
+EventGarbageCollector::Track (EventId event)
+{
+ m_events.insert (event);
+ if (m_events.size () >= m_nextCleanupSize)
+ Cleanup ();
+}
+
+void
+EventGarbageCollector::Grow ()
+{
+ m_nextCleanupSize += (m_nextCleanupSize < CLEANUP_CHUNK_MAX_SIZE ?
+ m_nextCleanupSize : CLEANUP_CHUNK_MAX_SIZE);
+}
+
+void
+EventGarbageCollector::Shrink ()
+{
+ while (m_nextCleanupSize > m_events.size ())
+ m_nextCleanupSize >>= 1;
+ Grow ();
+}
+
+// Called when a new event was added and the cleanup limit was exceeded in consequence.
+void
+EventGarbageCollector::Cleanup ()
+{
+ for (EventList::iterator iter = m_events.begin (); iter != m_events.end ();)
+ {
+ if ((*iter).IsExpired ())
+ {
+ m_events.erase (iter++);
+ }
+ else
+ break; // EventIds are sorted by timestamp => further events are not expired for sure
+ }
+
+ // If after cleanup we are still over the limit, increase the limit.
+ if (m_events.size () >= m_nextCleanupSize)
+ Grow ();
+ else
+ Shrink ();
+}
+
+
+EventGarbageCollector::~EventGarbageCollector ()
+{
+ for (EventList::iterator event = m_events.begin ();
+ event != m_events.end (); event++)
+ {
+ Simulator::Cancel (*event);
+ }
+}
+
+} // namespace ns3
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/helper/event-garbage-collector.h Tue Jun 18 10:05:15 2013 -0700
@@ -0,0 +1,73 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INESC Porto
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
+ */
+#ifndef EVENT_GARBAGE_COLLECTOR_H
+#define EVENT_GARBAGE_COLLECTOR_H
+
+#include <set>
+#include "ns3/event-id.h"
+#include "ns3/simulator.h"
+
+namespace ns3 {
+
+/**
+ * \ingroup tools
+ *
+ * \brief An object that tracks scheduled events and automatically
+ * cancels them when it is destroyed. It is useful in situations
+ * where multiple instances of the same type of event can
+ * simultaneously be scheduled, and when the events should be limited
+ * to the lifetime of a container object.
+ */
+class EventGarbageCollector
+{
+public:
+
+ EventGarbageCollector ();
+
+ /**
+ * \brief Tracks a new event
+ */
+ void Track (EventId event);
+
+ ~EventGarbageCollector ();
+
+private:
+
+ struct EventIdLessThanTs
+ {
+ bool operator () (const EventId &a, const EventId &b) const
+ {
+ return (a.GetTs () < b.GetTs ());
+ }
+ };
+
+ typedef std::multiset<EventId, EventIdLessThanTs> EventList;
+
+ EventList::size_type m_nextCleanupSize;
+ EventList m_events;
+
+ void Cleanup ();
+ void Grow ();
+ void Shrink ();
+};
+
+} // namespace ns3
+
+#endif /* EVENT_GARBAGE_COLLECTOR_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/test/event-garbage-collector-test-suite.cc Tue Jun 18 10:05:15 2013 -0700
@@ -0,0 +1,89 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2007 INESC Porto
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
+ */
+
+#include "ns3/test.h"
+#include "ns3/event-garbage-collector.h"
+
+namespace ns3 {
+
+class EventGarbageCollectorTestCase : public TestCase
+{
+ int m_counter;
+ EventGarbageCollector *m_events;
+
+ void EventGarbageCollectorCallback ();
+
+public:
+
+ EventGarbageCollectorTestCase ();
+ virtual ~EventGarbageCollectorTestCase ();
+ virtual void DoRun (void);
+};
+
+EventGarbageCollectorTestCase::EventGarbageCollectorTestCase ()
+ : TestCase ("EventGarbageCollector"), m_counter (0), m_events (0)
+{
+}
+
+EventGarbageCollectorTestCase::~EventGarbageCollectorTestCase ()
+{
+}
+
+void
+EventGarbageCollectorTestCase::EventGarbageCollectorCallback ()
+{
+ m_counter++;
+ if (m_counter == 50)
+ {
+ // this should cause the remaining (50) events to be cancelled
+ delete m_events;
+ m_events = 0;
+ }
+}
+
+void EventGarbageCollectorTestCase::DoRun (void)
+{
+ m_events = new EventGarbageCollector ();
+
+ for (int n = 0; n < 100; n++)
+ {
+ m_events->Track (Simulator::Schedule
+ (Simulator::Now (),
+ &EventGarbageCollectorTestCase::EventGarbageCollectorCallback,
+ this));
+ }
+ Simulator::Run ();
+ NS_TEST_EXPECT_MSG_EQ (m_events, 0, "");
+ NS_TEST_EXPECT_MSG_EQ (m_counter, 50, "");
+ Simulator::Destroy ();
+}
+
+static class EventGarbageCollectorTestSuite : public TestSuite
+{
+public:
+ EventGarbageCollectorTestSuite ()
+ : TestSuite ("event-garbage-collector", UNIT)
+ {
+ AddTestCase (new EventGarbageCollectorTestCase (), TestCase::QUICK);
+ }
+} g_eventGarbageCollectorTests;
+
+}
+
--- a/src/core/wscript Tue Jun 18 09:46:32 2013 -0700
+++ b/src/core/wscript Tue Jun 18 10:05:15 2013 -0700
@@ -148,6 +148,7 @@
'model/fatal-impl.cc',
'model/system-path.cc',
'helper/random-variable-stream-helper.cc',
+ 'helper/event-garbage-collector.cc',
]
core_test = bld.create_ns3_module_test_library('core')
@@ -162,6 +163,7 @@
'test/object-test-suite.cc',
'test/ptr-test-suite.cc',
'test/random-variable-test-suite.cc',
+ 'test/event-garbage-collector-test-suite.cc',
'test/many-uniform-random-variables-one-get-value-call-test-suite.cc',
'test/one-uniform-random-variable-many-get-value-calls-test-suite.cc',
'test/sample-test-suite.cc',
@@ -247,6 +249,7 @@
'model/system-path.h',
'model/unused.h',
'model/math.h',
+ 'helper/event-garbage-collector.h',
'helper/random-variable-stream-helper.h',
]
--- a/src/tools/model/event-garbage-collector.cc Tue Jun 18 09:46:32 2013 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,90 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2007 INESC Porto
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
- */
-#include "event-garbage-collector.h"
-
-#define CLEANUP_CHUNK_MIN_SIZE 8
-#define CLEANUP_CHUNK_MAX_SIZE 128
-
-
-namespace ns3 {
-
-
-EventGarbageCollector::EventGarbageCollector () :
- m_nextCleanupSize (CLEANUP_CHUNK_MIN_SIZE)
-{
-}
-
-void
-EventGarbageCollector::Track (EventId event)
-{
- m_events.insert (event);
- if (m_events.size () >= m_nextCleanupSize)
- Cleanup ();
-}
-
-void
-EventGarbageCollector::Grow ()
-{
- m_nextCleanupSize += (m_nextCleanupSize < CLEANUP_CHUNK_MAX_SIZE ?
- m_nextCleanupSize : CLEANUP_CHUNK_MAX_SIZE);
-}
-
-void
-EventGarbageCollector::Shrink ()
-{
- while (m_nextCleanupSize > m_events.size ())
- m_nextCleanupSize >>= 1;
- Grow ();
-}
-
-// Called when a new event was added and the cleanup limit was exceeded in consequence.
-void
-EventGarbageCollector::Cleanup ()
-{
- for (EventList::iterator iter = m_events.begin (); iter != m_events.end ();)
- {
- if ((*iter).IsExpired ())
- {
- m_events.erase (iter++);
- }
- else
- break; // EventIds are sorted by timestamp => further events are not expired for sure
- }
-
- // If after cleanup we are still over the limit, increase the limit.
- if (m_events.size () >= m_nextCleanupSize)
- Grow ();
- else
- Shrink ();
-}
-
-
-EventGarbageCollector::~EventGarbageCollector ()
-{
- for (EventList::iterator event = m_events.begin ();
- event != m_events.end (); event++)
- {
- Simulator::Cancel (*event);
- }
-}
-
-} // namespace ns3
-
-
--- a/src/tools/model/event-garbage-collector.h Tue Jun 18 09:46:32 2013 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2007 INESC Porto
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
- */
-#ifndef EVENT_GARBAGE_COLLECTOR_H
-#define EVENT_GARBAGE_COLLECTOR_H
-
-#include <set>
-#include "ns3/event-id.h"
-#include "ns3/simulator.h"
-
-namespace ns3 {
-
-/**
- * \ingroup tools
- *
- * \brief An object that tracks scheduled events and automatically
- * cancels them when it is destroyed. It is useful in situations
- * where multiple instances of the same type of event can
- * simultaneously be scheduled, and when the events should be limited
- * to the lifetime of a container object.
- */
-class EventGarbageCollector
-{
-public:
-
- EventGarbageCollector ();
-
- /**
- * \brief Tracks a new event
- */
- void Track (EventId event);
-
- ~EventGarbageCollector ();
-
-private:
-
- struct EventIdLessThanTs
- {
- bool operator () (const EventId &a, const EventId &b) const
- {
- return (a.GetTs () < b.GetTs ());
- }
- };
-
- typedef std::multiset<EventId, EventIdLessThanTs> EventList;
-
- EventList::size_type m_nextCleanupSize;
- EventList m_events;
-
- void Cleanup ();
- void Grow ();
- void Shrink ();
-};
-
-} // namespace ns3
-
-#endif /* EVENT_GARBAGE_COLLECTOR_H */
--- a/src/tools/test/event-garbage-collector-test-suite.cc Tue Jun 18 09:46:32 2013 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,89 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2007 INESC Porto
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
- */
-
-#include "ns3/test.h"
-#include "ns3/event-garbage-collector.h"
-
-namespace ns3 {
-
-class EventGarbageCollectorTestCase : public TestCase
-{
- int m_counter;
- EventGarbageCollector *m_events;
-
- void EventGarbageCollectorCallback ();
-
-public:
-
- EventGarbageCollectorTestCase ();
- virtual ~EventGarbageCollectorTestCase ();
- virtual void DoRun (void);
-};
-
-EventGarbageCollectorTestCase::EventGarbageCollectorTestCase ()
- : TestCase ("EventGarbageCollector"), m_counter (0), m_events (0)
-{
-}
-
-EventGarbageCollectorTestCase::~EventGarbageCollectorTestCase ()
-{
-}
-
-void
-EventGarbageCollectorTestCase::EventGarbageCollectorCallback ()
-{
- m_counter++;
- if (m_counter == 50)
- {
- // this should cause the remaining (50) events to be cancelled
- delete m_events;
- m_events = 0;
- }
-}
-
-void EventGarbageCollectorTestCase::DoRun (void)
-{
- m_events = new EventGarbageCollector ();
-
- for (int n = 0; n < 100; n++)
- {
- m_events->Track (Simulator::Schedule
- (Simulator::Now (),
- &EventGarbageCollectorTestCase::EventGarbageCollectorCallback,
- this));
- }
- Simulator::Run ();
- NS_TEST_EXPECT_MSG_EQ (m_events, 0, "");
- NS_TEST_EXPECT_MSG_EQ (m_counter, 50, "");
- Simulator::Destroy ();
-}
-
-static class EventGarbageCollectorTestSuite : public TestSuite
-{
-public:
- EventGarbageCollectorTestSuite ()
- : TestSuite ("event-garbage-collector", UNIT)
- {
- AddTestCase (new EventGarbageCollectorTestCase (), TestCase::QUICK);
- }
-} g_eventGarbageCollectorTests;
-
-}
-
--- a/src/tools/wscript Tue Jun 18 09:46:32 2013 -0700
+++ b/src/tools/wscript Tue Jun 18 10:05:15 2013 -0700
@@ -4,7 +4,6 @@
module = bld.create_ns3_module('tools', ['network', 'stats'])
module.source = [
- 'model/event-garbage-collector.cc',
'model/gnuplot.cc',
'model/delay-jitter-estimation.cc',
]
@@ -12,14 +11,12 @@
module_test = bld.create_ns3_module_test_library('tools')
module_test.source = [
'test/average-test-suite.cc',
- 'test/event-garbage-collector-test-suite.cc',
]
headers = bld(features='ns3header')
headers.module = 'tools'
headers.source = [
'model/average.h',
- 'model/event-garbage-collector.h',
'model/gnuplot.h',
'model/delay-jitter-estimation.h',
]