xrootd
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 <stdint.h>
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  virtual ~Buffer() { Free(); }
51 
52  //------------------------------------------------------------------------
54  //------------------------------------------------------------------------
55  const char *GetBuffer( uint32_t offset = 0 ) const
56  {
57  return pBuffer+offset;
58  }
59 
60  //------------------------------------------------------------------------
62  //------------------------------------------------------------------------
63  char *GetBuffer( uint32_t offset = 0 )
64  {
65  return pBuffer+offset;
66  }
67 
68  //------------------------------------------------------------------------
70  //------------------------------------------------------------------------
71  void ReAllocate( uint32_t size )
72  {
73  pBuffer = (char *)realloc( pBuffer, size );
74  if( !pBuffer )
75  throw std::bad_alloc();
76  pSize = size;
77  }
78 
79  //------------------------------------------------------------------------
81  //------------------------------------------------------------------------
82  void Free()
83  {
84  free( pBuffer );
85  pBuffer = 0;
86  pSize = 0;
87  pCursor = 0;
88  }
89 
90  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
93  void Allocate( uint32_t size )
94  {
95  if( !size )
96  return;
97 
98  pBuffer = (char *)malloc( size );
99  if( !pBuffer )
100  throw std::bad_alloc();
101  pSize = size;
102  }
103 
104  //------------------------------------------------------------------------
106  //------------------------------------------------------------------------
107  void Zero()
108  {
109  memset( pBuffer, 0, pSize );
110  }
111 
112  //------------------------------------------------------------------------
114  //------------------------------------------------------------------------
115  uint32_t GetSize() const
116  {
117  return pSize;
118  }
119 
120  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
123  uint32_t GetCursor() const
124  {
125  return pCursor;
126  }
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  void SetCursor( uint32_t cursor )
132  {
133  pCursor = cursor;
134  }
135 
136  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  void AdvanceCursor( uint32_t delta )
140  {
141  pCursor += delta;
142  }
143 
144  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  void Append( const char *buffer, uint32_t size )
148  {
149  uint32_t remaining = pSize-pCursor;
150  if( remaining < size )
151  ReAllocate( pCursor+size );
152 
153  memcpy( pBuffer+pCursor, buffer, size );
154  pCursor += size;
155  }
156 
157  //------------------------------------------------------------------------
159  //------------------------------------------------------------------------
160  void Append( const char *buffer, uint32_t size, uint32_t offset )
161  {
162  uint32_t remaining = pSize-offset;
163  if( remaining < size )
164  ReAllocate( offset+size );
165 
166  memcpy( pBuffer+offset, buffer, size );
167  }
168 
169  //------------------------------------------------------------------------
171  //------------------------------------------------------------------------
173  {
174  return GetBuffer( pCursor );
175  }
176 
177  //------------------------------------------------------------------------
179  //------------------------------------------------------------------------
180  const char *GetBufferAtCursor() const
181  {
182  return GetBuffer( pCursor );
183  }
184 
185  //------------------------------------------------------------------------
187  //------------------------------------------------------------------------
188  void FromString( const std::string str )
189  {
190  ReAllocate( str.length()+1 );
191  memcpy( pBuffer, str.c_str(), str.length() );
192  pBuffer[str.length()] = 0;
193  }
194 
195  //------------------------------------------------------------------------
197  //------------------------------------------------------------------------
198  std::string ToString() const
199  {
200  char *bf = new char[pSize+1];
201  bf[pSize] = 0;
202  memcpy( bf, pBuffer, pSize );
203  std::string tmp = bf;
204  delete [] bf;
205  return tmp;
206  }
207 
208  //------------------------------------------------------------------------
210  //------------------------------------------------------------------------
211  void Grab( char *buffer, uint32_t size )
212  {
213  Free();
214  pBuffer = buffer;
215  pSize = size;
216  }
217 
218  //------------------------------------------------------------------------
220  //------------------------------------------------------------------------
221  char *Release()
222  {
223  char *buffer = pBuffer;
224  pBuffer = 0;
225  pSize = 0;
226  pCursor = 0;
227  return buffer;
228  }
229 
230  private:
231  char *pBuffer;
232  uint32_t pSize;
233  uint32_t pCursor;
234  };
235 }
236 
237 #endif // __XRD_CL_BUFFER_HH__
const char * GetBufferAtCursor() const
Get the buffer pointer at the append cursor.
Definition: XrdClBuffer.hh:180
uint32_t pCursor
Definition: XrdClBuffer.hh:233
char * pBuffer
Definition: XrdClBuffer.hh:231
void Allocate(uint32_t size)
Allocate the buffer.
Definition: XrdClBuffer.hh:93
void SetCursor(uint32_t cursor)
Set the cursor.
Definition: XrdClBuffer.hh:131
char * Release()
Release the buffer.
Definition: XrdClBuffer.hh:221
void Append(const char *buffer, uint32_t size, uint32_t offset)
Append data at the given offset.
Definition: XrdClBuffer.hh:160
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:55
void Free()
Free the buffer.
Definition: XrdClBuffer.hh:82
uint32_t GetSize() const
Get the size of the message.
Definition: XrdClBuffer.hh:115
void Grab(char *buffer, uint32_t size)
Grab a buffer allocated outside.
Definition: XrdClBuffer.hh:211
void AdvanceCursor(uint32_t delta)
Advance the cursor.
Definition: XrdClBuffer.hh:139
char * GetBuffer(uint32_t offset=0)
Get the message buffer.
Definition: XrdClBuffer.hh:63
uint32_t pSize
Definition: XrdClBuffer.hh:232
void Append(const char *buffer, uint32_t size)
Append data at the position pointed to by the append cursor.
Definition: XrdClBuffer.hh:147
void FromString(const std::string str)
Fill the buffer from a string.
Definition: XrdClBuffer.hh:188
char * GetBufferAtCursor()
Get the buffer pointer at the append cursor.
Definition: XrdClBuffer.hh:172
virtual ~Buffer()
Destructor.
Definition: XrdClBuffer.hh:50
void ReAllocate(uint32_t size)
Reallocate the buffer to a new location of a given size.
Definition: XrdClBuffer.hh:71
uint32_t GetCursor() const
Get append cursor.
Definition: XrdClBuffer.hh:123
std::string ToString() const
Convert the buffer to a string.
Definition: XrdClBuffer.hh:198
void Zero()
Zero.
Definition: XrdClBuffer.hh:107
Buffer(uint32_t size=0)
Constructor.
Definition: XrdClBuffer.hh:39
Binary blob representation.
Definition: XrdClBuffer.hh:33