gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] gnash ChangeLog libbase/smart_ptr.h


From: Sandro Santilli
Subject: [Gnash-commit] gnash ChangeLog libbase/smart_ptr.h
Date: Wed, 08 Nov 2006 09:37:56 +0000

CVSROOT:        /sources/gnash
Module name:    gnash
Changes by:     Sandro Santilli <strk>  06/11/08 09:37:56

Modified files:
        .              : ChangeLog 
        libbase        : smart_ptr.h 

Log message:
                * libbase/smart_ptr.h: added invariant testing

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.1556&r2=1.1557
http://cvs.savannah.gnu.org/viewcvs/gnash/libbase/smart_ptr.h?cvsroot=gnash&r1=1.12&r2=1.13

Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.1556
retrieving revision 1.1557
diff -u -b -r1.1556 -r1.1557
--- ChangeLog   8 Nov 2006 09:36:15 -0000       1.1556
+++ ChangeLog   8 Nov 2006 09:37:56 -0000       1.1557
@@ -1,5 +1,6 @@
 2006-11-08 Sandro Santilli <address@hidden>
 
+       * libbase/smart_ptr.h: added invariant testing 
        * testsuite/libbase/CurlStreamTest.cpp: added missing
          config.h include.
        * testsuite/libbase/: Makefile.am, smart_ptrTest.cpp:

Index: libbase/smart_ptr.h
===================================================================
RCS file: /sources/gnash/gnash/libbase/smart_ptr.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- libbase/smart_ptr.h 7 Nov 2006 17:16:18 -0000       1.12
+++ libbase/smart_ptr.h 8 Nov 2006 09:37:56 -0000       1.13
@@ -9,7 +9,7 @@
 // although the nice thing about templates is that no particular
 // ref-counted class is mandated.
 
-/* $Id: smart_ptr.h,v 1.12 2006/11/07 17:16:18 strk Exp $ */
+/* $Id: smart_ptr.h,v 1.13 2006/11/08 09:37:56 strk Exp $ */
 
 #ifndef SMART_PTR_H
 #define SMART_PTR_H
@@ -27,6 +27,7 @@
 class DSOEXPORT smart_ptr
 {
 public:
+
        smart_ptr(T* ptr)
                :
                m_ptr(ptr)
@@ -35,9 +36,16 @@
                {
                        m_ptr->add_ref();
                }
+               testInvariant();
+       }
+
+       smart_ptr()
+               :
+               m_ptr(NULL)
+       {
+               testInvariant();
        }
 
-       smart_ptr() : m_ptr(NULL) {}
        smart_ptr(const smart_ptr<T>& s)
                :
                m_ptr(s.m_ptr)
@@ -46,10 +54,12 @@
                {
                        m_ptr->add_ref();
                }
+               testInvariant();
        }
 
        ~smart_ptr()
        {
+               testInvariant();
                if (m_ptr)
                {
                        m_ptr->drop_ref();
@@ -57,20 +67,96 @@
        }
 
        //operator bool() const { return m_ptr != NULL; }
-       void    operator=(const smart_ptr<T>& s) { set_ref(s.m_ptr); }
-       void    operator=(T* ptr) { set_ref(ptr); }
-       T*      operator->() const { assert(m_ptr); return m_ptr; }
-       const T& operator*() const { assert(m_ptr); return *m_ptr; }
-       T& operator*() { assert(m_ptr); return *m_ptr; }
-       T*      get_ptr() const { return m_ptr; }
-       bool    operator==(const smart_ptr<T>& p) const { return m_ptr == 
p.m_ptr; }
-       bool    operator!=(const smart_ptr<T>& p) const { return m_ptr != 
p.m_ptr; }
-       bool    operator==(T* p) const { return m_ptr == p; }
-       bool    operator!=(T* p) const { return m_ptr != p; }
+       void    operator=(const smart_ptr<T>& s)
+       {
+               set_ref(s.m_ptr);
+               testInvariant();
+       }
+
+       void    operator=(T* ptr)
+       {
+               set_ref(ptr);
+               testInvariant();
+       }
+
+       T*      operator->() const
+       {
+               assert(m_ptr);
+               testInvariant();
+               return m_ptr;
+       }
+
+       const T& operator*() const
+       {
+               assert(m_ptr);
+               testInvariant();
+               return *m_ptr;
+       }
+
+       T& operator*()
+       {
+               assert(m_ptr);
+               testInvariant();
+               return *m_ptr;
+       }
+
+       T*      get_ptr() const
+       {
+               testInvariant();
+               return m_ptr;
+       }
+
+       bool    operator==(const smart_ptr<T>& p) const
+       {
+               testInvariant();
+               return m_ptr == p.m_ptr;
+       }
+
+       bool    operator!=(const smart_ptr<T>& p) const
+       {
+               testInvariant();
+               return m_ptr != p.m_ptr;
+       }
+
+       bool    operator==(T* p) const
+       {
+               testInvariant();
+               return m_ptr == p;
+       }
+
+       bool    operator!=(T* p) const
+       {
+               testInvariant();
+               return m_ptr != p;
+       }
 
        // Provide work-alikes for static_cast, dynamic_cast, implicit up-cast? 
 ("gentle_cast" a la ajb?)
 
+       /// Check invariant of the smart pointer.
+       //      
+       /// This function is called as first thing by every public 
+       /// inspector function and as last thing by every mutator
+       /// function. If you build with NDEBUG defined all such
+       /// calls will be removed (in case you're worried about
+       /// overhead).
+       ///
+       /// Leaving the calls in will increase the *probability*
+       /// to detect memory corruption errors earier.
+       ///
+       /// To further improve this we might have the smart_ptr
+       /// testInvariant function call the pointed-to testInvariant
+       /// function. I dind't push it so far though (yet) :)
+       ///
+       void testInvariant() const
+       {
+               // If we have a pointer, check that it's refcount
+               // is greater then 0, as if it is not that means
+               // that someone deleted it
+               assert( m_ptr != NULL || m_ptr->get_ref_count() > 0 );
+       }
+
 private:
+
        void    set_ref(T* ptr)
        {
                if (ptr != m_ptr)
@@ -86,6 +172,8 @@
                                m_ptr->add_ref();
                        }
                }
+
+               testInvariant();
        }
 
        T*      m_ptr;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]