From 0b1ddb45a20fb872b7e9299371b516d5ae5339a0 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 09:50:21 +0200 Subject: [PATCH 1/9] GetEventImpl -> PeekEventImpl --- src/simulator/event-id.cc | 2 +- src/simulator/event-id.h | 2 +- src/simulator/scheduler-heap.cc | 4 ++-- src/simulator/scheduler-list.cc | 4 ++-- src/simulator/scheduler-map.cc | 4 ++-- src/simulator/simulator.cc | 10 +++++----- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index 18f875935..b7b367ab5 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -55,7 +55,7 @@ EventId::IsRunning (void) return !IsExpired (); } EventImpl * -EventId::GetEventImpl (void) const +EventId::PeekEventImpl (void) const { return m_eventImpl; } diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index 4350c9bbe..bbe7e388a 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -51,7 +51,7 @@ public: * they are supposed to be invoked only by * subclasses of the Scheduler base class. */ - EventImpl *GetEventImpl (void) const; + EventImpl *PeekEventImpl (void) const; uint64_t GetTs (void) const; uint32_t GetUid (void) const; private: diff --git a/src/simulator/scheduler-heap.cc b/src/simulator/scheduler-heap.cc index 0339dfed1..9bb08219e 100644 --- a/src/simulator/scheduler-heap.cc +++ b/src/simulator/scheduler-heap.cc @@ -225,7 +225,7 @@ SchedulerHeap::TopDown (uint32_t start) void SchedulerHeap::RealInsert (EventId id) { - EventImpl *event = id.GetEventImpl (); + EventImpl *event = id.PeekEventImpl (); Scheduler::EventKey key; key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); @@ -256,7 +256,7 @@ SchedulerHeap::RealRemove (EventId id) { if (uid == m_heap[i].second.m_uid) { - NS_ASSERT (m_heap[i].first == id.GetEventImpl ()); + NS_ASSERT (m_heap[i].first == id.PeekEventImpl ()); Exch (i, Last ()); m_heap.pop_back (); TopDown (i); diff --git a/src/simulator/scheduler-list.cc b/src/simulator/scheduler-list.cc index ba3960458..2bf9a5de7 100644 --- a/src/simulator/scheduler-list.cc +++ b/src/simulator/scheduler-list.cc @@ -70,7 +70,7 @@ void SchedulerList::RealInsert (EventId id) { Scheduler::EventKey key; - EventImpl *event = id.GetEventImpl (); + EventImpl *event = id.PeekEventImpl (); key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); for (EventsI i = m_events.begin (); i != m_events.end (); i++) @@ -108,7 +108,7 @@ SchedulerList::RealRemove (EventId id) { if (i->second.m_uid == id.GetUid ()) { - NS_ASSERT (id.GetEventImpl () == i->first); + NS_ASSERT (id.PeekEventImpl () == i->first); m_events.erase (i); return true; } diff --git a/src/simulator/scheduler-map.cc b/src/simulator/scheduler-map.cc index 59be61ca7..addf18290 100644 --- a/src/simulator/scheduler-map.cc +++ b/src/simulator/scheduler-map.cc @@ -90,7 +90,7 @@ SchedulerMap::EventKeyCompare::operator () (struct EventKey const&a, struct Even void SchedulerMap::RealInsert (EventId id) { - EventImpl *event = id.GetEventImpl (); + EventImpl *event = id.PeekEventImpl (); Scheduler::EventKey key; key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); @@ -126,7 +126,7 @@ SchedulerMap::RealRemove (EventId id) key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); EventMapI i = m_list.find (key); - NS_ASSERT (i->second == id.GetEventImpl ()); + NS_ASSERT (i->second == id.PeekEventImpl ()); m_list.erase (i); return true; } diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index c133cb5a4..9204f6fdf 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -114,7 +114,7 @@ SimulatorPrivate::~SimulatorPrivate () { while (!m_destroyEvents.empty ()) { - EventImpl *ev = m_destroyEvents.front ().GetEventImpl (); + EventImpl *ev = m_destroyEvents.front ().PeekEventImpl (); m_destroyEvents.pop_front (); TRACE ("handle destroy " << ev); if (!ev->IsCancelled ()) @@ -151,7 +151,7 @@ SimulatorPrivate::ProcessOneEvent (void) { m_log << "e "<Invoke (); delete event; } @@ -275,7 +275,7 @@ SimulatorPrivate::Remove (EventId ev) return; } m_events->Remove (ev); - delete ev.GetEventImpl (); + delete ev.PeekEventImpl (); if (m_logEnable) { @@ -306,11 +306,11 @@ SimulatorPrivate::IsExpired (const EventId ev) } return true; } - if (ev.GetEventImpl () == 0 || + if (ev.PeekEventImpl () == 0 || ev.GetTs () < m_currentTs || (ev.GetTs () == m_currentTs && ev.GetUid () <= m_currentUid) || - ev.GetEventImpl ()->IsCancelled ()) + ev.PeekEventImpl ()->IsCancelled ()) { return true; } From c7906b2d6130240f810f9c491a119e3bc80ac0d1 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 09:53:15 +0200 Subject: [PATCH 2/9] add refcounting to EventImpl --- src/simulator/event-impl.cc | 18 +++++++++++++++++- src/simulator/event-impl.h | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/simulator/event-impl.cc b/src/simulator/event-impl.cc index fb6c7b1ad..e3e10bc53 100644 --- a/src/simulator/event-impl.cc +++ b/src/simulator/event-impl.cc @@ -29,8 +29,24 @@ EventImpl::~EventImpl () {} EventImpl::EventImpl () - : m_cancel (false) + : m_cancel (false), + m_count (1) {} +void +EventImpl::Ref (void) const +{ + m_count++; +} +void +EventImpl::Unref (void) const +{ + m_count--; + if (m_count == 0) + { + delete this; + } +} + void EventImpl::Invoke (void) { diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index fd7d54e2d..c6ff0f5eb 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -28,6 +28,8 @@ namespace ns3 { class EventImpl { public: EventImpl (); + void Ref (void) const; + void Unref (void) const; virtual ~EventImpl () = 0; void Invoke (void); void Cancel (void); @@ -37,6 +39,7 @@ protected: private: friend class Event; bool m_cancel; + mutable uint32_t m_count; }; }; // namespace ns3 From 9d5184bae4539b72f84efb2f200acf8d9163677b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 15:37:05 +0200 Subject: [PATCH 3/9] add an extra constructor to use when the Create template cannot be used --- src/core/ptr.cc | 15 +++++++++++++++ src/core/ptr.h | 21 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/core/ptr.cc b/src/core/ptr.cc index 10e63c606..cee3f59e2 100644 --- a/src/core/ptr.cc +++ b/src/core/ptr.cc @@ -28,9 +28,14 @@ namespace ns3 { +template +void Foo (void) {} + + class NoCount : public Object { public: + NoCount (void (*fn) (void)); NoCount (Callback cb); ~NoCount (); void Nothing (void) const; @@ -292,12 +297,22 @@ PtrTest::RunTests (void) callback (); } + #if 0 // as expected, fails compilation. { Ptr p = Create (cb); Callback callback = MakeCallback (&NoCount::Nothing, p); } + // local types are not allowed as arguments to a template. + { + class B + { + public: + B () {} + }; + Foo (); + } #endif diff --git a/src/core/ptr.h b/src/core/ptr.h index f5a52292e..5d3ce46a6 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -81,7 +81,16 @@ public: * same, so that object is deleted if no more references to it * remain. */ - Ptr (T *ptr); + Ptr (T *ptr); + /** + * \param ptr raw pointer to manage + * \param ref if set to true, this method calls Ref, otherwise, + * it does not call Ref. + * + * Create a smart pointer which points to the object pointed to by + * the input raw pointer ptr. + */ + Ptr (T *ptr, bool ref); Ptr (Ptr const&o); // allow conversions from T to T const. template @@ -378,6 +387,16 @@ Ptr::Ptr (T *ptr) Acquire (); } +template +Ptr::Ptr (T *ptr, bool ref) + : m_ptr (ptr) +{ + if (ref) + { + Acquire (); + } +} + template Ptr::Ptr (Ptr const&o) : m_ptr (PeekPointer (o)) From daa9c3c13c3fdf68a13c2cc7abc41721d7b62548 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 15:37:22 +0200 Subject: [PATCH 4/9] use a Ptr<> to manage EventImpl instances --- src/simulator/event-id.cc | 12 ++--- src/simulator/event-id.h | 8 ++-- src/simulator/scheduler-heap.cc | 11 ++++- src/simulator/scheduler-list.cc | 10 ++++- src/simulator/scheduler-map.cc | 10 ++++- src/simulator/scheduler.h | 2 - src/simulator/simulator.cc | 72 ++++++++++++++++++------------ src/simulator/simulator.h | 78 ++++++++++++++++----------------- 8 files changed, 118 insertions(+), 85 deletions(-) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index b7b367ab5..f25902160 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -30,7 +30,7 @@ EventId::EventId () m_uid (0) {} -EventId::EventId (EventImpl *impl, uint64_t ts, uint32_t uid) +EventId::EventId (Ptr impl, uint64_t ts, uint32_t uid) : m_eventImpl (impl), m_ts (ts), m_uid (uid) @@ -38,11 +38,7 @@ EventId::EventId (EventImpl *impl, uint64_t ts, uint32_t uid) void EventId::Cancel (void) { - if (!IsExpired ()) - { - m_eventImpl->Cancel (); - m_eventImpl = 0; - } + Simulator::Cancel (*this); } bool EventId::IsExpired (void) @@ -54,8 +50,8 @@ EventId::IsRunning (void) { return !IsExpired (); } -EventImpl * -EventId::PeekEventImpl (void) const +Ptr +EventId::GetEventImpl (void) const { return m_eventImpl; } diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index bbe7e388a..3f28d367a 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -22,6 +22,8 @@ #define EVENT_ID_H #include +#include "ns3/ptr.h" +#include "event-impl.h" namespace ns3 { @@ -33,7 +35,7 @@ class EventImpl; class EventId { public: EventId (); - EventId (EventImpl *impl, uint64_t ts, uint32_t uid); + EventId (Ptr impl, uint64_t ts, uint32_t uid); /** * This method is syntactic sugar for the ns3::Simulator::cancel * method. @@ -51,12 +53,12 @@ public: * they are supposed to be invoked only by * subclasses of the Scheduler base class. */ - EventImpl *PeekEventImpl (void) const; + Ptr GetEventImpl (void) const; uint64_t GetTs (void) const; uint32_t GetUid (void) const; private: friend bool operator == (const EventId &a, const EventId &b); - EventImpl *m_eventImpl; + Ptr m_eventImpl; uint64_t m_ts; uint32_t m_uid; }; diff --git a/src/simulator/scheduler-heap.cc b/src/simulator/scheduler-heap.cc index 9bb08219e..5251e3af9 100644 --- a/src/simulator/scheduler-heap.cc +++ b/src/simulator/scheduler-heap.cc @@ -225,7 +225,8 @@ SchedulerHeap::TopDown (uint32_t start) void SchedulerHeap::RealInsert (EventId id) { - EventImpl *event = id.PeekEventImpl (); + // acquire single ref + EventImpl *event = GetPointer (id.GetEventImpl ()); Scheduler::EventKey key; key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); @@ -242,6 +243,9 @@ SchedulerHeap::RealPeekNext (void) const void SchedulerHeap::RealRemoveNext (void) { + std::pair next = m_heap[Root ()]; + // release single ref + next.first->Unref (); Exch (Root (), Last ()); m_heap.pop_back (); TopDown (Root ()); @@ -256,7 +260,10 @@ SchedulerHeap::RealRemove (EventId id) { if (uid == m_heap[i].second.m_uid) { - NS_ASSERT (m_heap[i].first == id.PeekEventImpl ()); + NS_ASSERT (m_heap[i].first == id.GetEventImpl ()); + std::pair next = m_heap[i]; + // release single ref + next.first->Unref (); Exch (i, Last ()); m_heap.pop_back (); TopDown (i); diff --git a/src/simulator/scheduler-list.cc b/src/simulator/scheduler-list.cc index 2bf9a5de7..f96c5e0cd 100644 --- a/src/simulator/scheduler-list.cc +++ b/src/simulator/scheduler-list.cc @@ -70,7 +70,8 @@ void SchedulerList::RealInsert (EventId id) { Scheduler::EventKey key; - EventImpl *event = id.PeekEventImpl (); + // acquire refcount on EventImpl + EventImpl *event = GetPointer (id.GetEventImpl ()); key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); for (EventsI i = m_events.begin (); i != m_events.end (); i++) @@ -98,6 +99,9 @@ SchedulerList::RealPeekNext (void) const void SchedulerList::RealRemoveNext (void) { + std::pair next = m_events.front (); + // release single acquired ref. + next.first->Unref (); m_events.pop_front (); } @@ -108,7 +112,9 @@ SchedulerList::RealRemove (EventId id) { if (i->second.m_uid == id.GetUid ()) { - NS_ASSERT (id.PeekEventImpl () == i->first); + NS_ASSERT (id.GetEventImpl () == i->first); + // release single acquire ref. + i->first->Unref (); m_events.erase (i); return true; } diff --git a/src/simulator/scheduler-map.cc b/src/simulator/scheduler-map.cc index addf18290..29f68dee4 100644 --- a/src/simulator/scheduler-map.cc +++ b/src/simulator/scheduler-map.cc @@ -90,7 +90,8 @@ SchedulerMap::EventKeyCompare::operator () (struct EventKey const&a, struct Even void SchedulerMap::RealInsert (EventId id) { - EventImpl *event = id.PeekEventImpl (); + // acquire a single ref + EventImpl *event = GetPointer (id.GetEventImpl ()); Scheduler::EventKey key; key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); @@ -116,6 +117,9 @@ SchedulerMap::RealPeekNext (void) const void SchedulerMap::RealRemoveNext (void) { + EventMapCI i = m_list.begin (); + // release single ref. + i->second->Unref (); m_list.erase (m_list.begin ()); } @@ -126,7 +130,9 @@ SchedulerMap::RealRemove (EventId id) key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); EventMapI i = m_list.find (key); - NS_ASSERT (i->second == id.PeekEventImpl ()); + NS_ASSERT (i->second == id.GetEventImpl ()); + // release single ref. + i->second->Unref (); m_list.erase (i); return true; } diff --git a/src/simulator/scheduler.h b/src/simulator/scheduler.h index 0df7a77df..194beead2 100644 --- a/src/simulator/scheduler.h +++ b/src/simulator/scheduler.h @@ -27,8 +27,6 @@ namespace ns3 { -class EventImpl; - /** * \brief Maintain the event list * diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 9204f6fdf..5b825fae5 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -23,6 +23,7 @@ #include "scheduler.h" #include "event-impl.h" +#include "ns3/ptr.h" #include "ns3/assert.h" #include "ns3/default-value.h" @@ -61,9 +62,9 @@ public: Time Next (void) const; void Stop (void); void StopAt (Time const &time); - EventId Schedule (Time const &time, EventImpl *event); - EventId ScheduleNow (EventImpl *event); - EventId ScheduleDestroy (EventImpl *event); + EventId Schedule (Time const &time, Ptr event); + EventId ScheduleNow (Ptr event); + EventId ScheduleDestroy (Ptr event); void Remove (EventId ev); void Cancel (EventId &ev); bool IsExpired (EventId ev); @@ -114,13 +115,12 @@ SimulatorPrivate::~SimulatorPrivate () { while (!m_destroyEvents.empty ()) { - EventImpl *ev = m_destroyEvents.front ().PeekEventImpl (); + Ptr ev = m_destroyEvents.front ().GetEventImpl (); m_destroyEvents.pop_front (); TRACE ("handle destroy " << ev); if (!ev->IsCancelled ()) { ev->Invoke (); - delete ev; } } delete m_events; @@ -151,9 +151,8 @@ SimulatorPrivate::ProcessOneEvent (void) { m_log << "e "< event = next.GetEventImpl (); event->Invoke (); - delete event; } bool @@ -204,7 +203,7 @@ SimulatorPrivate::StopAt (Time const &at) m_stopAt = at.GetTimeStep (); } EventId -SimulatorPrivate::Schedule (Time const &time, EventImpl *event) +SimulatorPrivate::Schedule (Time const &time, Ptr event) { NS_ASSERT (time.IsPositive ()); NS_ASSERT (time >= TimeStep (m_currentTs)); @@ -221,7 +220,7 @@ SimulatorPrivate::Schedule (Time const &time, EventImpl *event) return id; } EventId -SimulatorPrivate::ScheduleNow (EventImpl *event) +SimulatorPrivate::ScheduleNow (Ptr event) { EventId id (event, m_currentTs, m_uid); if (m_logEnable) @@ -235,7 +234,7 @@ SimulatorPrivate::ScheduleNow (EventImpl *event) return id; } EventId -SimulatorPrivate::ScheduleDestroy (EventImpl *event) +SimulatorPrivate::ScheduleDestroy (Ptr event) { EventId id (event, m_currentTs, 2); m_destroyEvents.push_back (id); @@ -275,7 +274,7 @@ SimulatorPrivate::Remove (EventId ev) return; } m_events->Remove (ev); - delete ev.PeekEventImpl (); + Cancel (ev); if (m_logEnable) { @@ -288,7 +287,10 @@ SimulatorPrivate::Remove (EventId ev) void SimulatorPrivate::Cancel (EventId &id) { - id.Cancel (); + if (!IsExpired (id)) + { + id.GetEventImpl ()->Cancel (); + } } bool @@ -306,11 +308,11 @@ SimulatorPrivate::IsExpired (const EventId ev) } return true; } - if (ev.PeekEventImpl () == 0 || + if (ev.GetEventImpl () == 0 || ev.GetTs () < m_currentTs || (ev.GetTs () == m_currentTs && ev.GetUid () <= m_currentUid) || - ev.PeekEventImpl ()->IsCancelled ()) + ev.GetEventImpl ()->IsCancelled ()) { return true; } @@ -411,39 +413,39 @@ Simulator::Now (void) return GetPriv ()->Now (); } -EventImpl * +Ptr Simulator::MakeEvent (void (*f) (void)) { // zero arg version class EventFunctionImpl0 : public EventImpl { public: - typedef void (*F)(void); + typedef void (*F)(void); - EventFunctionImpl0 (F function) - : m_function (function) - {} - virtual ~EventFunctionImpl0 () {} + EventFunctionImpl0 (F function) + : m_function (function) + {} + virtual ~EventFunctionImpl0 () {} protected: - virtual void Notify (void) { - (*m_function) (); - } + virtual void Notify (void) { + (*m_function) (); + } private: F m_function; } *ev = new EventFunctionImpl0 (f); - return ev; + return Ptr (ev, false); } EventId -Simulator::Schedule (Time const &time, EventImpl *ev) +Simulator::Schedule (Time const &time, Ptr ev) { return GetPriv ()->Schedule (Now () + time, ev); } EventId -Simulator::ScheduleNow (EventImpl *ev) +Simulator::ScheduleNow (Ptr ev) { return GetPriv ()->ScheduleNow (ev); } EventId -Simulator::ScheduleDestroy (EventImpl *ev) +Simulator::ScheduleDestroy (Ptr ev) { return GetPriv ()->ScheduleDestroy (ev); } @@ -884,6 +886,22 @@ SimulatorTests::RunTests (void) ok = false; } + EventId anId = Simulator::ScheduleNow (&foo0); + EventId anotherId = anId; + if (anId.IsExpired () || anotherId.IsExpired ()) + { + ok = false; + } + Simulator::Remove (anId); + if (!anId.IsExpired () || !anotherId.IsExpired ()) + { + ok = false; + } + + Simulator::Run (); + Simulator::Destroy (); + + return ok; } diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index a7eea7600..b12ba0763 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -570,49 +570,49 @@ private: ~Simulator (); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (void), OBJ obj); + static Ptr MakeEvent (void (T::*mem_ptr) (void), OBJ obj); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1); + static Ptr MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2); + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3); + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4); + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4); template - static EventImpl *MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, + static Ptr MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); - static EventImpl *MakeEvent (void (*f) (void)); + static Ptr MakeEvent (void (*f) (void)); template - static EventImpl *MakeEvent (void (*f) (U1), T1 a1); + static Ptr MakeEvent (void (*f) (U1), T1 a1); template - static EventImpl *MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2); + static Ptr MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2); template - static EventImpl *MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3); + static Ptr MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3); template - static EventImpl *MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4); + static Ptr MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4); template - static EventImpl *MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + static Ptr MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); static SimulatorPrivate *GetPriv (void); - static EventId Schedule (Time const &time, EventImpl *event); - static EventId ScheduleDestroy (EventImpl *event); - static EventId ScheduleNow (EventImpl *event); + static EventId Schedule (Time const &time, Ptr event); + static EventId ScheduleDestroy (Ptr event); + static EventId ScheduleNow (Ptr event); static SimulatorPrivate *m_priv; }; @@ -650,7 +650,7 @@ struct EventMemberImplObjTraits }; template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) { // zero argument version class EventMemberImpl0 : public EventImpl { @@ -667,15 +667,15 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (void), OBJ obj) } OBJ m_obj; F m_function; - } *ev = new EventMemberImpl0 (obj, mem_ptr); - return ev; + } * ev = new EventMemberImpl0 (obj, mem_ptr); + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1) { // one argument version class EventMemberImpl1 : public EventImpl { @@ -696,13 +696,13 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1), OBJ obj, T1 a1) F m_function; typename TypeTraits::ReferencedType m_a1; } *ev = new EventMemberImpl1 (obj, mem_ptr, a1); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 a2) { // two argument version class EventMemberImpl2 : public EventImpl { @@ -726,13 +726,13 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2), OBJ obj, T1 a1, T2 typename TypeTraits::ReferencedType m_a1; typename TypeTraits::ReferencedType m_a2; } *ev = new EventMemberImpl2 (obj, mem_ptr, a1, a2); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, T2 a2, T3 a3) { // three argument version class EventMemberImpl3 : public EventImpl { @@ -758,13 +758,13 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3), OBJ obj, T1 a1, typename TypeTraits::ReferencedType m_a2; typename TypeTraits::ReferencedType m_a3; } *ev = new EventMemberImpl3 (obj, mem_ptr, a1, a2, a3); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4) { // four argument version class EventMemberImpl4 : public EventImpl { @@ -792,13 +792,13 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4), OBJ obj, T1 a typename TypeTraits::ReferencedType m_a3; typename TypeTraits::ReferencedType m_a4; } *ev = new EventMemberImpl4 (obj, mem_ptr, a1, a2, a3, a4); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, +Ptr Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { // five argument version @@ -829,11 +829,11 @@ EventImpl *Simulator::MakeEvent (void (T::*mem_ptr) (U1,U2,U3,U4,U5), OBJ obj, typename TypeTraits::ReferencedType m_a4; typename TypeTraits::ReferencedType m_a5; } *ev = new EventMemberImpl5 (obj, mem_ptr, a1, a2, a3, a4, a5); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1), T1 a1) +Ptr Simulator::MakeEvent (void (*f) (U1), T1 a1) { // one arg version class EventFunctionImpl1 : public EventImpl { @@ -852,12 +852,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1), T1 a1) } F m_function; typename TypeTraits::ReferencedType m_a1; - } *ev = new EventFunctionImpl1(f, a1); - return ev; + } *ev = new EventFunctionImpl1 (f, a1); + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) +Ptr Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) { // two arg version class EventFunctionImpl2 : public EventImpl { @@ -879,12 +879,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2), T1 a1, T2 a2) typename TypeTraits::ReferencedType m_a1; typename TypeTraits::ReferencedType m_a2; } *ev = new EventFunctionImpl2 (f, a1, a2); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3) +Ptr Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3) { // three arg version class EventFunctionImpl3 : public EventImpl { @@ -908,12 +908,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3), T1 a1, T2 a2, T3 a3) typename TypeTraits::ReferencedType m_a2; typename TypeTraits::ReferencedType m_a3; } *ev = new EventFunctionImpl3 (f, a1, a2, a3); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4) +Ptr Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T4 a4) { // four arg version class EventFunctionImpl4 : public EventImpl { @@ -939,12 +939,12 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4), T1 a1, T2 a2, T3 a3, T typename TypeTraits::ReferencedType m_a3; typename TypeTraits::ReferencedType m_a4; } *ev = new EventFunctionImpl4 (f, a1, a2, a3, a4); - return ev; + return Ptr (ev, false); } template -EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) +Ptr Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) { // five arg version class EventFunctionImpl5 : public EventImpl { @@ -972,7 +972,7 @@ EventImpl *Simulator::MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3 typename TypeTraits::ReferencedType m_a4; typename TypeTraits::ReferencedType m_a5; } *ev = new EventFunctionImpl5 (f, a1, a2, a3, a4, a5); - return ev; + return Ptr (ev, false); } template From 21be3e3912657e8b830d042d96b55509ee7472e9 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 17:18:14 +0200 Subject: [PATCH 5/9] optimize EventImpl refcounting --- src/simulator/event-id.cc | 5 +++++ src/simulator/event-id.h | 1 + src/simulator/event-impl.cc | 15 --------------- src/simulator/event-impl.h | 23 +++++++++++++++++++++-- src/simulator/scheduler-heap.cc | 15 +++++++-------- src/simulator/scheduler-heap.h | 12 ++++++------ src/simulator/scheduler-list.cc | 15 +++++++-------- src/simulator/scheduler-list.h | 12 ++++++------ src/simulator/scheduler-map.cc | 15 +++++++-------- src/simulator/scheduler-map.h | 10 +++++----- src/simulator/scheduler.cc | 29 ----------------------------- src/simulator/scheduler.h | 30 +++++++++++------------------- src/simulator/simulator.cc | 5 ++--- 13 files changed, 78 insertions(+), 109 deletions(-) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index f25902160..43491505d 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -55,6 +55,11 @@ EventId::GetEventImpl (void) const { return m_eventImpl; } +EventImpl * +EventId::PeekEventImpl (void) const +{ + return PeekPointer (m_eventImpl); +} uint64_t EventId::GetTs (void) const { diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index 3f28d367a..9432a5d4b 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -54,6 +54,7 @@ public: * subclasses of the Scheduler base class. */ Ptr GetEventImpl (void) const; + EventImpl *PeekEventImpl (void) const; uint64_t GetTs (void) const; uint32_t GetUid (void) const; private: diff --git a/src/simulator/event-impl.cc b/src/simulator/event-impl.cc index e3e10bc53..9c89bb52f 100644 --- a/src/simulator/event-impl.cc +++ b/src/simulator/event-impl.cc @@ -32,21 +32,6 @@ EventImpl::EventImpl () : m_cancel (false), m_count (1) {} -void -EventImpl::Ref (void) const -{ - m_count++; -} -void -EventImpl::Unref (void) const -{ - m_count--; - if (m_count == 0) - { - delete this; - } -} - void EventImpl::Invoke (void) { diff --git a/src/simulator/event-impl.h b/src/simulator/event-impl.h index c6ff0f5eb..51ac1e7a1 100644 --- a/src/simulator/event-impl.h +++ b/src/simulator/event-impl.h @@ -28,8 +28,8 @@ namespace ns3 { class EventImpl { public: EventImpl (); - void Ref (void) const; - void Unref (void) const; + inline void Ref (void) const; + inline void Unref (void) const; virtual ~EventImpl () = 0; void Invoke (void); void Cancel (void); @@ -44,4 +44,23 @@ private: }; // namespace ns3 +namespace ns3 { + +void +EventImpl::Ref (void) const +{ + m_count++; +} +void +EventImpl::Unref (void) const +{ + m_count--; + if (m_count == 0) + { + delete this; + } +} + +} // namespace ns3 + #endif /* EVENT_IMPL_H */ diff --git a/src/simulator/scheduler-heap.cc b/src/simulator/scheduler-heap.cc index 5251e3af9..fb62e248a 100644 --- a/src/simulator/scheduler-heap.cc +++ b/src/simulator/scheduler-heap.cc @@ -170,7 +170,7 @@ SchedulerHeap::Smallest (uint32_t a, uint32_t b) const } bool -SchedulerHeap::RealIsEmpty (void) const +SchedulerHeap::IsEmpty (void) const { return (m_heap.size () == 1)?true:false; } @@ -223,7 +223,7 @@ SchedulerHeap::TopDown (uint32_t start) void -SchedulerHeap::RealInsert (EventId id) +SchedulerHeap::Insert (const EventId &id) { // acquire single ref EventImpl *event = GetPointer (id.GetEventImpl ()); @@ -235,25 +235,24 @@ SchedulerHeap::RealInsert (EventId id) } EventId -SchedulerHeap::RealPeekNext (void) const +SchedulerHeap::PeekNext (void) const { std::pair next = m_heap[Root ()]; return EventId (next.first, next.second.m_ts, next.second.m_uid); } -void -SchedulerHeap::RealRemoveNext (void) +EventId +SchedulerHeap::RemoveNext (void) { std::pair next = m_heap[Root ()]; - // release single ref - next.first->Unref (); Exch (Root (), Last ()); m_heap.pop_back (); TopDown (Root ()); + return EventId (Ptr (next.first, false), next.second.m_ts, next.second.m_uid); } bool -SchedulerHeap::RealRemove (EventId id) +SchedulerHeap::Remove (const EventId &id) { uint32_t uid = id.GetUid (); for (uint32_t i = 1; i < m_heap.size (); i++) diff --git a/src/simulator/scheduler-heap.h b/src/simulator/scheduler-heap.h index c57fe1368..fbc6e1ca1 100644 --- a/src/simulator/scheduler-heap.h +++ b/src/simulator/scheduler-heap.h @@ -35,13 +35,13 @@ public: SchedulerHeap (); virtual ~SchedulerHeap (); -private: - virtual void RealInsert (EventId id); - virtual bool RealIsEmpty (void) const; - virtual EventId RealPeekNext (void) const; - virtual void RealRemoveNext (void); - virtual bool RealRemove (EventId ev); + virtual void Insert (const EventId &id); + virtual bool IsEmpty (void) const; + virtual EventId PeekNext (void) const; + virtual EventId RemoveNext (void); + virtual bool Remove (const EventId &ev); +private: typedef std::vector > BinaryHeap; inline uint32_t Parent (uint32_t id) const; diff --git a/src/simulator/scheduler-list.cc b/src/simulator/scheduler-list.cc index f96c5e0cd..4c85713c7 100644 --- a/src/simulator/scheduler-list.cc +++ b/src/simulator/scheduler-list.cc @@ -67,7 +67,7 @@ SchedulerList::IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b } void -SchedulerList::RealInsert (EventId id) +SchedulerList::Insert (const EventId &id) { Scheduler::EventKey key; // acquire refcount on EventImpl @@ -85,28 +85,27 @@ SchedulerList::RealInsert (EventId id) m_events.push_back (std::make_pair (event, key)); } bool -SchedulerList::RealIsEmpty (void) const +SchedulerList::IsEmpty (void) const { return m_events.empty (); } EventId -SchedulerList::RealPeekNext (void) const +SchedulerList::PeekNext (void) const { std::pair next = m_events.front (); return EventId (next.first, next.second.m_ts, next.second.m_uid); } -void -SchedulerList::RealRemoveNext (void) +EventId +SchedulerList::RemoveNext (void) { std::pair next = m_events.front (); - // release single acquired ref. - next.first->Unref (); m_events.pop_front (); + return EventId (Ptr (next.first,false), next.second.m_ts, next.second.m_uid); } bool -SchedulerList::RealRemove (EventId id) +SchedulerList::Remove (const EventId &id) { for (EventsI i = m_events.begin (); i != m_events.end (); i++) { diff --git a/src/simulator/scheduler-list.h b/src/simulator/scheduler-list.h index cbce5e68f..306d19270 100644 --- a/src/simulator/scheduler-list.h +++ b/src/simulator/scheduler-list.h @@ -37,13 +37,13 @@ class SchedulerList : public Scheduler { SchedulerList (); virtual ~SchedulerList (); - private: - virtual void RealInsert (EventId id); - virtual bool RealIsEmpty (void) const; - virtual EventId RealPeekNext (void) const; - virtual void RealRemoveNext (void); - virtual bool RealRemove (EventId ev); + virtual void Insert (const EventId &id); + virtual bool IsEmpty (void) const; + virtual EventId PeekNext (void) const; + virtual EventId RemoveNext (void); + virtual bool Remove (const EventId &ev); + private: inline bool IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const; typedef std::list > Events; diff --git a/src/simulator/scheduler-map.cc b/src/simulator/scheduler-map.cc index 29f68dee4..5e002a4a7 100644 --- a/src/simulator/scheduler-map.cc +++ b/src/simulator/scheduler-map.cc @@ -88,7 +88,7 @@ SchedulerMap::EventKeyCompare::operator () (struct EventKey const&a, struct Even void -SchedulerMap::RealInsert (EventId id) +SchedulerMap::Insert (const EventId &id) { // acquire a single ref EventImpl *event = GetPointer (id.GetEventImpl ()); @@ -101,30 +101,29 @@ SchedulerMap::RealInsert (EventId id) } bool -SchedulerMap::RealIsEmpty (void) const +SchedulerMap::IsEmpty (void) const { return m_list.empty (); } EventId -SchedulerMap::RealPeekNext (void) const +SchedulerMap::PeekNext (void) const { EventMapCI i = m_list.begin (); NS_ASSERT (i != m_list.end ()); return EventId (i->second, i->first.m_ts, i->first.m_uid); } -void -SchedulerMap::RealRemoveNext (void) +EventId +SchedulerMap::RemoveNext (void) { EventMapCI i = m_list.begin (); - // release single ref. - i->second->Unref (); m_list.erase (m_list.begin ()); + return EventId (Ptr (i->second, false), i->first.m_ts, i->first.m_uid); } bool -SchedulerMap::RealRemove (EventId id) +SchedulerMap::Remove (const EventId &id) { Scheduler::EventKey key; key.m_ts = id.GetTs (); diff --git a/src/simulator/scheduler-map.h b/src/simulator/scheduler-map.h index b8fb2cfa5..dbbc7fdbd 100644 --- a/src/simulator/scheduler-map.h +++ b/src/simulator/scheduler-map.h @@ -36,12 +36,12 @@ public: SchedulerMap (); virtual ~SchedulerMap (); + virtual void Insert (const EventId &id); + virtual bool IsEmpty (void) const; + virtual EventId PeekNext (void) const; + virtual EventId RemoveNext (void); + virtual bool Remove (const EventId &ev); private: - virtual void RealInsert (EventId id); - virtual bool RealIsEmpty (void) const; - virtual EventId RealPeekNext (void) const; - virtual void RealRemoveNext (void); - virtual bool RealRemove (EventId ev); class EventKeyCompare { public: diff --git a/src/simulator/scheduler.cc b/src/simulator/scheduler.cc index f8190bb39..4ba1d01b6 100644 --- a/src/simulator/scheduler.cc +++ b/src/simulator/scheduler.cc @@ -27,33 +27,4 @@ namespace ns3 { Scheduler::~Scheduler () {} -void -Scheduler::Insert (EventId id) -{ - return RealInsert (id); -} -bool -Scheduler::IsEmpty (void) const -{ - return RealIsEmpty (); -} -EventId -Scheduler::PeekNext (void) const -{ - NS_ASSERT (!RealIsEmpty ()); - return RealPeekNext (); -} -void -Scheduler::RemoveNext (void) -{ - NS_ASSERT (!RealIsEmpty ()); - return RealRemoveNext (); -} -bool -Scheduler::Remove (EventId id) -{ - NS_ASSERT (!RealIsEmpty ()); - return RealRemove (id); -} - }; // namespace ns3 diff --git a/src/simulator/scheduler.h b/src/simulator/scheduler.h index 194beead2..33917a870 100644 --- a/src/simulator/scheduler.h +++ b/src/simulator/scheduler.h @@ -33,13 +33,12 @@ namespace ns3 { * This base class specifies the interface used to maintain the * event list. If you want to provide a new event list scheduler, * you need to create a subclass of this base class and implement - * all the private pure virtual methods defined here. Namely: - * - ns3::Scheduler::realInsert - * - ns3::Scheduler::realIsEmpty - * - ns3::Scheduler::realPeekNext - * - ns3::Scheduler::realPeekNextKey - * - ns3::Scheduler::realRemoveNext - * - ns3::Scheduler::realRemove + * all the pure virtual methods defined here. Namely: + * - ns3::Scheduler::Insert + * - ns3::Scheduler::IsEmpty + * - ns3::Scheduler::PeekNext + * - ns3::Scheduler::RemoveNext + * - ns3::Scheduler::Remove * * If you need to provide a new event list scheduler without * editing the main simulator class, you need to also implement @@ -58,13 +57,6 @@ class Scheduler { virtual ~Scheduler () = 0; - void Insert (EventId id); - bool IsEmpty (void) const; - EventId PeekNext (void) const; - void RemoveNext (void); - bool Remove (EventId); - -private: /** * \param event event to store in the event list * \param key timecode associated to this new event @@ -72,23 +64,23 @@ private: * * This method takes ownership of the event pointer. */ - virtual void RealInsert (EventId id) = 0; + virtual void Insert (const EventId &id) = 0; /** * \returns true if the event list is empty and false otherwise. */ - virtual bool RealIsEmpty (void) const = 0; + virtual bool IsEmpty (void) const = 0; /** * \returns a pointer to the next earliest event. The caller * takes ownership of the returned pointer. * * This method cannot be invoked if the list is empty. */ - virtual EventId RealPeekNext (void) const = 0; + virtual EventId PeekNext (void) const = 0; /** * This method cannot be invoked if the list is empty. * Remove the next earliest event from the event list. */ - virtual void RealRemoveNext (void) = 0; + virtual EventId RemoveNext (void) = 0; /** * \param id the id of the event to remove * \param key the timecode of the event removed @@ -97,7 +89,7 @@ private: * * This methods cannot be invoked if the list is empty. */ - virtual bool RealRemove (EventId id) = 0; + virtual bool Remove (const EventId &id) = 0; }; }; // namespace ns3 diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 5b825fae5..83e8b1d45 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -138,8 +138,7 @@ SimulatorPrivate::EnableLogTo (char const *filename) void SimulatorPrivate::ProcessOneEvent (void) { - EventId next = m_events->PeekNext (); - m_events->RemoveNext (); + EventId next = m_events->RemoveNext (); NS_ASSERT (next.GetTs () >= m_currentTs); --m_unscheduledEvents; @@ -151,7 +150,7 @@ SimulatorPrivate::ProcessOneEvent (void) { m_log << "e "< event = next.GetEventImpl (); + EventImpl *event = next.PeekEventImpl (); event->Invoke (); } From dcf642ffb0e9511fca0b3439075212b52399f1f5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 17:28:17 +0200 Subject: [PATCH 6/9] more optimizations --- src/simulator/event-id.cc | 2 +- src/simulator/event-id.h | 2 +- src/simulator/simulator.cc | 18 +++++++++--------- src/simulator/simulator.h | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index 43491505d..fa6ee84fa 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -30,7 +30,7 @@ EventId::EventId () m_uid (0) {} -EventId::EventId (Ptr impl, uint64_t ts, uint32_t uid) +EventId::EventId (const Ptr &impl, uint64_t ts, uint32_t uid) : m_eventImpl (impl), m_ts (ts), m_uid (uid) diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index 9432a5d4b..14023c4c9 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -35,7 +35,7 @@ class EventImpl; class EventId { public: EventId (); - EventId (Ptr impl, uint64_t ts, uint32_t uid); + EventId (const Ptr &impl, uint64_t ts, uint32_t uid); /** * This method is syntactic sugar for the ns3::Simulator::cancel * method. diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 83e8b1d45..2e2e39347 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -62,9 +62,9 @@ public: Time Next (void) const; void Stop (void); void StopAt (Time const &time); - EventId Schedule (Time const &time, Ptr event); - EventId ScheduleNow (Ptr event); - EventId ScheduleDestroy (Ptr event); + EventId Schedule (Time const &time, const Ptr &event); + EventId ScheduleNow (const Ptr &event); + EventId ScheduleDestroy (const Ptr &event); void Remove (EventId ev); void Cancel (EventId &ev); bool IsExpired (EventId ev); @@ -202,7 +202,7 @@ SimulatorPrivate::StopAt (Time const &at) m_stopAt = at.GetTimeStep (); } EventId -SimulatorPrivate::Schedule (Time const &time, Ptr event) +SimulatorPrivate::Schedule (Time const &time, const Ptr &event) { NS_ASSERT (time.IsPositive ()); NS_ASSERT (time >= TimeStep (m_currentTs)); @@ -219,7 +219,7 @@ SimulatorPrivate::Schedule (Time const &time, Ptr event) return id; } EventId -SimulatorPrivate::ScheduleNow (Ptr event) +SimulatorPrivate::ScheduleNow (const Ptr &event) { EventId id (event, m_currentTs, m_uid); if (m_logEnable) @@ -233,7 +233,7 @@ SimulatorPrivate::ScheduleNow (Ptr event) return id; } EventId -SimulatorPrivate::ScheduleDestroy (Ptr event) +SimulatorPrivate::ScheduleDestroy (const Ptr &event) { EventId id (event, m_currentTs, 2); m_destroyEvents.push_back (id); @@ -434,17 +434,17 @@ Simulator::MakeEvent (void (*f) (void)) return Ptr (ev, false); } EventId -Simulator::Schedule (Time const &time, Ptr ev) +Simulator::Schedule (Time const &time, const Ptr &ev) { return GetPriv ()->Schedule (Now () + time, ev); } EventId -Simulator::ScheduleNow (Ptr ev) +Simulator::ScheduleNow (const Ptr &ev) { return GetPriv ()->ScheduleNow (ev); } EventId -Simulator::ScheduleDestroy (Ptr ev) +Simulator::ScheduleDestroy (const Ptr &ev) { return GetPriv ()->ScheduleDestroy (ev); } diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index b12ba0763..ecd0550bd 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -610,9 +610,9 @@ private: static Ptr MakeEvent (void (*f) (U1,U2,U3,U4,U5), T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); static SimulatorPrivate *GetPriv (void); - static EventId Schedule (Time const &time, Ptr event); - static EventId ScheduleDestroy (Ptr event); - static EventId ScheduleNow (Ptr event); + static EventId Schedule (Time const &time, const Ptr &event); + static EventId ScheduleDestroy (const Ptr &event); + static EventId ScheduleNow (const Ptr &event); static SimulatorPrivate *m_priv; }; From 0b1819cdc8762ee133f833d3605479979a5a6d12 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 17:36:14 +0200 Subject: [PATCH 7/9] be more consistant in the handling of references --- src/simulator/simulator.cc | 18 +++++++++--------- src/simulator/simulator.h | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 2e2e39347..39afc093c 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -65,9 +65,9 @@ public: EventId Schedule (Time const &time, const Ptr &event); EventId ScheduleNow (const Ptr &event); EventId ScheduleDestroy (const Ptr &event); - void Remove (EventId ev); - void Cancel (EventId &ev); - bool IsExpired (EventId ev); + void Remove (const EventId &ev); + void Cancel (const EventId &ev); + bool IsExpired (const EventId &ev); void Run (void); Time Now (void) const; @@ -253,7 +253,7 @@ SimulatorPrivate::Now (void) const } void -SimulatorPrivate::Remove (EventId ev) +SimulatorPrivate::Remove (const EventId &ev) { if (ev.GetUid () == 2) { @@ -284,7 +284,7 @@ SimulatorPrivate::Remove (EventId ev) } void -SimulatorPrivate::Cancel (EventId &id) +SimulatorPrivate::Cancel (const EventId &id) { if (!IsExpired (id)) { @@ -293,7 +293,7 @@ SimulatorPrivate::Cancel (EventId &id) } bool -SimulatorPrivate::IsExpired (const EventId ev) +SimulatorPrivate::IsExpired (const EventId &ev) { if (ev.GetUid () == 2) { @@ -466,18 +466,18 @@ Simulator::ScheduleDestroy (void (*f) (void)) void -Simulator::Remove (EventId ev) +Simulator::Remove (const EventId &ev) { return GetPriv ()->Remove (ev); } void -Simulator::Cancel (EventId &ev) +Simulator::Cancel (const EventId &ev) { return GetPriv ()->Cancel (ev); } bool -Simulator::IsExpired (EventId id) +Simulator::IsExpired (const EventId &id) { return GetPriv ()->IsExpired (id); } diff --git a/src/simulator/simulator.h b/src/simulator/simulator.h index ecd0550bd..9c9cd201e 100644 --- a/src/simulator/simulator.h +++ b/src/simulator/simulator.h @@ -534,7 +534,7 @@ public: * * @param id the event to remove from the list of scheduled events. */ - static void Remove (EventId id); + static void Remove (const EventId &id); /** * Set the cancel bit on this event: the event's associated function * will not be invoked when it expires. @@ -547,7 +547,7 @@ public: * * @param id the event to cancel */ - static void Cancel (EventId &id); + static void Cancel (const EventId &id); /** * This method has O(1) complexity. * Note that it is not possible to test for the expiration of @@ -560,7 +560,7 @@ public: * @param id the event to test for expiration * @returns true if the event has expired, false otherwise. */ - static bool IsExpired (const EventId id); + static bool IsExpired (const EventId &id); /** * Return the "current simulation time". */ From c3e1f137e1f8fe47658d82760bdf30f94225f3e7 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 17:44:15 +0200 Subject: [PATCH 8/9] remove GetEventImpl --- src/simulator/event-id.cc | 5 ----- src/simulator/event-id.h | 1 - src/simulator/scheduler-heap.cc | 5 +++-- src/simulator/scheduler-list.cc | 5 +++-- src/simulator/scheduler-map.cc | 5 +++-- src/simulator/simulator.cc | 8 ++++---- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/simulator/event-id.cc b/src/simulator/event-id.cc index fa6ee84fa..548634b59 100644 --- a/src/simulator/event-id.cc +++ b/src/simulator/event-id.cc @@ -50,11 +50,6 @@ EventId::IsRunning (void) { return !IsExpired (); } -Ptr -EventId::GetEventImpl (void) const -{ - return m_eventImpl; -} EventImpl * EventId::PeekEventImpl (void) const { diff --git a/src/simulator/event-id.h b/src/simulator/event-id.h index 14023c4c9..e080499fd 100644 --- a/src/simulator/event-id.h +++ b/src/simulator/event-id.h @@ -53,7 +53,6 @@ public: * they are supposed to be invoked only by * subclasses of the Scheduler base class. */ - Ptr GetEventImpl (void) const; EventImpl *PeekEventImpl (void) const; uint64_t GetTs (void) const; uint32_t GetUid (void) const; diff --git a/src/simulator/scheduler-heap.cc b/src/simulator/scheduler-heap.cc index fb62e248a..f924c70f4 100644 --- a/src/simulator/scheduler-heap.cc +++ b/src/simulator/scheduler-heap.cc @@ -226,7 +226,8 @@ void SchedulerHeap::Insert (const EventId &id) { // acquire single ref - EventImpl *event = GetPointer (id.GetEventImpl ()); + EventImpl *event = id.PeekEventImpl (); + event->Ref (); Scheduler::EventKey key; key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); @@ -259,7 +260,7 @@ SchedulerHeap::Remove (const EventId &id) { if (uid == m_heap[i].second.m_uid) { - NS_ASSERT (m_heap[i].first == id.GetEventImpl ()); + NS_ASSERT (m_heap[i].first == id.PeekEventImpl ()); std::pair next = m_heap[i]; // release single ref next.first->Unref (); diff --git a/src/simulator/scheduler-list.cc b/src/simulator/scheduler-list.cc index 4c85713c7..c3de7891a 100644 --- a/src/simulator/scheduler-list.cc +++ b/src/simulator/scheduler-list.cc @@ -71,7 +71,8 @@ SchedulerList::Insert (const EventId &id) { Scheduler::EventKey key; // acquire refcount on EventImpl - EventImpl *event = GetPointer (id.GetEventImpl ()); + EventImpl *event = id.PeekEventImpl (); + event->Ref (); key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); for (EventsI i = m_events.begin (); i != m_events.end (); i++) @@ -111,7 +112,7 @@ SchedulerList::Remove (const EventId &id) { if (i->second.m_uid == id.GetUid ()) { - NS_ASSERT (id.GetEventImpl () == i->first); + NS_ASSERT (id.PeekEventImpl () == i->first); // release single acquire ref. i->first->Unref (); m_events.erase (i); diff --git a/src/simulator/scheduler-map.cc b/src/simulator/scheduler-map.cc index 5e002a4a7..f413ed830 100644 --- a/src/simulator/scheduler-map.cc +++ b/src/simulator/scheduler-map.cc @@ -91,7 +91,8 @@ void SchedulerMap::Insert (const EventId &id) { // acquire a single ref - EventImpl *event = GetPointer (id.GetEventImpl ()); + EventImpl *event = id.PeekEventImpl (); + event->Ref (); Scheduler::EventKey key; key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); @@ -129,7 +130,7 @@ SchedulerMap::Remove (const EventId &id) key.m_ts = id.GetTs (); key.m_uid = id.GetUid (); EventMapI i = m_list.find (key); - NS_ASSERT (i->second == id.GetEventImpl ()); + NS_ASSERT (i->second == id.PeekEventImpl ()); // release single ref. i->second->Unref (); m_list.erase (i); diff --git a/src/simulator/simulator.cc b/src/simulator/simulator.cc index 39afc093c..8190aeb79 100644 --- a/src/simulator/simulator.cc +++ b/src/simulator/simulator.cc @@ -115,7 +115,7 @@ SimulatorPrivate::~SimulatorPrivate () { while (!m_destroyEvents.empty ()) { - Ptr ev = m_destroyEvents.front ().GetEventImpl (); + Ptr ev = m_destroyEvents.front ().PeekEventImpl (); m_destroyEvents.pop_front (); TRACE ("handle destroy " << ev); if (!ev->IsCancelled ()) @@ -288,7 +288,7 @@ SimulatorPrivate::Cancel (const EventId &id) { if (!IsExpired (id)) { - id.GetEventImpl ()->Cancel (); + id.PeekEventImpl ()->Cancel (); } } @@ -307,11 +307,11 @@ SimulatorPrivate::IsExpired (const EventId &ev) } return true; } - if (ev.GetEventImpl () == 0 || + if (ev.PeekEventImpl () == 0 || ev.GetTs () < m_currentTs || (ev.GetTs () == m_currentTs && ev.GetUid () <= m_currentUid) || - ev.GetEventImpl ()->IsCancelled ()) + ev.PeekEventImpl ()->IsCancelled ()) { return true; } From 64a8418674337d1fa889ebbe226806186cf00731 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 27 Jul 2007 17:49:04 +0200 Subject: [PATCH 9/9] optmize slightly Ptr::Acquire --- src/core/ptr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/ptr.h b/src/core/ptr.h index 5d3ce46a6..f2436dd63 100644 --- a/src/core/ptr.h +++ b/src/core/ptr.h @@ -65,7 +65,7 @@ private: template friend U *PeekPointer (const Ptr &p); - void Acquire (void) const; + inline void Acquire (void) const; public: /** * Create an empty smart pointer