0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StaticBuffer.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 
27 #ifndef Common_StaticBuffer_h
28 #define Common_StaticBuffer_h
29 
30 #include "DynamicBuffer.h"
31 #include "Logger.h"
32 
33 #include <cstdlib>
34 #include <memory>
35 
36 namespace Hypertable {
37 
40 
45  class StaticBuffer {
46  public:
49  : base(0), alignment(0), size(0), own(true) {
50  }
51 
61  explicit StaticBuffer(size_t len, size_t alignment=0)
62  : alignment(alignment), size(len), own(true) {
63  if (alignment > 0) {
64  void *vptr = 0;
65  size_t aligned_len = (len % alignment) == 0 ? len :
66  ((len / alignment)+1)*alignment;
67  HT_ASSERT(posix_memalign(&vptr, alignment, aligned_len) == 0);
68  base = (uint8_t *)vptr;
69  }
70  else
71  base = new uint8_t[len];
72  }
73 
83  StaticBuffer(void *data, uint32_t len, bool take_ownership = true)
84  : base((uint8_t *)data), alignment(0), size(len), own(take_ownership) {
85  }
86 
89  base = dbuf.base;
90  size = dbuf.fill();
91  own = dbuf.own;
92  alignment = 0;
93  if (own) {
94  dbuf.base = dbuf.ptr = 0;
95  dbuf.size = 0;
96  }
97  }
98 
101  free();
102  }
103 
117  base = other.base;
118  size = other.size;
119  own = other.own;
120  alignment = other.alignment;
121  if (own) {
122  other.own = false;
123  other.base = 0;
124  }
125  }
126 
140  base = other.base;
141  size = other.size;
142  own = other.own;
143  alignment = other.alignment;
144  if (own) {
145  other.own = false;
146  other.base = 0;
147  }
148  return *this;
149  }
150 
156  base = dbuf.base;
157  size = dbuf.fill();
158  own = dbuf.own;
159  alignment = 0;
160  if (own) {
161  dbuf.base = dbuf.ptr = 0;
162  dbuf.size = 0;
163  }
164  return *this;
165  }
166 
175  void set(uint8_t *data, uint32_t len, bool take_ownership = true) {
176  free();
177  base = data;
178  size = len;
179  own = take_ownership;
180  alignment = 0;
181  }
182 
185  void free() {
186  if (own && base) {
187  if (alignment > 0)
188  ::free(base);
189  else
190  delete [] base;
191  }
192  base = 0;
193  size = 0;
194  }
195 
196  size_t aligned_size() {
197  if (alignment == 0 || (size % alignment) == 0)
198  return size;
199  return ((size / alignment)+1) * alignment;
200  }
201 
202  uint8_t *base;
203  size_t alignment;
204  uint32_t size;
205  bool own;
206  };
207 
209  inline bool operator<(const StaticBuffer& sb1, const StaticBuffer& sb2) {
210  size_t len = (sb1.size < sb2.size) ? sb1.size : sb2.size;
211  int cmp = memcmp(sb1.base, sb2.base, len);
212  return (cmp==0) ? sb1.size < sb2.size : cmp < 0;
213  }
214 
216  inline bool operator==(const StaticBuffer& sb1, const StaticBuffer& sb2) {
217  if (sb1.size != sb2.size)
218  return false;
219  size_t len = (sb1.size < sb2.size) ? sb1.size : sb2.size;
220  int equal = memcmp(sb1.base, sb2.base, len);
221  if (equal == 0 )
222  return true;
223  return false;
224  }
225 
227  inline bool operator!=(const StaticBuffer& sb1, const StaticBuffer& sb2) {
228  return !(sb1 == sb2);
229  }
230 
232  typedef std::shared_ptr<StaticBuffer> StaticBufferPtr;
233 
235 
236 } // namespace Hypertable
237 
238 #endif // Common_StaticBuffer_h
A memory buffer of static size.
Definition: StaticBuffer.h:45
StaticBuffer()
Constructor.
Definition: StaticBuffer.h:48
StaticBuffer & operator=(DynamicBuffer &dbuf)
Assignment operator for DynamicBuffer.
Definition: StaticBuffer.h:155
void set(uint8_t *data, uint32_t len, bool take_ownership=true)
Sets data pointer; the existing buffer is discarded and deleted.
Definition: StaticBuffer.h:175
StaticBuffer(StaticBuffer &other)
Copy constructor.
Definition: StaticBuffer.h:116
StaticBuffer(DynamicBuffer &dbuf)
Constructor; takes ownership from a DynamicBuffer.
Definition: StaticBuffer.h:88
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
#define HT_ASSERT(_e_)
Definition: Logger.h:396
StaticBuffer(size_t len, size_t alignment=0)
Constructor.
Definition: StaticBuffer.h:61
A dynamic, resizable memory buffer.
uint32_t size
The size of the allocated memory buffer (base)
Logging routines and macros.
StaticBuffer & operator=(StaticBuffer &other)
Assignment operator.
Definition: StaticBuffer.h:139
bool operator==(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:112
void free()
Clears the data; if this object is owner of the data then the allocated buffer is delete[]d...
Definition: StaticBuffer.h:185
bool own
If true then the buffer (base) will be released when going out of scope; if false then the caller has...
bool operator!=(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:122
bool operator<(const directory_entry< _Key, _Tp > &lhs, const directory_entry< _Key, _Tp > &rhs)
Definition: directory.h:128
Hypertable definitions
StaticBuffer(void *data, uint32_t len, bool take_ownership=true)
Constructor; assigns an existing buffer and can take ownership of that buffer.
Definition: StaticBuffer.h:83
bool equal(double a, double b)
Compare doubles that may have been serialized and unserialized.
uint8_t * base
Pointer to the allocated memory buffer.
size_t fill() const
Returns the size of the used portion.
Definition: DynamicBuffer.h:70
~StaticBuffer()
Destructor; if "own" is true then the buffer will be delete[]d.
Definition: StaticBuffer.h:100
std::shared_ptr< StaticBuffer > StaticBufferPtr
Smart pointer to StaticBuffer.
Definition: StaticBuffer.h:232