MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UtilBuffer.hpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef __BUFFER_HPP_INCLUDED__
19 #define __BUFFER_HPP_INCLUDED__
20 
21 #include <ndb_global.h>
22 
23 /* This class represents a buffer of binary data, where you can append
24  * data at the end, and later read the entire bunch.
25  * It will take care of the hairy details of realloc()ing the space
26  * for you
27  */
28 class UtilBuffer {
29 public:
30  UtilBuffer() { data = NULL; len = 0; alloc_size = 0; };
31  ~UtilBuffer() { if(data) free(data); data = NULL; len = 0; alloc_size = 0; };
32 
33 
34  int reallocate(size_t newsize) {
35  if(newsize < len) {
36  errno = EINVAL;
37  return -1;
38  }
39  void *newdata;
40  if((newdata = realloc(data, newsize)) == NULL) {
41  errno = ENOMEM;
42  return -1;
43  }
44  alloc_size = newsize;
45  data = newdata;
46  return 0;
47  };
48 
49  int grow(size_t l) {
50  if(l > alloc_size)
51  return reallocate(l);
52  return 0;
53  };
54 
55  int append(const void *d, size_t l) {
56  int ret;
57  ret = grow(len+l);
58  if(ret != 0)
59  return ret;
60 
61  memcpy((char *)data+len, d, l);
62  len+=l;
63 
64  return 0;
65  };
66 
67  void * append(size_t l){
68  if(grow(len+l) != 0)
69  return 0;
70 
71  void * ret = (char*)data+len;
72  len += l;
73  return ret;
74  }
75 
76  int assign(const void * d, size_t l) {
77  /* Free the old data only after copying, in case d==data. */
78  void *old_data= data;
79  data = NULL;
80  len = 0;
81  alloc_size = 0;
82  int ret= append(d, l);
83  if (old_data)
84  free(old_data);
85  return ret;
86  }
87 
88  void clear() {
89  len = 0;
90  }
91 
92  int length() const { assert(Uint64(len) == Uint32(len)); return (int)len; }
93 
94  void *get_data() const { return data; }
95 
96  bool empty () const { return len == 0; }
97 
98  bool equal(const UtilBuffer &cmp) const {
99  if(len==0 && cmp.len==0)
100  return true;
101  else if(len!=cmp.len)
102  return false;
103  else
104  return (memcmp(get_data(), cmp.get_data(), len) == 0);
105  }
106 
107  int assign(const UtilBuffer& buf) {
108  int ret = 0;
109  if(this != &buf) {
110  ret = assign(buf.get_data(), buf.length());
111  }
112  return ret;
113  }
114 private:
115  void *data; /* Pointer to data storage */
116  size_t len; /* Size of the stored data */
117  size_t alloc_size; /* Size of the allocated space,
118  * i.e. len can grow to this size */
119 };
120 
121 #endif /* !__BUFFER_HPP_INCLUDED__ */