A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
event-garbage-collector.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INESC Porto
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
7 */
9
10/**
11 * \file
12 * \ingroup core-helpers
13 * \ingroup events
14 * ns3::EventGarbageCollector implementation.
15 */
16
17namespace ns3
18{
19
21 : m_nextCleanupSize(CHUNK_INIT_SIZE),
22 m_events()
23{
24}
25
26void
28{
29 m_events.insert(event);
30 if (m_events.size() >= m_nextCleanupSize)
31 {
32 Cleanup();
33 }
34}
35
36void
41
42void
44{
45 while (m_nextCleanupSize > m_events.size())
46 {
48 }
49 Grow();
50}
51
52// Called when a new event was added and
53// the cleanup limit was exceeded in consequence.
54void
56{
57 for (auto iter = m_events.begin(); iter != m_events.end();)
58 {
59 if ((*iter).IsExpired())
60 {
61 m_events.erase(iter++);
62 }
63 else
64 {
65 break; // EventIds are sorted by timestamp => further events are not expired for sure
66 }
67 }
68
69 // If after cleanup we are still over the limit, increase the limit.
70 if (m_events.size() >= m_nextCleanupSize)
71 {
72 Grow();
73 }
74 else
75 {
76 Shrink();
77 }
78}
79
81{
82 for (const auto& event : m_events)
83 {
84 Simulator::Cancel(event);
85 }
86}
87
88} // namespace ns3
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:45
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:274
ns3::EventGarbageCollector declaration.
Every class exported by the ns3 library is enclosed in the ns3 namespace.