0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DynamicBuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #ifndef Common_DynamicBuffer_h
27 #define Common_DynamicBuffer_h
28 
29 #include <cstdint>
30 #include <cstring>
31 #include <memory>
32 
33 namespace Hypertable {
34 
42  class DynamicBuffer {
43 
44  public:
52  explicit DynamicBuffer(size_t initial_size = 0, bool own_buffer = true)
53  : size(initial_size), own(own_buffer) {
54  if (size)
55  base = ptr = mark = new uint8_t[size];
56  else
57  base = ptr = mark = 0;
58  }
59 
62  if (own)
63  delete [] base;
64  }
65 
67  size_t remaining() const { return size - (ptr - base); }
68 
70  size_t fill() const { return ptr - base; }
71 
73  bool empty() const { return ptr == base; }
74 
82  void ensure(size_t len) {
83  if (len > remaining())
84  grow((fill() + len) * 3 / 2);
85  }
86 
95  void reserve(size_t len, bool nocopy = false) {
96  if (len > remaining())
97  grow(fill() + len, nocopy);
98  }
99 
106  uint8_t *add_unchecked(const void *data, size_t len) {
107  if (data == 0)
108  return 0;
109  uint8_t *rptr = ptr;
110  memcpy(ptr, data, len);
111  ptr += len;
112  return rptr;
113  }
114 
122  uint8_t *add(const void *data, size_t len) {
123  ensure(len);
124  return add_unchecked(data, len);
125  }
126 
132  void set(const void *data, size_t len) {
133  clear();
134  reserve(len);
135  add_unchecked(data, len);
136  }
137 
139  void clear() {
140  ptr = base;
141  }
142 
145  void set_mark() {
146  mark = ptr;
147  }
148 
150  void free() {
151  if (own)
152  delete [] base;
153  base = ptr = mark = 0;
154  size = 0;
155  }
156 
162  uint8_t *release(size_t *lenp = 0) {
163  uint8_t *rbuf = base;
164  if (lenp)
165  *lenp = fill();
166  ptr = base = mark = 0;
167  size = 0;
168  return rbuf;
169  }
170 
176  void grow(size_t new_size, bool nocopy = false) {
177  uint8_t *new_buf = new uint8_t[new_size];
178 
179  if (!nocopy && base)
180  memcpy(new_buf, base, ptr-base);
181 
182  ptr = new_buf + (ptr-base);
183  mark = new_buf + (mark-base);
184  if (own)
185  delete [] base;
186  base = new_buf;
187  size = new_size;
188  }
189 
191  uint8_t *base;
192 
194  uint8_t *ptr;
195 
197  uint8_t *mark;
198 
200  uint32_t size;
201 
204  bool own;
205  };
206 
207  typedef std::shared_ptr<DynamicBuffer> DynamicBufferPtr;
208 
211 }
212 
213 
214 
215 #endif // Common_DynamicBuffer_h
void free()
Frees resources.
bool empty() const
Returns true if the buffer is empty.
Definition: DynamicBuffer.h:73
void set_mark()
Sets the mark; the mark can be used by the caller just like a bookmark.
std::shared_ptr< DynamicBuffer > DynamicBufferPtr
DynamicBuffer(size_t initial_size=0, bool own_buffer=true)
Constructor.
Definition: DynamicBuffer.h:52
uint8_t * ptr
Pointer to the end of the used part of the buffer.
A dynamic, resizable and reference counted memory buffer.
Definition: DynamicBuffer.h:42
void grow(size_t new_size, bool nocopy=false)
Grows the buffer and copies the data unless nocopy is true.
uint8_t * add(const void *data, size_t len)
Adds more data WITH boundary checks; if required the buffer is resized and existing data is preserved...
~DynamicBuffer()
Destructor; releases the buffer if it "owns" it.
Definition: DynamicBuffer.h:61
uint32_t size
The size of the allocated memory buffer (base)
bool own
If true then the buffer (base) will be released when going out of scope; if false then the caller has...
Hypertable definitions
void clear()
Clears the buffer.
uint8_t * base
Pointer to the allocated memory buffer.
size_t fill() const
Returns the size of the used portion.
Definition: DynamicBuffer.h:70
void set(const void *data, size_t len)
Overwrites the existing data.
uint8_t * release(size_t *lenp=0)
Moves ownership of the buffer to the caller.
uint8_t * mark
A "bookmark", can be set by the caller.
void ensure(size_t len)
Ensure space for additional data Will grow the space to 1.5 of the needed space with existing data un...
Definition: DynamicBuffer.h:82
uint8_t * add_unchecked(const void *data, size_t len)
Adds additional data without boundary checks.
size_t remaining() const
Returns the size of the unused portion.
Definition: DynamicBuffer.h:67
void reserve(size_t len, bool nocopy=false)
Reserve space for additional data Will grow the space to exactly what's needed.
Definition: DynamicBuffer.h:95