# HG changeset patch # User Mathieu Lacage # Date 1178814832 -7200 # Node ID 6fb98941c36f219418234cc55f2354f026e82bc0 # Parent a4ef066d1185b52d9d9af1a1a34cecd6715dbce4 remove leaks and rework the Ptr class to work with a new refcount mechanism diff -r a4ef066d1185 -r 6fb98941c36f examples/simple-p2p.cc --- a/examples/simple-p2p.cc Thu May 10 09:57:46 2007 +0200 +++ b/examples/simple-p2p.cc Thu May 10 18:33:52 2007 +0200 @@ -137,7 +137,7 @@ // Create the OnOff application to send UDP datagrams of size // 210 bytes at a rate of 448 Kb/s - OnOffApplication* ooff0 = new OnOffApplication( + Ptr ooff0 = new OnOffApplication( n0, Ipv4Address("10.1.3.2"), 80, @@ -154,10 +154,9 @@ // Start the application ooff0->Start(Seconds(1.0)); ooff0->Stop (Seconds(10.0)); - ooff0->Unref (); // Create a similar flow from n3 to n1, starting at time 1.1 seconds - OnOffApplication* ooff1 = new OnOffApplication( + Ptr ooff1 = new OnOffApplication( n3, Ipv4Address("10.1.2.1"), 80, @@ -172,7 +171,6 @@ // Start the application ooff1->Start(Seconds(1.1)); ooff1->Stop (Seconds(10.0)); - ooff1->Unref (); // Here, finish off packet routing configuration // This will likely set by some global StaticRouting object in the future diff -r a4ef066d1185 -r 6fb98941c36f samples/main-simple.cc --- a/samples/main-simple.cc Thu May 10 09:57:46 2007 +0200 +++ b/samples/main-simple.cc Thu May 10 18:33:52 2007 +0200 @@ -35,9 +35,10 @@ socket->RecvDummy (MakeCallback (&SocketPrinter)); } -int main (int argc, char *argv[]) +void +RunSimulation (void) { - InternetNode *a = new InternetNode (); + Ptr a = new InternetNode (); IUdp *udp; udp = a->QueryInterface (IUdp::iid); @@ -60,7 +61,13 @@ sink->Unref (); source->Unref (); - a->Unref (); + + std::cout << "o" << std::endl; +} + +int main (int argc, char *argv[]) +{ + RunSimulation (); return 0; } diff -r a4ef066d1185 -r 6fb98941c36f src/applications/application-list.cc --- a/src/applications/application-list.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/applications/application-list.cc Thu May 10 18:33:52 2007 +0200 @@ -35,12 +35,12 @@ void ApplicationList::DoDispose (void) { - for (std::vector::const_iterator i = m_apps.begin(); + for (std::vector >::iterator i = m_apps.begin(); i != m_apps.end(); ++i) { - Application *app = *i; + Ptr app = *i; app->Dispose (); - app->Unref (); + *i = 0; } m_apps.clear (); NsUnknown::DoDispose (); @@ -50,9 +50,8 @@ {} void -ApplicationList::Add(Application* a) +ApplicationList::Add(Ptr a) { - a->Ref (); m_apps.push_back(a); } @@ -61,9 +60,12 @@ return m_apps.size(); } -Application* ApplicationList::Get(uint32_t i) const -{ // Get the i'th application. Note, this is linear time in N - if (m_apps.empty()) return 0; // List is empty +Ptr ApplicationList::Get(uint32_t i) const +{ + if (m_apps.empty()) + { + return 0; + } return m_apps[i]; } diff -r a4ef066d1185 -r 6fb98941c36f src/applications/application-list.h --- a/src/applications/application-list.h Thu May 10 09:57:46 2007 +0200 +++ b/src/applications/application-list.h Thu May 10 18:33:52 2007 +0200 @@ -26,6 +26,7 @@ #include "application.h" #include "ns3/ns-unknown.h" +#include "ns3/ptr.h" #include namespace ns3 { @@ -37,23 +38,15 @@ ApplicationList(Ptr); // Copy constructor not needed, default one is correct virtual ~ApplicationList(); - // Inherited from Capabilty - virtual void Add(Application*); // Add an already new'ed app - // Manage the list - template T* AddCopy(const T& t) // Add a new application - { - T* a = t.Copy(); - m_apps.push_back(a); - return a; - } - void Remove(Application*); // Application has finished + virtual void Add(Ptr application); + uint32_t Count() const; // Number of applications - Application* Get(uint32_t i) const; // Get app by index + Ptr Get(uint32_t i) const; // Get app by index protected: virtual void DoDispose (void); private: - std::vector m_apps; + std::vector > m_apps; }; }//namespace ns3 diff -r a4ef066d1185 -r 6fb98941c36f src/core/ns-unknown.cc --- a/src/core/ns-unknown.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/core/ns-unknown.cc Thu May 10 18:33:52 2007 +0200 @@ -65,9 +65,10 @@ }; NsUnknownImpl::NsUnknownImpl (Iid iid, NsUnknown *interface) - : m_ref (1), + : m_ref (0), m_disposed (false) { + NS_DEBUG ("new " << this << " ref=" << m_ref); m_list.push_back (std::make_pair (iid, interface)); } NsUnknownImpl::~NsUnknownImpl () @@ -89,10 +90,12 @@ NsUnknownImpl::RefAll (NsUnknownImpl *other) { m_ref += other->m_ref; + NS_DEBUG ("inc all " << this << " o=" << other->m_ref << " ref=" << m_ref); } void NsUnknownImpl::Unref (void) { + NS_ASSERT (m_ref > 0); m_ref--; NS_DEBUG ("dec " << this << " ref=" << m_ref); if (m_ref == 0) @@ -103,8 +106,10 @@ void NsUnknownImpl::UnrefAll (void) { + NS_ASSERT (m_ref > 0); m_ref = 0; delete this; + NS_DEBUG ("dec all " << this); } void NsUnknownImpl::DoDisposeAll (void) @@ -159,14 +164,15 @@ NsUnknown::~NsUnknown () { m_impl = 0; + m_ref = -1; } void -NsUnknown::Ref (void) +NsUnknown::Ref (void) const { m_impl->Ref (); } void -NsUnknown::Unref (void) +NsUnknown::Unref (void) const { m_impl->Unref (); } diff -r a4ef066d1185 -r 6fb98941c36f src/core/ns-unknown.h --- a/src/core/ns-unknown.h Thu May 10 09:57:46 2007 +0200 +++ b/src/core/ns-unknown.h Thu May 10 18:33:52 2007 +0200 @@ -49,8 +49,8 @@ { public: virtual ~NsUnknown (); - void Ref (void); - void Unref (void); + void Ref (void) const; + void Unref (void) const; /** * \param iid the NsUnknown id of the requested interface diff -r a4ef066d1185 -r 6fb98941c36f src/core/object.cc --- a/src/core/object.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/core/object.cc Thu May 10 18:33:52 2007 +0200 @@ -27,9 +27,11 @@ namespace ns3 { Object::Object () - : m_count (1), + : m_count (0), m_disposed (false) -{} +{ + NS_DEBUG ("Object::Object: m_count=0"); +} Object::~Object () {} @@ -37,17 +39,16 @@ void Object::Ref (void) const { - NS_DEBUG("Object::Ref (): this == 0x" << this); m_count++; - NS_DEBUG("Object::Ref (): m_count bumped to " << m_count); + NS_DEBUG("Object::Ref (): this == 0x" << this << " m_count=" << m_count); } void Object::Unref (void) const { - NS_DEBUG("Object::Unref (): this == 0x" << this); + NS_ASSERT (m_count > 0); m_count--; - NS_DEBUG("Object::Ref (): m_count dropped to " << m_count); + NS_DEBUG("Object::Unref (): this == 0x" << this << " m_count=" << m_count); if (m_count == 0) { diff -r a4ef066d1185 -r 6fb98941c36f src/core/ptr.cc --- a/src/core/ptr.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/core/ptr.cc Thu May 10 18:33:52 2007 +0200 @@ -264,6 +264,22 @@ { ok = false; } + + { + Ptr p0 = new NoCount (cb); + Ptr p1 = new NoCount (cb); + if (p0 == p1) + { + ok = false; + } + if (p0 != p1) + { + } + else + { + ok = false; + } + } return ok; diff -r a4ef066d1185 -r 6fb98941c36f src/core/ptr.h --- a/src/core/ptr.h Thu May 10 09:57:46 2007 +0200 +++ b/src/core/ptr.h Thu May 10 18:33:52 2007 +0200 @@ -51,6 +51,7 @@ void operator delete (void *); }; friend class Ptr; + void Acquire (void) const; public: /** * Create an empty smart pointer @@ -109,6 +110,13 @@ template inline friend bool operator != (T1 const *lhs, Ptr &rhs); + // allow if (sp0 == sp1) + template + inline friend bool operator == (Ptr const &lhs, Ptr const &rhs); + // allow if (sp0 != sp1) + template + inline friend bool operator != (Ptr const &lhs, Ptr const &rhs); + template inline friend Ptr const_pointer_cast (Ptr const&p); @@ -116,6 +124,16 @@ }; template +void +Ptr::Acquire (void) const +{ + if (m_ptr != 0) + { + m_ptr->Ref (); + } +} + +template Ptr::Ptr () : m_ptr (0) {} @@ -123,27 +141,22 @@ template Ptr::Ptr (T *ptr) : m_ptr (ptr) -{} +{ + Acquire (); +} template Ptr::Ptr (Ptr const&o) - : m_ptr (o.m_ptr) + : m_ptr (o.Peek ()) { - if (m_ptr != 0) - { - m_ptr->Ref(); - } + Acquire (); } template template Ptr::Ptr (Ptr const &o) - : m_ptr (o.m_ptr) + : m_ptr (o.Peek ()) { - if (m_ptr != 0) - { - NS_ASSERT (o.m_ptr != 0); - m_ptr->Ref(); - } + Acquire (); } template @@ -160,16 +173,15 @@ Ptr::operator = (Ptr const& o) { if (&o == this) - return *this; + { + return *this; + } if (m_ptr != 0) { m_ptr->Unref(); } m_ptr = o.m_ptr; - if (m_ptr != 0) - { - m_ptr->Ref(); - } + Acquire (); return *this; } @@ -184,7 +196,7 @@ T * Ptr::Get () const { - m_ptr->Ref(); + Acquire (); return m_ptr; } @@ -247,6 +259,20 @@ } template +bool +operator == (Ptr const &lhs, Ptr const &rhs) +{ + return lhs.Get () == rhs.Get (); +} +template +bool +operator != (Ptr const &lhs, Ptr const &rhs) +{ + return lhs.Get () != rhs.Get (); +} + + +template Ptr const_pointer_cast (Ptr const&p) { diff -r a4ef066d1185 -r 6fb98941c36f src/devices/p2p/p2p-topology.cc --- a/src/devices/p2p/p2p-topology.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/devices/p2p/p2p-topology.cc Thu May 10 18:33:52 2007 +0200 @@ -46,14 +46,19 @@ const Time& delay) { PointToPointChannel* channel = new PointToPointChannel(bps, delay); + channel->Ref (); PointToPointNetDevice* net1 = new PointToPointNetDevice(n1); + net1->Ref (); + net1->AddQueue(Queue::Default().Copy()); n1->AddDevice (net1); net1->Attach (channel); net1->Unref (); PointToPointNetDevice* net2 = new PointToPointNetDevice(n2); + net2->Ref (); + net2->AddQueue(Queue::Default().Copy()); n2->AddDevice (net2); net2->Attach (channel); diff -r a4ef066d1185 -r 6fb98941c36f src/internet-node/internet-node.cc --- a/src/internet-node/internet-node.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/internet-node/internet-node.cc Thu May 10 18:33:52 2007 +0200 @@ -45,10 +45,18 @@ Arp *arp = new Arp (this); Udp *udp = new Udp (this); + ipv4->Ref (); + arp->Ref (); + udp->Ref (); + ApplicationList *applicationList = new ApplicationList(this); L3Demux *l3Demux = new L3Demux(this); Ipv4L4Demux *ipv4L4Demux = new Ipv4L4Demux(this); + applicationList->Ref (); + l3Demux->Ref (); + ipv4L4Demux->Ref (); + l3Demux->Insert (ipv4); l3Demux->Insert (arp); ipv4L4Demux->Insert (udp); @@ -58,6 +66,11 @@ IIpv4Impl *ipv4Impl = new IIpv4Impl (ipv4); IIpv4Private *ipv4Private = new IIpv4Private (ipv4); + udpImpl->Ref (); + arpPrivate->Ref (); + ipv4Impl->Ref (); + ipv4Private->Ref (); + NsUnknown::AddInterface (ipv4Private); NsUnknown::AddInterface (ipv4Impl); NsUnknown::AddInterface (arpPrivate); diff -r a4ef066d1185 -r 6fb98941c36f src/internet-node/udp.cc --- a/src/internet-node/udp.cc Thu May 10 09:57:46 2007 +0200 +++ b/src/internet-node/udp.cc Thu May 10 18:33:52 2007 +0200 @@ -68,7 +68,9 @@ Socket * Udp::CreateSocket (void) { - return new UdpSocket (m_node, this); + Socket *socket = new UdpSocket (m_node, this); + socket->Ref (); + return socket; } Ipv4EndPoint *