LCOV - code coverage report
Current view: top level - lunchbox - referenced.h (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 27 27 100.0 %
Date: 2014-08-05 Functions: 7 8 87.5 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or modify it under
       5             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       6             :  * by the Free Software Foundation.
       7             :  *
       8             :  * This library is distributed in the hope that it will be useful, but WITHOUT
       9             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      10             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      11             :  * details.
      12             :  *
      13             :  * You should have received a copy of the GNU Lesser General Public License
      14             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      15             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      16             :  */
      17             : 
      18             : #ifndef LUNCHBOX_REFERENCED_H
      19             : #define LUNCHBOX_REFERENCED_H
      20             : 
      21             : #include <lunchbox/api.h>      // for LUNCHBOX_API
      22             : #include <lunchbox/atomic.h>   // member
      23             : #include <lunchbox/debug.h>    // for LBERROR
      24             : 
      25             : //#define LUNCHBOX_REFERENCED_DEBUG
      26             : #ifdef LUNCHBOX_REFERENCED_DEBUG
      27             : #  include <lunchbox/clock.h>
      28             : #  include <lunchbox/hash.h>
      29             : #  include <lunchbox/lockable.h>
      30             : #  include <lunchbox/scopedMutex.h>
      31             : #  include <lunchbox/spinLock.h>
      32             : #endif
      33             : 
      34             : namespace lunchbox
      35             : {
      36             : /**
      37             :  * Base class for referenced objects.
      38             :  *
      39             :  * Implements reference-counted objects which destroy themselves once they are
      40             :  * no longer referenced. Uses an Atomic variable to keep the reference count
      41             :  * access thread-safe and efficient.
      42             :  *
      43             :  * @sa RefPtr
      44             :  */
      45             : class Referenced
      46             : {
      47             : public:
      48             :     /** Increase the reference count. @version 1.0 .*/
      49    10095139 :     void ref( const void* holder LB_UNUSED = 0 ) const
      50             :     {
      51             : #ifndef NDEBUG
      52    10095139 :         LBASSERT( !_hasBeenDeleted );
      53             : #endif
      54    10346172 :         ++_refCount;
      55             : 
      56             : #ifdef LUNCHBOX_REFERENCED_DEBUG
      57             :         if( holder )
      58             :         {
      59             :             std::stringstream cs;
      60             :             cs << "Thread " << Log::instance().getThreadName() << " @ "
      61             :                << Log::instance().getClock().getTime64() << " rc " << _refCount
      62             :                << " from " << backtrace;
      63             :             ScopedFastWrite mutex( _holders );
      64             :             HolderHash::iterator i = _holders->find( holder );
      65             :             LBASSERTINFO( i == _holders->end(), i->second );
      66             :             _holders.data[ holder ] = cs.str();
      67             :         }
      68             : #endif
      69    10677813 :     }
      70             : 
      71             :     /**
      72             :      * Decrease the reference count.
      73             :      *
      74             :      * The object is deleted when the reference count reaches 0.
      75             :      * @version 1.0
      76             :      * @return true if the reference count went to 0, false otherwise.
      77             :      */
      78    10497761 :     bool unref( const void* holder LB_UNUSED = 0 ) const
      79             :     {
      80             : #ifndef NDEBUG
      81    10497761 :         LBASSERT( !_hasBeenDeleted );
      82             : #endif
      83    10716359 :         LBASSERT( _refCount > 0 );
      84    11368624 :         const bool last = (--_refCount==0);
      85             : 
      86             : #ifdef LUNCHBOX_REFERENCED_DEBUG
      87             :         if( holder )
      88             :         {
      89             :             ScopedFastWrite mutex( _holders );
      90             :             HolderHash::iterator i = _holders->find( holder );
      91             :             LBASSERT( i != _holders->end( ));
      92             :             _holders->erase( i );
      93             :             LBASSERT( _holders->find( holder ) == _holders->end( ));
      94             :         }
      95             : #endif
      96             : 
      97    10515994 :         if( last )
      98           6 :             const_cast< Referenced* >( this )->notifyFree();
      99    12560094 :         return last;
     100             :     }
     101             : 
     102             :     /** @return the current reference count. @version 1.0 */
     103           8 :     int32_t getRefCount() const { return _refCount; }
     104             : 
     105             :     /** @internal print holders of this if debugging is enabled. */
     106             : #ifdef LUNCHBOX_REFERENCED_DEBUG
     107             :     void printHolders( std::ostream& os ) const
     108             :     {
     109             :         os << disableFlush << disableHeader << std::endl;
     110             :         {
     111             :             ScopedFastRead mutex( _holders );
     112             :             for( HolderHash::const_iterator i = _holders->begin();
     113             :                  i != _holders->end(); ++i )
     114             :             {
     115             :                 os << "Holder " << i->first << ": " << i->second << std::endl;
     116             :             }
     117             :         }
     118             :         os << enableHeader << enableFlush;
     119             :     }
     120             : #else
     121             :     void printHolders( std::ostream& ) const {}
     122             : #endif
     123             : 
     124             : protected:
     125             :     /** Construct a new reference-counted object. @version 1.0 */
     126           9 :     Referenced()
     127             :         : _refCount( 0 )
     128           9 :         , _hasBeenDeleted( false )
     129           9 :     {}
     130             : 
     131             :     /** Construct a new copy of a reference-counted object. @version 1.0 */
     132             :     Referenced( const Referenced& )
     133             :         : _refCount( 0 )
     134             :         , _hasBeenDeleted( false )
     135             : #ifdef LUNCHBOX_REFERENCED_DEBUG
     136             :         , _holders()
     137             : #endif
     138             :     {}
     139             : 
     140             :     /** Destruct a reference-counted object. @version 1.0 */
     141           7 :     virtual ~Referenced()
     142           7 :     {
     143           7 :         LBASSERT( !_hasBeenDeleted );
     144           7 :         _hasBeenDeleted = true;
     145           7 :         LBASSERTINFO( _refCount == 0,
     146             :                       "Deleting object with ref count " << _refCount );
     147           7 :     }
     148             : 
     149             :     /** Assign another object to this object. @version 1.1.3 */
     150             :     // cppcheck-suppress operatorEqVarError
     151             :     Referenced& operator = ( const Referenced& /*rhs*/ ) { return *this; }
     152             : 
     153             :     LUNCHBOX_API virtual void notifyFree();
     154             : 
     155             : private:
     156             :     mutable a_int32_t _refCount;
     157             :     bool _hasBeenDeleted;
     158             : 
     159             : #ifdef LUNCHBOX_REFERENCED_DEBUG
     160             :     typedef PtrHash< const void*, std::string > HolderHash;
     161             :     mutable Lockable< HolderHash, SpinLock > _holders;
     162             : #endif
     163             : };
     164             : }
     165             : 
     166             : namespace boost
     167             : {
     168             : /** Allow creation of boost::intrusive_ptr from RefPtr or Referenced. */
     169     5696293 : inline void intrusive_ptr_add_ref( lunchbox::Referenced* referenced )
     170             : {
     171     5696293 :     referenced->ref();
     172     6491240 : }
     173             : 
     174             : /** Allow creation of boost::intrusive_ptr from RefPtr or Referenced. */
     175     6535142 : inline void intrusive_ptr_release( lunchbox::Referenced* referenced )
     176             : {
     177     6535142 :     referenced->unref();
     178     6795671 : }
     179             : }
     180             : 
     181             : #endif //LUNCHBOX_REFERENCED_H

Generated by: LCOV version 1.10