A Discrete-Event Network Simulator
API
event-garbage-collector.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INESC Porto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
19  */
21 
29 namespace ns3 {
30 
31 
33  : m_nextCleanupSize (CHUNK_INIT_SIZE),
34  m_events ()
35 {}
36 
37 void
39 {
40  m_events.insert (event);
41  if (m_events.size () >= m_nextCleanupSize)
42  {
43  Cleanup ();
44  }
45 }
46 
47 void
49 {
52 }
53 
54 void
56 {
57  while (m_nextCleanupSize > m_events.size ())
58  {
59  m_nextCleanupSize >>= 1;
60  }
61  Grow ();
62 }
63 
64 // Called when a new event was added and
65 // the cleanup limit was exceeded in consequence.
66 void
68 {
69  for (EventList::iterator iter = m_events.begin (); iter != m_events.end ();)
70  {
71  if ((*iter).IsExpired ())
72  {
73  m_events.erase (iter++);
74  }
75  else
76  {
77  break; // EventIds are sorted by timestamp => further events are not expired for sure
78  }
79  }
80 
81  // If after cleanup we are still over the limit, increase the limit.
82  if (m_events.size () >= m_nextCleanupSize)
83  {
84  Grow ();
85  }
86  else
87  {
88  Shrink ();
89  }
90 }
91 
92 
94 {
95  for (auto event : m_events)
96  {
97  Simulator::Cancel (event);
98  }
99 }
100 
101 } // namespace ns3
102 
103 
void Track(EventId event)
Tracks a new event.
const EventList::size_type CHUNK_MAX_SIZE
Threshold to switch from exponential to linear growth in the cleanup frequency.
EventList m_events
The tracked event list.
void Cleanup()
Called when a new event was added and the cleanup limit was exceeded in consequence.
EventList::size_type m_nextCleanupSize
Batch size for cleanup.
void Grow()
Grow the cleanup limit.
void Shrink()
Shrink the cleanup limit Reduce the cleanup size by factors of two until less than the current event ...
An identifier for simulation events.
Definition: event-id.h:54
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:268
ns3::EventGarbageCollector declaration.
Every class exported by the ns3 library is enclosed in the ns3 namespace.