src/contrib/event-garbage-collector.h
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu, 06 Dec 2007 11:05:17 +0000
changeset 1878 f947cd18f0bb
parent 1778 src/routing/olsr/event-garbage-collector.h@ae7c637dfa8e
permissions -rw-r--r--
Create a new contrib module; move event-garbage-collector.{h,cc} to the contrib module.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1671
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     2
/*
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
 * Copyright (c) 2007 INESC Porto
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
 *
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License version 2 as
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     7
 * published by the Free Software Foundation;
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
 *
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    12
 * GNU General Public License for more details.
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    13
 *
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
 *
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
 * Author: Gustavo J. A. M. Carneiro  <gjc@inescporto.pt>
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
 */
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    20
#ifndef EVENT_GARBAGE_COLLECTOR_H
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    21
#define EVENT_GARBAGE_COLLECTOR_H
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
1769
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    23
#include <set>
1768
1664a9db93b9 Make EventGarbageCollector private to the olsr module; adapt olsr to Timer API changes.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1671
diff changeset
    24
#include "ns3/event-id.h"
1664a9db93b9 Make EventGarbageCollector private to the olsr module; adapt olsr to Timer API changes.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1671
diff changeset
    25
#include "ns3/simulator.h"
1671
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    26
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    27
namespace ns3 {
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    28
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    29
/**
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    30
 * \brief An object that tracks scheduled events and automatically
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    31
 * cancels them when it is destroyed.  It is useful in situations
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    32
 * where multiple instances of the same type of event can
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    33
 * simultaneously be scheduled, and when the events should be limited
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    34
 * to the lifetime of a container object.
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    35
 */
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    36
class EventGarbageCollector
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    37
{
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
public:
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    40
  EventGarbageCollector ();
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    41
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
  /**
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    43
   * \brief Tracks a new event
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    44
   */
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    45
  void Track (EventId event);
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    46
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    47
  ~EventGarbageCollector ();
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    48
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    49
private:
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    50
1769
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    51
  struct EventIdLessThanTs
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    52
  {
1770
6d15f1dbfe71 Add a missing const.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1769
diff changeset
    53
    bool operator () (const EventId &a, const EventId &b) const
1769
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    54
    {
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    55
      return (a.GetTs () < b.GetTs ());
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    56
    }
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    57
  };
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    58
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    59
  typedef std::multiset<EventId, EventIdLessThanTs> EventList;
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    60
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    61
  EventList::size_type m_nextCleanupSize;
0ca4a39dca8b Optimize EventGarbageCollector by using std::multiset with EventIds sorted by expiration time.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 1768
diff changeset
    62
  EventList m_events;
1671
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
  void Cleanup ();
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    65
  void Grow ();
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    66
  void Shrink ();
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    67
};
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    68
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    69
}; // namespace ns3
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    70
878a9e02bcd6 Event Garbage Collector
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    71
#endif /* EVENT_GARBAGE_COLLECTOR_H */