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

Removed reference counter and binding functionality from Array

parent ab6d4854
Loading
Loading
Loading
Loading
+3 −71
Original line number Diff line number Diff line
@@ -296,55 +296,6 @@ class Array
      template< typename ArrayT >
      void setLike( const ArrayT& array );

      /**
       * \brief Binds \e _data with this array.
       *
       * Releases old data and binds this array with new \e _data. Also sets new
       * \e _size of this array.
       *
       * This method is deprecated, use \ref ArrayView instead.
       *
       * \param _data Pointer to new data.
       * \param _size Size of new _data. Number of elements.
       */
      [[deprecated("Binding functionality of Array is deprecated, ArrayView should be used instead.")]]
      void bind( Value* _data,
                 const Index _size );

      /**
       * \brief Binds this array with another \e array.
       *
       * Releases old data and binds this array with new \e array starting at
       * position \e begin. Also sets new \e size of this array.
       *
       * This method is deprecated, use \ref ArrayView instead.
       *
       * \tparam ArrayT Type of array.
       * \param array Reference to a new array.
       * \param begin Starting index position.
       * \param size Size of new array. Number of elements.
       */
      template< typename ArrayT >
      [[deprecated("Binding functionality of Array is deprecated, ArrayView should be used instead.")]]
      void bind( const ArrayT& array,
                 const IndexType& begin = 0,
                 const IndexType& size = 0 );

      /**
       * \brief Binds this array with a static array of size \e Size.
       *
       * Releases old data and binds this array with a static array of size \e
       * Size.
       *
       * This method is deprecated, use \ref ArrayView instead.
       *
       * \tparam Size Size of array.
       * \param array Reference to a static array.
       */
      template< int Size >
      [[deprecated("Binding functionality of Array is deprecated, ArrayView should be used instead.")]]
      void bind( StaticArray< Size, Value >& array );

      /**
       * \brief Returns a modifiable view of the array.
       *
@@ -673,30 +624,11 @@ class Array
      /** \brief Method for releasing (deallocating) array data. */
      void releaseData();

      /** \brief Number of elements in the array. */
      mutable Index size = 0;

      /** \brief Pointer to the data. */
      mutable Value* data = nullptr;

      /**
       * \brief Pointer to the originally allocated data.
       *
       * They might differ if one long array is partitioned into more shorter
       * arrays. Each of them must know the pointer on allocated data because
       * the last one must deallocate the array. If outer data (not allocated
       * by TNL) are bind then this pointer is zero since no deallocation is
       * necessary.
       */
      mutable Value* allocationPointer = nullptr;
      Value* data = nullptr;

      /**
       * \brief Counter of objects sharing this array or some parts of it.
       *
       * The reference counter is allocated after first sharing of the data between
       * more arrays. This is to avoid unnecessary dynamic memory allocation.
       */
      mutable int* referenceCounter = nullptr;
      /** \brief Number of elements in the array. */
      Index size = 0;

      /**
       * \brief The internal allocator instance.
+8 −102
Original line number Diff line number Diff line
@@ -30,15 +30,11 @@ template< typename Value,
          typename Allocator >
Array< Value, Device, Index, Allocator >::
Array( Array&& array )
: size( std::move(array.size) ),
  data( std::move(array.data) ),
  allocationPointer( std::move(array.allocationPointer) ),
  referenceCounter( std::move(array.referenceCounter) ),
: data( std::move(array.data) ),
  size( std::move(array.size) ),
  allocator( std::move(array.allocator) )
{
   array.data = nullptr;
   array.allocationPointer = nullptr;
   array.referenceCounter = nullptr;
}

template< typename Value,
@@ -232,22 +228,10 @@ void
Array< Value, Device, Index, Allocator >::
releaseData()
{
   if( this->referenceCounter )
   {
      if( --*this->referenceCounter == 0 )
      {
         allocator.deallocate( this->allocationPointer, this->size );
         delete this->referenceCounter;
         //std::cerr << "Deallocating reference counter " << this->referenceCounter << std::endl;
      }
   }
   else
      if( allocationPointer )
         allocator.deallocate( this->allocationPointer, this->size );
   this->allocationPointer = 0;
   this->data = 0;
   if( this->data )
      allocator.deallocate( this->data, this->size );
   this->data = nullptr;
   this->size = 0;
   this->referenceCounter = 0;
}

template< typename Value,
@@ -260,17 +244,16 @@ setSize( Index size )
{
   TNL_ASSERT_GE( size, (Index) 0, "Array size must be non-negative." );

   if( this->size == size && allocationPointer && ! referenceCounter )
   if( this->size == size )
      return;
   this->releaseData();

   // Allocating zero bytes is useless. Moreover, the allocators don't behave the same way:
   // "operator new" returns some non-zero address, the latter returns a null pointer.
   if( size > 0 ) {
      this->allocationPointer = allocator.allocate( size );
      this->data = this->allocationPointer;
      this->data = allocator.allocate( size );
      this->size = size;
      TNL_ASSERT_TRUE( this->allocationPointer,
      TNL_ASSERT_TRUE( this->data,
                       "This should never happen - allocator did not throw on an error." );
   }
}
@@ -299,76 +282,6 @@ setLike( const ArrayT& array )
   setSize( array.getSize() );
}

template< typename Value,
          typename Device,
          typename Index,
          typename Allocator >
void
Array< Value, Device, Index, Allocator >::
bind( Value* data,
      const Index size )
{
   TNL_ASSERT_TRUE( data, "Null pointer cannot be bound." );
   this->releaseData();
   this->data = data;
   this->size = size;
}

template< typename Value,
          typename Device,
          typename Index,
          typename Allocator >
   template< typename ArrayT >
void
Array< Value, Device, Index, Allocator >::
bind( const ArrayT& array,
      const IndexType& begin,
      const IndexType& size )
{
   // all template parameters of Array must match, otherwise binding does not make sense
   static_assert( std::is_same< Value, typename ArrayT::ValueType >::value, "ValueType of both arrays must be the same." );
   static_assert( std::is_same< Device, typename ArrayT::DeviceType >::value, "DeviceType of both arrays must be the same." );
   static_assert( std::is_same< Index, typename ArrayT::IndexType >::value, "IndexType of both arrays must be the same." );
   TNL_ASSERT_TRUE( array.getData(), "Empty array cannot be bound." );
   TNL_ASSERT_LT( begin, array.getSize(), "Begin of array is out of bounds." );
   TNL_ASSERT_LE( begin + size, array.getSize(), "End of array is out of bounds." );

   this->releaseData();
   if( size )
      this->size = size;
   else
      this->size = array.getSize() - begin;
   this->data = const_cast< Value* >( &array.getData()[ begin ] );
   this->allocationPointer = array.allocationPointer;
   if( array.allocationPointer )
   {
      if( array.referenceCounter )
      {
         this->referenceCounter = array.referenceCounter;
         ( *this->referenceCounter )++;
      }
      else
      {
         this->referenceCounter = array.referenceCounter = new int( 2 );
         //std::cerr << "Allocating reference counter " << this->referenceCounter << std::endl;
      }
   }
}

template< typename Value,
          typename Device,
          typename Index,
          typename Allocator >
   template< int Size >
void
Array< Value, Device, Index, Allocator >::
bind( StaticArray< Size, Value >& array )
{
   this->releaseData();
   this->size = Size;
   this->data = array.getData();
}

template< typename Value,
          typename Device,
          typename Index,
@@ -425,8 +338,6 @@ swap( Array< Value, Device, Index, Allocator >& array )
{
   TNL::swap( this->size, array.size );
   TNL::swap( this->data, array.data );
   TNL::swap( this->allocationPointer, array.allocationPointer );
   TNL::swap( this->referenceCounter, array.referenceCounter );
}

template< typename Value,
@@ -595,13 +506,8 @@ operator=( Array< Value, Device, Index, Allocator >&& array )

   this->size = array.size;
   this->data = array.data;
   this->allocationPointer = array.allocationPointer;
   this->referenceCounter = array.referenceCounter;

   array.size = 0;
   array.data = nullptr;
   array.allocationPointer = nullptr;
   array.referenceCounter = nullptr;
   return *this;
}