xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdClBuffer.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_BUFFER_HH__
20 #define __XRD_CL_BUFFER_HH__
21 
22 #include <cstdlib>
23 #include <cstdint>
24 #include <new>
25 #include <cstring>
26 #include <string>
27 
28 namespace XrdCl
29 {
30  //----------------------------------------------------------------------------
32  //----------------------------------------------------------------------------
33  class Buffer
34  {
35  public:
36  //------------------------------------------------------------------------
38  //------------------------------------------------------------------------
39  Buffer( uint32_t size = 0 ): pBuffer(0), pSize(0), pCursor(0)
40  {
41  if( size )
42  {
43  Allocate( size );
44  }
45  }
46 
47  //------------------------------------------------------------------------
49  //------------------------------------------------------------------------
50  Buffer( Buffer &&buffer )
51  {
52  Steal( std::move( buffer ) );
53  }
54 
55  //------------------------------------------------------------------------
57  //------------------------------------------------------------------------
58  Buffer& operator=( Buffer && buffer )
59  {
60  Steal( std::move( buffer ) );
61  return *this;
62  }
63 
64  //------------------------------------------------------------------------
66  //------------------------------------------------------------------------
67  virtual ~Buffer() { Free(); }
68 
69  //------------------------------------------------------------------------
71  //------------------------------------------------------------------------
72  const char *GetBuffer( uint32_t offset = 0 ) const
73  {
74  return pBuffer+offset;
75  }
76 
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  char *GetBuffer( uint32_t offset = 0 )
81  {
82  return pBuffer+offset;
83  }
84 
85  //------------------------------------------------------------------------
87  //------------------------------------------------------------------------
88  void ReAllocate( uint32_t size )
89  {
90  pBuffer = (char *)realloc( pBuffer, size );
91  if( !pBuffer )
92  throw std::bad_alloc();
93  pSize = size;
94  }
95 
96  //------------------------------------------------------------------------
98  //------------------------------------------------------------------------
99  void Free()
100  {
101  free( pBuffer );
102  pBuffer = 0;
103  pSize = 0;
104  pCursor = 0;
105  }
106 
107  //------------------------------------------------------------------------
109  //------------------------------------------------------------------------
110  void Allocate( uint32_t size )
111  {
112  if( !size )
113  return;
114 
115  pBuffer = (char *)malloc( size );
116  if( !pBuffer )
117  throw std::bad_alloc();
118  pSize = size;
119  }
120 
121  //------------------------------------------------------------------------
123  //------------------------------------------------------------------------
124  void Zero()
125  {
126  memset( pBuffer, 0, pSize );
127  }
128 
129  //------------------------------------------------------------------------
131  //------------------------------------------------------------------------
132  uint32_t GetSize() const
133  {
134  return pSize;
135  }
136 
137  //------------------------------------------------------------------------
139  //------------------------------------------------------------------------
140  uint32_t GetCursor() const
141  {
142  return pCursor;
143  }
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  void SetCursor( uint32_t cursor )
149  {
150  pCursor = cursor;
151  }
152 
153  //------------------------------------------------------------------------
155  //------------------------------------------------------------------------
156  void AdvanceCursor( uint32_t delta )
157  {
158  pCursor += delta;
159  }
160 
161  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  void Append( const char *buffer, uint32_t size )
165  {
166  uint32_t remaining = pSize-pCursor;
167  if( remaining < size )
168  ReAllocate( pCursor+size );
169 
170  memcpy( pBuffer+pCursor, buffer, size );
171  pCursor += size;
172  }
173 
174  //------------------------------------------------------------------------
176  //------------------------------------------------------------------------
177  void Append( const char *buffer, uint32_t size, uint32_t offset )
178  {
179  uint32_t remaining = pSize-offset;
180  if( remaining < size )
181  ReAllocate( offset+size );
182 
183  memcpy( pBuffer+offset, buffer, size );
184  }
185 
186  //------------------------------------------------------------------------
188  //------------------------------------------------------------------------
190  {
191  return GetBuffer( pCursor );
192  }
193 
194  //------------------------------------------------------------------------
196  //------------------------------------------------------------------------
197  const char *GetBufferAtCursor() const
198  {
199  return GetBuffer( pCursor );
200  }
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  void FromString( const std::string str )
206  {
207  ReAllocate( str.length()+1 );
208  memcpy( pBuffer, str.c_str(), str.length() );
209  pBuffer[str.length()] = 0;
210  }
211 
212  //------------------------------------------------------------------------
214  //------------------------------------------------------------------------
215  std::string ToString() const
216  {
217  char *bf = new char[pSize+1];
218  bf[pSize] = 0;
219  memcpy( bf, pBuffer, pSize );
220  std::string tmp = bf;
221  delete [] bf;
222  return tmp;
223  }
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
228  void Grab( char *buffer, uint32_t size )
229  {
230  Free();
231  pBuffer = buffer;
232  pSize = size;
233  }
234 
235  //------------------------------------------------------------------------
237  //------------------------------------------------------------------------
238  char *Release()
239  {
240  char *buffer = pBuffer;
241  pBuffer = 0;
242  pSize = 0;
243  pCursor = 0;
244  return buffer;
245  }
246 
247  protected:
248 
249  void Steal( Buffer &&buffer )
250  {
251  pBuffer = buffer.pBuffer;
252  buffer.pBuffer = 0;
253 
254  pSize = buffer.pSize;
255  buffer.pSize = 0;
256 
257  pCursor = buffer.pCursor;
258  buffer.pCursor = 0;
259  }
260 
261  private:
262 
263  Buffer( const Buffer& );
264  Buffer& operator=( const Buffer& );
265 
266  char *pBuffer;
267  uint32_t pSize;
268  uint32_t pCursor;
269  };
270 }
271 
272 #endif // __XRD_CL_BUFFER_HH__
const char * GetBufferAtCursor() const
Get the buffer pointer at the append cursor.
Definition: XrdClBuffer.hh:197
Buffer & operator=(Buffer &&buffer)
Move assignment operator.
Definition: XrdClBuffer.hh:58
uint32_t pCursor
Definition: XrdClBuffer.hh:268
char * pBuffer
Definition: XrdClBuffer.hh:266
void Allocate(uint32_t size)
Allocate the buffer.
Definition: XrdClBuffer.hh:110
void SetCursor(uint32_t cursor)
Set the cursor.
Definition: XrdClBuffer.hh:148
char * Release()
Release the buffer.
Definition: XrdClBuffer.hh:238
void Append(const char *buffer, uint32_t size, uint32_t offset)
Append data at the given offset.
Definition: XrdClBuffer.hh:177
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
void Free()
Free the buffer.
Definition: XrdClBuffer.hh:99
uint32_t GetSize() const
Get the size of the message.
Definition: XrdClBuffer.hh:132
void Grab(char *buffer, uint32_t size)
Grab a buffer allocated outside.
Definition: XrdClBuffer.hh:228
Buffer(Buffer &&buffer)
Move Constructor.
Definition: XrdClBuffer.hh:50
void AdvanceCursor(uint32_t delta)
Advance the cursor.
Definition: XrdClBuffer.hh:156
char * GetBuffer(uint32_t offset=0)
Get the message buffer.
Definition: XrdClBuffer.hh:80
uint32_t pSize
Definition: XrdClBuffer.hh:267
void Append(const char *buffer, uint32_t size)
Append data at the position pointed to by the append cursor.
Definition: XrdClBuffer.hh:164
void FromString(const std::string str)
Fill the buffer from a string.
Definition: XrdClBuffer.hh:205
char * GetBufferAtCursor()
Get the buffer pointer at the append cursor.
Definition: XrdClBuffer.hh:189
virtual ~Buffer()
Destructor.
Definition: XrdClBuffer.hh:67
void ReAllocate(uint32_t size)
Reallocate the buffer to a new location of a given size.
Definition: XrdClBuffer.hh:88
uint32_t GetCursor() const
Get append cursor.
Definition: XrdClBuffer.hh:140
std::string ToString() const
Convert the buffer to a string.
Definition: XrdClBuffer.hh:215
void Steal(Buffer &&buffer)
Definition: XrdClBuffer.hh:249
void Zero()
Zero.
Definition: XrdClBuffer.hh:124
Buffer(uint32_t size=0)
Constructor.
Definition: XrdClBuffer.hh:39
Binary blob representation.
Definition: XrdClBuffer.hh:33