LCOV - code coverage report
Current view: top level - lunchbox - buffer.ipp (source / functions) Hit Total Coverage
Test: lcov2.info Lines: 25 26 96.2 %
Date: 2014-10-01 Functions: 4 4 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2007-2014, Stefan Eilemann <eile@equalizergraphics.com>
       3             :  *               2011-2012, Daniel Nachbaur <danielnachbaur@gmail.com>
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or modify it under
       6             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       7             :  * by the Free Software Foundation.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful, but WITHOUT
      10             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      11             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      12             :  * details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public License
      15             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      16             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      17             :  */
      18             : 
      19             : namespace lunchbox
      20             : {
      21             : template< class T > Buffer< T >::Buffer( Buffer< T >& from )
      22             : {
      23             :     _data = from._data; _size = from._size; _maxSize = from._maxSize;
      24             :     from._data = 0; from._size = 0; from._maxSize = 0;
      25             : }
      26             : 
      27         904 : template< class T > T* Buffer< T >::pack()
      28             : {
      29         904 :     if( _maxSize != _size )
      30             :     {
      31         903 :         _data = static_cast< T* >( realloc( _data, _size * sizeof( T )));
      32         903 :         _maxSize = _size;
      33             :     }
      34         904 :     return _data;
      35             : }
      36             : 
      37             : template< class T >
      38             : Buffer< T >& Buffer< T >::operator = ( const Buffer< T >& from )
      39             : {
      40             :     replace( from );
      41             :     return *this;
      42             : }
      43             : 
      44         304 : template< class T > T* Buffer< T >::resize( const uint64_t newSize )
      45             : {
      46         304 :     _size = newSize;
      47         304 :     if( newSize <= _maxSize )
      48         152 :         return _data;
      49             : 
      50             :     // avoid excessive reallocs
      51         152 :     const uint64_t nElems = newSize + (newSize >> 3);
      52         152 :     const uint64_t nBytes = nElems * sizeof( T );
      53         152 :     _data = static_cast< T* >( realloc( _data, nBytes ));
      54         152 :     _maxSize = nElems;
      55         152 :     return _data;
      56             : }
      57             : 
      58             : template< class T > void Buffer< T >::grow( const uint64_t newSize )
      59             : {
      60             :     if( newSize > _size )
      61             :         resize( newSize );
      62             : }
      63             : 
      64        1132 : template< class T > T* Buffer< T >::reserve( const uint64_t newSize )
      65             : {
      66        1132 :     if( newSize <= _maxSize )
      67         114 :         return _data;
      68             : 
      69        1018 :     _data = static_cast< T* >( realloc( _data, newSize * sizeof( T )));
      70        1018 :     _maxSize = newSize;
      71        1018 :     return _data;
      72             : }
      73             : 
      74             : template< class T > T* Buffer< T >::reset( const uint64_t newSize )
      75             : {
      76             :     reserve( newSize );
      77             :     setSize( newSize );
      78             :     return _data;
      79             : }
      80             : 
      81             : template< class T >
      82             : void Buffer< T >::append( const T* data, const uint64_t size )
      83             : {
      84             :     LBASSERT( data );
      85             :     LBASSERT( size );
      86             : 
      87             :     const uint64_t oldSize = _size;
      88             :     resize( oldSize + size );
      89             :     memcpy( _data + oldSize, data, size * sizeof( T ));
      90             : }
      91             : 
      92             : template< class T > void Buffer< T >::append( const T& element )
      93             : {
      94             :     resize( _size + 1 );
      95             :     _data[ _size - 1 ] = element;
      96             : }
      97             : 
      98             : template< class T >
      99             : void Buffer< T >::replace( const void* data, const uint64_t size )
     100             : {
     101             :     LBASSERT( data );
     102             :     LBASSERT( size );
     103             : 
     104             :     reserve( size );
     105             :     memcpy( _data, data, size * sizeof( T ));
     106             :     _size = size;
     107             : }
     108             : 
     109             : template< class T > void Buffer< T >::swap( Buffer< T >& buffer )
     110             : {
     111             :     T*             tmpData    = buffer._data;
     112             :     const uint64_t tmpSize    = buffer._size;
     113             :     const uint64_t tmpMaxSize = buffer._maxSize;
     114             : 
     115             :     buffer._data = _data;
     116             :     buffer._size = _size;
     117             :     buffer._maxSize = _maxSize;
     118             : 
     119             :     _data     = tmpData;
     120             :     _size     = tmpSize;
     121             :     _maxSize = tmpMaxSize;
     122             : }
     123             : 
     124         974 : template< class T > bool Buffer< T >::setSize( const uint64_t size )
     125             : {
     126         974 :     LBASSERT( size <= _maxSize );
     127         974 :     if( size > _maxSize )
     128           0 :         return false;
     129             : 
     130         974 :     _size = size;
     131         974 :     return true;
     132             : }
     133             : }

Generated by: LCOV version 1.10