From 4e7a215f6a2942b84606e9effaf173347cbb7ed3 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 2 May 2007 23:08:41 +0200 Subject: [PATCH] implement Dispose properly throughout the network stack --- SConstruct | 2 - src/node/application-list.cc | 47 ++++++++---------- src/node/application-list.h | 17 +++---- src/node/application.cc | 28 +++++++++-- src/node/application.h | 2 + src/node/capability.cc | 94 ----------------------------------- src/node/capability.h | 52 ------------------- src/node/internet-node.cc | 27 +++++++--- src/node/onoff-application.cc | 13 +++++ src/node/onoff-application.h | 2 + 10 files changed, 90 insertions(+), 194 deletions(-) delete mode 100644 src/node/capability.cc delete mode 100644 src/node/capability.h diff --git a/SConstruct b/SConstruct index bd0b7a637..28d1d08d4 100644 --- a/SConstruct +++ b/SConstruct @@ -200,7 +200,6 @@ node.add_sources ([ 'ipv4.cc', 'ipv4-end-point.cc', 'udp.cc', - 'capability.cc', 'arp-header.cc', 'application.cc', 'application-list.cc', @@ -257,7 +256,6 @@ node.add_inst_headers ([ 'udp-header.h', 'channel.h', 'node-list.h', - 'capability.h', 'application.h', 'application-list.h', 'onoff-application.h', diff --git a/src/node/application-list.cc b/src/node/application-list.cc index ef31c0f1c..a81b8b437 100644 --- a/src/node/application-list.cc +++ b/src/node/application-list.cc @@ -27,12 +27,24 @@ namespace ns3{ ApplicationList::ApplicationList(Node* n) - : Capability(n) +{} + +void +ApplicationList::Dispose (void) { + for (std::vector::const_iterator i = m_apps.begin(); + i != m_apps.end(); ++i) + { + Application *app = *i; + app->Dispose (); + delete app; + } + m_apps.clear (); } ApplicationList::~ApplicationList() { // Destructor, nothing needed as the SmartSet destroys itself + Dispose (); } ApplicationList* ApplicationList::Copy(Node * n) const @@ -44,46 +56,29 @@ ApplicationList* ApplicationList::Copy(Node * n) const void ApplicationList::Add(Application* a) { - m_apps.Add(a); + m_apps.push_back(a); } void ApplicationList::SetNode(Node * n) { - Capability::SetNode(n); // Set the node pointer in each application - for (SmartSet::const_iterator i = m_apps.Begin(); - i != m_apps.End(); ++i) + for (std::vector::const_iterator i = m_apps.begin(); + i != m_apps.end(); ++i) { // Set correct node pointer in each app (*i)->SetNode(n); } } -void ApplicationList::Remove(Application* a) -{ // Remove the specified application from the list - m_apps.Remove(a); -} -SmartSet::size_type ApplicationList::Count() const +uint32_t ApplicationList::Count() const { - return m_apps.Size(); + return m_apps.size(); } -Application* ApplicationList::Get(SmartSet::size_type i) const +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 - SmartSet::const_iterator k = m_apps.Begin(); - while(i > 0) - { - if (k == m_apps.End()) return 0; // Not found - --i; - ++k; - } - return *k; -} - -const SmartSet& ApplicationList::GetAll() const -{ - return m_apps; + if (m_apps.empty()) return 0; // List is empty + return m_apps[i]; } }//namespace ns3 diff --git a/src/node/application-list.h b/src/node/application-list.h index 63286e441..b5cc91f76 100644 --- a/src/node/application-list.h +++ b/src/node/application-list.h @@ -25,17 +25,17 @@ #define __APPLICATION_LIST_H__ #include "application.h" -#include "ns3/smartset.h" -#include "capability.h" +#include namespace ns3 { -class ApplicationList : public Capability { +class ApplicationList { public: ApplicationList(Node*); // Copy constructor not needed, default one is correct - ~ApplicationList(); + virtual ~ApplicationList(); // Inherited from Capabilty + void Dispose (void); virtual ApplicationList* Copy(Node*) const; virtual void SetNode(Node *); // Sets the node for all apps virtual void Add(Application*); // Add an already new'ed app @@ -43,16 +43,15 @@ public: template T* AddCopy(const T& t) // Add a new application { T* a = t.Copy(); - m_apps.Add(a); + m_apps.push_back(a); return a; } void Remove(Application*); // Application has finished - SmartSet::size_type Count() const; // Number of applications - Application* Get(SmartSet::size_type) const; // Get app by index - const SmartSet& GetAll() const; // Get the entire app list + uint32_t Count() const; // Number of applications + Application* Get(uint32_t i) const; // Get app by index private: - SmartSet m_apps; + std::vector m_apps; }; }//namespace ns3 diff --git a/src/node/application.cc b/src/node/application.cc index fa49cba06..fb75f1113 100644 --- a/src/node/application.cc +++ b/src/node/application.cc @@ -59,13 +59,31 @@ Application::Application(const Application& o) // \brief Application Destructor Application::~Application() { - m_node->Unref (); - // Cancel the start/stop events if scheduled - if (m_start) Simulator::Cancel(m_startEvent); - if (m_stop) Simulator::Cancel(m_stopEvent); - // Delete the random variablse + Dispose (); +} + +void +Application::Dispose (void) +{ + if (m_node != 0) + { + m_node->Unref (); + m_node = 0; + } + if (m_start) + { + Simulator::Cancel(m_startEvent); + m_start = false; + } + if (m_stop) + { + Simulator::Cancel(m_stopEvent); + m_stop = false; + } delete m_startVar; + m_startVar = 0; delete m_stopVar; + m_stopVar = 0; } Application& Application::operator=(const Application& rhs) diff --git a/src/node/application.h b/src/node/application.h index 5fbf82e4a..b217bb783 100644 --- a/src/node/application.h +++ b/src/node/application.h @@ -61,6 +61,8 @@ public: Application(const Application&); // Copy constructor Application& operator=(const Application&); // Assignment operator virtual ~Application(); + + virtual void Dispose (void); virtual Application* Copy() const = 0; // All applications must provide diff --git a/src/node/capability.cc b/src/node/capability.cc deleted file mode 100644 index 4fc41ebdb..000000000 --- a/src/node/capability.cc +++ /dev/null @@ -1,94 +0,0 @@ -// -*- Mode:NS3 -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License version 2 as -// published by the Free Software Foundation; -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Author: George F. Riley -// - -// Implementation of the capability base class. - -#include "capability.h" -#include "node.h" - -namespace ns3 { - -Capability::Capability(Node* n) - : m_node (n) -{ - m_node->Ref (); -} - -Capability::~Capability () -{ - if (m_node != 0) - { - m_node->Unref (); - } -} - -// Copy constructor -Capability::Capability(const Capability& o) - : m_node (o.m_node) -{ - if (m_node != 0) - { - m_node->Ref (); - } -} - -// Assignment operator -Capability& Capability::operator=(const Capability& rhs) -{ - if (this == &rhs) return *this; // Self assignment - if (m_node != 0) - { - m_node->Unref (); - } - m_node = rhs.m_node; - if (m_node != 0) - { - m_node->Ref (); - } - return *this; -} - -// SetNode should be overridden by any capability subclass -// that manages other objects needing node information, such -// as the ApplicationList. -void Capability::SetNode(Node *node) -{ - if (m_node != 0) - { - m_node->Unref (); - } - m_node = node; - if (m_node != 0) - { - m_node->Ref (); - } -} - - -Node* Capability::GetNode() const -{ - if (!m_node) return 0; - m_node->Ref (); - return m_node; -} - -} // namespace ns3 - diff --git a/src/node/capability.h b/src/node/capability.h deleted file mode 100644 index 80dffe909..000000000 --- a/src/node/capability.h +++ /dev/null @@ -1,52 +0,0 @@ -// -*- Mode:NS3 -*- -// -// Copyright (c) 2006 Georgia Tech Research Corporation -// All rights reserved. -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License version 2 as -// published by the Free Software Foundation; -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Author: George F. Riley -// -// Define the base class for all node capabilities. -// George F. Riley, Georgia Tech, Fall 2006 - -#ifndef __CAPABILITY_H__ -#define __CAPABILITY_H__ - -// All capabilities must implement a copy method, to allow node subclasses -// to have a pointer to any subclass of the capability and still copy -// correctly. Capabilities also have a pointer to the owning node. -// The node pointer is not owned by the capability, and is not deleted -// by the capability destructor. - -namespace ns3 { -class Node; -class NodeReference; - -class Capability -{ -public: - Capability(Node* n); - Capability(const Capability&); // Copy constructor - virtual Capability& operator=(const Capability&); // Assignment operator - virtual ~Capability(); - virtual Capability* Copy(Node *node) const = 0; - virtual void SetNode(Node *node); - virtual Node* GetNode() const; -private: - Node* m_node; // Node on which this capability is assigned -}; - -} //namespace ns3 -#endif diff --git a/src/node/internet-node.cc b/src/node/internet-node.cc index e24998397..4fa5be061 100644 --- a/src/node/internet-node.cc +++ b/src/node/internet-node.cc @@ -48,12 +48,10 @@ InternetNode::InternetNode() InternetNode::~InternetNode () { - delete m_applicationList; - delete m_l3Demux; - delete m_ipv4L4Demux; + Dispose (); } - void +void InternetNode::SetName (std::string name) { m_name = name; @@ -77,8 +75,25 @@ InternetNode::CreateTraceResolver (TraceContext const &context) void InternetNode::Dispose() { - m_l3Demux->Dispose (); - m_ipv4L4Demux->Dispose (); + if (m_l3Demux != 0) + { + m_l3Demux->Dispose (); + delete m_l3Demux; + m_l3Demux = 0; + } + if (m_ipv4L4Demux != 0) + { + m_ipv4L4Demux->Dispose (); + delete m_ipv4L4Demux; + m_ipv4L4Demux = 0; + } + + if (m_applicationList != 0) + { + m_applicationList->Dispose (); + delete m_applicationList; + m_applicationList = 0; + } // chain up. Node::Dispose (); diff --git a/src/node/onoff-application.cc b/src/node/onoff-application.cc index aa7047d18..b6f03c424 100644 --- a/src/node/onoff-application.cc +++ b/src/node/onoff-application.cc @@ -88,10 +88,23 @@ OnOffApplication::OnOffApplication(Node * n, const OnOffApplication& c) } OnOffApplication::~OnOffApplication() +{ + Dispose (); +} + +void +OnOffApplication::Dispose (void) { delete m_socket; delete m_onTime; delete m_offTime; + + m_socket = 0; + m_onTime = 0; + m_offTime = 0; + + // chain up + Application::Dispose (); } #ifdef REMOVE_THIS diff --git a/src/node/onoff-application.h b/src/node/onoff-application.h index c00208132..e04319de2 100644 --- a/src/node/onoff-application.h +++ b/src/node/onoff-application.h @@ -54,6 +54,8 @@ public: virtual void StopApplication(); // Called at time specified by Stop virtual OnOffApplication* Copy() const;// Make a copy of the application + virtual void Dispose (void); + // Event handlers void StartSending(); void StopSending();