diff --git a/src/core/object-ref-count.h b/src/core/object-ref-count.h new file mode 100644 index 000000000..c148ebf33 --- /dev/null +++ b/src/core/object-ref-count.h @@ -0,0 +1,121 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 Mathieu Lacage + * + * 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: Mathieu Lacage + */ +#ifndef OBJECT_REF_COUNT_H +#define OBJECT_REF_COUNT_H + +#include "empty.h" + +namespace ns3 { + +/** + * This templated class provides the refcounting implementation + * for the ns3::Object class. + * + * The tricky aspect of the implementation of this class is that + * instead of storing a count directly, it keeps track of a pointer + * to a count to allow multiple instances of the class to share the + * same pointer/count. This is mainly used by the aggregation support + * of the ns3::Object class + */ +template +class ObjectRefCount : public PARENT +{ +public: + ObjectRefCount () + : m_count (new int (1)) + {} + ObjectRefCount (const ObjectRefCount &o) + : m_count (new int (1)) + {} + ObjectRefCount & operator = (const ObjectRefCount &o) + { + *m_count = *o.m_count; + return *this; + } + ~ObjectRefCount () + { + m_count = 0; + } + + /** + * Get the reference count of the object. Normally not needed; for language bindings. + */ + int GetReferenceCount (void) const { + return *m_count; + } + /** + * Increment the reference count. This method should not be called + * by user code. Object instances are expected to be used in conjunction + * of the Ptr template which would make calling Ref unecessary and + * dangerous. + */ + inline void Ref (void) const + { + (*m_count)++; + } + /** + * Decrement the reference count. This method should not be called + * by user code. Object instances are expected to be used in conjunction + * of the Ptr template which would make calling Ref unecessary and + * dangerous. + */ + inline void Unref (void) const + { + (*m_count)--; + if ((*m_count) == 0) + { + const_cast*>(this)->DoDelete (); + } + } +protected: + /** + * \param other another object + * + * This method makes this object and the input other object + * share the same reference count. + */ + void ShareCount (ObjectRefCount *other) + { + (*m_count) += (*other->m_count); + delete other->m_count; + other->m_count = m_count; + } + /** + * Called just before deleting this object: when two + * objects share the same reference count, the user + * who is deleting them must be careful to delete the + * associated count only once and this is done by calling + * this method to get a reference to the count and, then, + * calling delete on the count. + * + * \sa ns3::Object::DoDelete + */ + int *PeekCountPtr (void) const + { + return m_count; + } +private: + virtual void DoDelete (void) = 0; + mutable int *m_count; +}; + +} // namespace ns3 + +#endif /* OBJECT_REF_COUNT_H */ diff --git a/src/core/object.cc b/src/core/object.cc index 2725ba6cf..0db017b75 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -79,8 +79,7 @@ Object::GetTypeId (void) Object::Object () - : m_count (1), - m_tid (Object::GetTypeId ()), + : m_tid (Object::GetTypeId ()), m_disposed (false), m_aggregates ((struct Aggregates *)malloc (sizeof (struct Aggregates))), m_getObjectCount (0) @@ -112,8 +111,7 @@ Object::~Object () m_aggregates = 0; } Object::Object (const Object &o) - : m_count (1), - m_tid (o.m_tid), + : m_tid (o.m_tid), m_disposed (false), m_aggregates ((struct Aggregates *)malloc (sizeof (struct Aggregates))), m_getObjectCount (0) @@ -121,11 +119,6 @@ Object::Object (const Object &o) m_aggregates->n = 1; m_aggregates->buffer[0] = this; } -uint32_t -Object::GetReferenceCount (void) const -{ - return m_count; -} void Object::Construct (const AttributeList &attributes) { @@ -231,6 +224,10 @@ Object::AggregateObject (Ptr o) Object *current = aggregates->buffer[i]; current->m_aggregates = aggregates; } + + // share the counts + ShareCount (other); + // Finally, call NotifyNewAggregate in the listed chain for (uint32_t i = 0; i < n; i++) { @@ -271,7 +268,7 @@ Object::DoDispose (void) bool Object::Check (void) const { - return (m_count > 0); + return (GetReferenceCount () > 0); } /* In some cases, when an event is scheduled against a subclass of @@ -289,13 +286,12 @@ Object::CheckLoose (void) const for (uint32_t i = 0; i < n; i++) { Object *current = m_aggregates->buffer[i]; - refcount += current->m_count; + refcount += current->GetReferenceCount (); } return (refcount > 0); } - void -Object::MaybeDelete (void) const +Object::DoDelete (void) { // First, check if any of the attached // Object has a non-zero count. @@ -303,7 +299,7 @@ Object::MaybeDelete (void) const for (uint32_t i = 0; i < n; i++) { Object *current = m_aggregates->buffer[i]; - if (current->m_count != 0) + if (current->GetReferenceCount () != 0) { return; } @@ -319,6 +315,8 @@ Object::MaybeDelete (void) const } } + int *count = PeekCountPtr (); + // all attached objects have a zero count so, // we can delete them all. struct Aggregates *aggregates = m_aggregates; @@ -331,6 +329,8 @@ Object::MaybeDelete (void) const Object *current = aggregates->buffer[0]; delete current; } + + delete count; } } // namespace ns3 diff --git a/src/core/object.h b/src/core/object.h index e91994e90..4488b351e 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -28,6 +28,7 @@ #include "attribute.h" #include "object-base.h" #include "attribute-list.h" +#include "object-ref-count.h" namespace ns3 { @@ -55,7 +56,7 @@ class TraceSourceAccessor; * invoked from the Object::Unref method before destroying the object, even if the user * did not call Object::Dispose directly. */ -class Object : public ObjectBase +class Object : public ObjectRefCount { public: static TypeId GetTypeId (void); @@ -98,26 +99,6 @@ public: */ virtual TypeId GetInstanceTypeId (void) const; - /** - * Increment the reference count. This method should not be called - * by user code. Object instances are expected to be used in conjunction - * of the Ptr template which would make calling Ref unecessary and - * dangerous. - */ - inline void Ref (void) const; - /** - * Decrement the reference count. This method should not be called - * by user code. Object instances are expected to be used in conjunction - * of the Ptr template which would make calling Ref unecessary and - * dangerous. - */ - inline void Unref (void) const; - - /** - * Get the reference count of the object. Normally not needed; for language bindings. - */ - uint32_t GetReferenceCount (void) const; - /** * \returns a pointer to the requested interface or zero if it could not be found. */ @@ -233,13 +214,6 @@ private: Ptr DoGetObject (TypeId tid) const; bool Check (void) const; bool CheckLoose (void) const; - /** - * Attempt to delete this object. This method iterates - * over all aggregated objects to check if they all - * have a zero refcount. If yes, the object and all - * its aggregates are deleted. If not, nothing is done. - */ - void MaybeDelete (void) const; /** * \param tid an TypeId * @@ -259,15 +233,14 @@ private: void Construct (const AttributeList &attributes); void UpdateSortedArray (struct Aggregates *aggregates, uint32_t i) const; - /** - * The reference count for this object. Each aggregate - * has an individual reference count. When the global - * reference count (the sum of all reference counts) - * reaches zero, the object and all its aggregates is - * deleted. + * Attempt to delete this object. This method iterates + * over all aggregated objects to check if they all + * have a zero refcount. If yes, the object and all + * its aggregates are deleted. If not, nothing is done. */ - mutable uint32_t m_count; + virtual void DoDelete (void); + /** * Identifies the type of this object instance. */ @@ -363,22 +336,6 @@ namespace ns3 { * The Object implementation which depends on templates *************************************************************************/ -void -Object::Ref (void) const -{ - m_count++; -} -void -Object::Unref (void) const -{ - NS_ASSERT (Check ()); - m_count--; - if (m_count == 0) - { - MaybeDelete (); - } -} - template Ptr Object::GetObject () const diff --git a/src/core/wscript b/src/core/wscript index 519f17eda..40f146858 100644 --- a/src/core/wscript +++ b/src/core/wscript @@ -126,6 +126,7 @@ def build(bld): 'abort.h', 'names.h', 'vector.h', + 'object-ref-count.h', ] if sys.platform == 'win32':