Commit 37702129 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Refactoring: code reduction in SharedPointer.h

Also added the allocate method to DevicePointer and UniquePointer for
consistency. Added missing free() method to UniquePointer.
parent 7c90af90
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -215,14 +215,7 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
      : pointer( 0 ), cuda_pointer( 0 ),
        counter( 0 ), last_sync_state( 0 )
      {
         this->counter = new int( 1 );
         this->pointer = &obj;
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return;
         this->last_sync_state = ::operator new( sizeof( Object ) );
         this->set_last_sync_state();
         Devices::Cuda::insertSmartPointer( this );
         this->allocate( obj );
      }

      // this is needed only to avoid the default compiler-generated constructor
@@ -416,6 +409,21 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer

   protected:

      bool allocate( ObjectType& obj )
      {
         this->counter = new int( 1 );
         if( ! this->counter )
            return false;
         this->pointer = &obj;
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return false;
         this->last_sync_state = ::operator new( sizeof( Object ) );
         this->set_last_sync_state();
         Devices::Cuda::insertSmartPointer( this );
         return true;
      }

      void set_last_sync_state()
      {
         std::memcpy( (void*) this->last_sync_state, (void*) this->pointer, sizeof( ObjectType ) );
+25 −37
Original line number Diff line number Diff line
@@ -314,19 +314,7 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
        counter( 0 ), last_sync_state( 0 )
      {
         if( ! lazy )
         {
            this->counter = new int( 1 );
            this->pointer = new Object( args... );
            this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
            if( ! this->cuda_pointer )
               return;
            this->last_sync_state = ::operator new( sizeof( Object ) );
            this->set_last_sync_state();
#ifdef TNL_DEBUG_SHARED_POINTERS
            std::cerr << "Created shared pointer to " << demangle(typeid(ObjectType).name()) << " (cuda_pointer = " << this->cuda_pointer << ")" << std::endl;
#endif
            Devices::Cuda::insertSmartPointer( this );
         }
            this->allocate( args... );
      }

      // this is needed only to avoid the default compiler-generated constructor
@@ -386,17 +374,8 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         std::cerr << "Recreating shared pointer to " << demangle(typeid(ObjectType).name()) << std::endl;
#endif
         if( ! this->counter )
         {
            this->counter = new int( 1 );
            this->pointer = new ObjectType( args... );
            this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
            if( ! this->cuda_pointer )
               return false;
            this->last_sync_state = ::operator new( sizeof( Object ) );
            this->set_last_sync_state();
            Devices::Cuda::insertSmartPointer( this );
            return true;
         }
            return this->allocate( args... );

         if( *this->counter == 1 )
         {
            /****
@@ -414,17 +393,7 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         // free will just decrement the counter
         this->free();

         this->counter= new int( 1 );
         this->pointer = new Object( args... );
         if( ! this->pointer || ! this->counter )
            return false;
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return false;
         this->last_sync_state = ::operator new( sizeof( Object ) );
         this->set_last_sync_state();
         Devices::Cuda::insertSmartPointer( this );
         return true;
         return this->allocate( args... );
      }

      const Object* operator->() const
@@ -584,14 +553,33 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer

   protected:

      template< typename... Args >
      bool allocate( Args... args )
      {
         this->counter = new int( 1 );
         this->pointer = new Object( args... );
         if( ! this->pointer || ! this->counter )
            return false;
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return false;
         this->last_sync_state = ::operator new( sizeof( Object ) );
         this->set_last_sync_state();
#ifdef TNL_DEBUG_SHARED_POINTERS
         std::cerr << "Created shared pointer to " << demangle(typeid(ObjectType).name()) << " (cuda_pointer = " << this->cuda_pointer << ")" << std::endl;
#endif
         Devices::Cuda::insertSmartPointer( this );
         return true;
      }

      void set_last_sync_state()
      {
         std::memcpy( (void*) this->last_sync_state, (void*) this->pointer, sizeof( ObjectType ) );
         std::memcpy( (void*) (this->pointer + 1), (void*) this->pointer, sizeof( ObjectType ) );
      }

      bool modified()
      {
         return std::memcmp( (void*) this->last_sync_state, (void*) this->pointer, sizeof( ObjectType ) ) != 0;
         return std::memcmp( (void*) (this->pointer + 1), (void*) this->pointer, sizeof( ObjectType ) ) != 0;
      }

      void free()
+28 −15
Original line number Diff line number Diff line
@@ -128,11 +128,7 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      : pointer( 0 ), cuda_pointer( 0 ),
        last_sync_state( 0 )
      {
         this->pointer = new Object( args... );
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         this->last_sync_state = ::operator new( sizeof( Object ) );
         this->set_last_sync_state();
         Devices::Cuda::insertSmartPointer( this );
         this->allocate( args... );
      }
      
      const Object* operator->() const
@@ -186,10 +182,7 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      
      const ThisType& operator=( ThisType& ptr )
      {
         if( this->pointer )
            delete this->pointer;
         if( this->cuda_pointer )
            Devices::Cuda::freeFromDevice( this->cuda_pointer );
         this->free();
         this->pointer = ptr.pointer;
         this->cuda_pointer = ptr.cuda_pointer;
         this->last_sync_state = ptr.last_sync_state;
@@ -223,17 +216,27 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
            
      ~UniquePointer()
      {
         if( this->pointer )
            delete this->pointer;
         if( this->cuda_pointer )
            Devices::Cuda::freeFromDevice( this->cuda_pointer );
         if( this->last_sync_state )
            ::operator delete( this->last_sync_state );
         this->free();
         Devices::Cuda::removeSmartPointer( this );
      }
      
   protected:

      template< typename... Args >
      bool allocate( Args... args )
      {
         this->pointer = new Object( args... );
         if( ! this->pointer )
            return false;
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return false;
         this->last_sync_state = ::operator new( sizeof( Object ) );
         this->set_last_sync_state();
         Devices::Cuda::insertSmartPointer( this );
         return true;
      }

      void set_last_sync_state()
      {
         std::memcpy( (void*) this->last_sync_state, (void*) this->pointer, sizeof( ObjectType ) );
@@ -244,6 +247,16 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
         return std::memcmp( (void*) this->last_sync_state, (void*) this->pointer, sizeof( ObjectType ) ) != 0;
      }

      void free()
      {
         if( this->pointer )
            delete this->pointer;
         if( this->cuda_pointer )
            Devices::Cuda::freeFromDevice( this->cuda_pointer );
         if( this->last_sync_state )
            ::operator delete( this->last_sync_state );
      }
      
      Object *pointer, *cuda_pointer;
      
      void* last_sync_state;