Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:52

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
0003 // Author: Lukasz Janyst <ljanyst@cern.ch>
0004 //------------------------------------------------------------------------------
0005 // XRootD is free software: you can redistribute it and/or modify
0006 // it under the terms of the GNU Lesser General Public License as published by
0007 // the Free Software Foundation, either version 3 of the License, or
0008 // (at your option) any later version.
0009 //
0010 // XRootD is distributed in the hope that it will be useful,
0011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 // GNU General Public License for more details.
0014 //
0015 // You should have received a copy of the GNU Lesser General Public License
0016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0017 //------------------------------------------------------------------------------
0018 
0019 #ifndef __XRD_CL_BUFFER_HH__
0020 #define __XRD_CL_BUFFER_HH__
0021 
0022 #include <cstdlib>
0023 #include <cstdint>
0024 #include <new>
0025 #include <cstring>
0026 #include <string>
0027 
0028 namespace XrdCl
0029 {
0030   //----------------------------------------------------------------------------
0031   //! Binary blob representation
0032   //----------------------------------------------------------------------------
0033   class Buffer
0034   {
0035     public:
0036       //------------------------------------------------------------------------
0037       //! Constructor
0038       //------------------------------------------------------------------------
0039       Buffer( uint32_t size = 0 ): pBuffer(0), pSize(0), pCursor(0)
0040       {
0041         if( size )
0042         {
0043           Allocate( size );
0044         }
0045       }
0046 
0047       //------------------------------------------------------------------------
0048       //! Move Constructor
0049       //------------------------------------------------------------------------
0050       Buffer( Buffer &&buffer )
0051       {
0052         Steal( std::move( buffer ) );
0053       }
0054 
0055       //------------------------------------------------------------------------
0056       //! Move assignment operator
0057       //------------------------------------------------------------------------
0058       Buffer& operator=( Buffer && buffer )
0059       {
0060         Steal( std::move( buffer ) );
0061         return *this;
0062       }
0063 
0064       //------------------------------------------------------------------------
0065       //! Destructor
0066       //------------------------------------------------------------------------
0067       virtual ~Buffer() { Free(); }
0068 
0069       //------------------------------------------------------------------------
0070       //! Get the message buffer
0071       //------------------------------------------------------------------------
0072       const char *GetBuffer( uint32_t offset = 0 ) const
0073       {
0074         return pBuffer+offset;
0075       }
0076 
0077       //------------------------------------------------------------------------
0078       //! Get the message buffer
0079       //------------------------------------------------------------------------
0080       char *GetBuffer( uint32_t offset = 0 )
0081       {
0082         return pBuffer+offset;
0083       }
0084 
0085       //------------------------------------------------------------------------
0086       //! Reallocate the buffer to a new location of a given size
0087       //------------------------------------------------------------------------
0088       void ReAllocate( uint32_t size )
0089       {
0090         pBuffer = (char *)realloc( pBuffer, size );
0091         if( !pBuffer )
0092           throw std::bad_alloc();
0093         pSize = size;
0094       }
0095 
0096       //------------------------------------------------------------------------
0097       //! Free the buffer
0098       //------------------------------------------------------------------------
0099       void Free()
0100       {
0101         free( pBuffer );
0102         pBuffer = 0;
0103         pSize   = 0;
0104         pCursor = 0;
0105       }
0106 
0107       //------------------------------------------------------------------------
0108       //! Allocate the buffer
0109       //------------------------------------------------------------------------
0110       void Allocate( uint32_t size )
0111       {
0112         if( !size )
0113          return;
0114 
0115         pBuffer = (char *)malloc( size );
0116         if( !pBuffer )
0117           throw std::bad_alloc();
0118         pSize = size;
0119       }
0120 
0121       //------------------------------------------------------------------------
0122       //! Zero
0123       //------------------------------------------------------------------------
0124       void Zero()
0125       {
0126         memset( pBuffer, 0, pSize );
0127       }
0128 
0129       //------------------------------------------------------------------------
0130       //! Get the size of the message
0131       //------------------------------------------------------------------------
0132       uint32_t GetSize() const
0133       {
0134         return pSize;
0135       }
0136 
0137       //------------------------------------------------------------------------
0138       //! Get append cursor
0139       //------------------------------------------------------------------------
0140       uint32_t GetCursor() const
0141       {
0142         return pCursor;
0143       }
0144 
0145       //------------------------------------------------------------------------
0146       //! Set the cursor
0147       //------------------------------------------------------------------------
0148       void SetCursor( uint32_t cursor )
0149       {
0150         pCursor = cursor;
0151       }
0152 
0153       //------------------------------------------------------------------------
0154       //! Advance the cursor
0155       //------------------------------------------------------------------------
0156       void AdvanceCursor( uint32_t delta )
0157       {
0158         pCursor += delta;
0159       }
0160 
0161       //------------------------------------------------------------------------
0162       //! Append data at the position pointed to by the append cursor
0163       //------------------------------------------------------------------------
0164       void Append( const char *buffer, uint32_t size )
0165       {
0166         uint32_t remaining = pSize-pCursor;
0167         if( remaining < size )
0168           ReAllocate( pCursor+size );
0169 
0170         memcpy( pBuffer+pCursor, buffer, size );
0171         pCursor += size;
0172       }
0173 
0174       //------------------------------------------------------------------------
0175       //! Append data at the given offset
0176       //------------------------------------------------------------------------
0177       void Append( const char *buffer, uint32_t size, uint32_t offset )
0178       {
0179         uint32_t remaining = pSize-offset;
0180         if( remaining < size )
0181           ReAllocate( offset+size );
0182 
0183         memcpy( pBuffer+offset, buffer, size );
0184       }
0185 
0186       //------------------------------------------------------------------------
0187       //! Get the buffer pointer at the append cursor
0188       //------------------------------------------------------------------------
0189       char *GetBufferAtCursor()
0190       {
0191         return GetBuffer( pCursor );
0192       }
0193 
0194       //------------------------------------------------------------------------
0195       //! Get the buffer pointer at the append cursor
0196       //------------------------------------------------------------------------
0197       const char *GetBufferAtCursor() const
0198       {
0199         return GetBuffer( pCursor );
0200       }
0201 
0202       //------------------------------------------------------------------------
0203       //! Fill the buffer from a string
0204       //------------------------------------------------------------------------
0205       void FromString( const std::string str )
0206       {
0207         ReAllocate( str.length()+1 );
0208         memcpy( pBuffer, str.c_str(), str.length() );
0209         pBuffer[str.length()] = 0;
0210       }
0211 
0212       //------------------------------------------------------------------------
0213       //! Convert the buffer to a string
0214       //------------------------------------------------------------------------
0215       std::string ToString() const
0216       {
0217         char *bf = new char[pSize+1];
0218         bf[pSize] = 0;
0219         memcpy( bf, pBuffer, pSize );
0220         std::string tmp = bf;
0221         delete [] bf;
0222         return tmp;
0223       }
0224 
0225       //------------------------------------------------------------------------
0226       //! Grab a buffer allocated outside
0227       //------------------------------------------------------------------------
0228       void Grab( char *buffer, uint32_t size )
0229       {
0230         Free();
0231         pBuffer = buffer;
0232         pSize   = size;
0233       }
0234 
0235       //------------------------------------------------------------------------
0236       //! Release the buffer
0237       //------------------------------------------------------------------------
0238       char *Release()
0239       {
0240         char *buffer = pBuffer;
0241         pBuffer = 0;
0242         pSize   = 0;
0243         pCursor = 0;
0244         return buffer;
0245       }
0246 
0247     protected:
0248 
0249       void Steal( Buffer &&buffer )
0250       {
0251         pBuffer = buffer.pBuffer;
0252         buffer.pBuffer = 0;
0253 
0254         pSize = buffer.pSize;
0255         buffer.pSize = 0;
0256 
0257         pCursor = buffer.pCursor;
0258         buffer.pCursor = 0;
0259       }
0260 
0261     private:
0262 
0263       Buffer( const Buffer& );
0264       Buffer& operator=( const Buffer& );
0265 
0266       char     *pBuffer;
0267       uint32_t  pSize;
0268       uint32_t  pCursor;
0269   };
0270 }
0271 
0272 #endif // __XRD_CL_BUFFER_HH__