MessagePack for C++
sbuffer.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ simple buffer implementation
3 //
4 // Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 #ifndef MSGPACK_V1_SBUFFER_HPP
11 #define MSGPACK_V1_SBUFFER_HPP
12 
14 #include "msgpack/assert.hpp"
15 
16 #include <stdexcept>
17 #include <cstring>
18 
19 namespace msgpack {
20 
24 
25 class sbuffer {
26 public:
27  sbuffer(size_t initsz = MSGPACK_SBUFFER_INIT_SIZE):m_size(0), m_alloc(initsz)
28  {
29  if(initsz == 0) {
30  m_data = MSGPACK_NULLPTR;
31  } else {
32  m_data = (char*)::malloc(initsz);
33  if(!m_data) {
34  throw std::bad_alloc();
35  }
36  }
37  }
38 
40  {
41  ::free(m_data);
42  }
43 
44 #if !defined(MSGPACK_USE_CPP03)
45  sbuffer(const sbuffer&) = delete;
46  sbuffer& operator=(const sbuffer&) = delete;
47 
48  sbuffer(sbuffer&& other) :
49  m_size(other.m_size), m_data(other.m_data), m_alloc(other.m_alloc)
50  {
51  other.m_size = other.m_alloc = 0;
52  other.m_data = MSGPACK_NULLPTR;
53  }
54 
56  {
57  ::free(m_data);
58 
59  m_size = other.m_size;
60  m_alloc = other.m_alloc;
61  m_data = other.m_data;
62 
63  other.m_size = other.m_alloc = 0;
64  other.m_data = MSGPACK_NULLPTR;
65 
66  return *this;
67  }
68 #endif // !defined(MSGPACK_USE_CPP03)
69 
70  void write(const char* buf, size_t len)
71  {
72  MSGPACK_ASSERT(buf || len == 0);
73 
74  if (!buf) return;
75 
76  if(m_alloc - m_size < len) {
77  expand_buffer(len);
78  }
79  std::memcpy(m_data + m_size, buf, len);
80  m_size += len;
81  }
82 
83  char* data()
84  {
85  return m_data;
86  }
87 
88  const char* data() const
89  {
90  return m_data;
91  }
92 
93  size_t size() const
94  {
95  return m_size;
96  }
97 
98  char* release()
99  {
100  char* tmp = m_data;
101  m_size = 0;
102  m_data = MSGPACK_NULLPTR;
103  m_alloc = 0;
104  return tmp;
105  }
106 
107  void clear()
108  {
109  m_size = 0;
110  }
111 
112 private:
113  void expand_buffer(size_t len)
114  {
115  size_t nsize = (m_alloc > 0) ?
116  m_alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE;
117 
118  while(nsize < m_size + len) {
119  size_t tmp_nsize = nsize * 2;
120  if (tmp_nsize <= nsize) {
121  nsize = m_size + len;
122  break;
123  }
124  nsize = tmp_nsize;
125  }
126 
127  void* tmp = ::realloc(m_data, nsize);
128  if(!tmp) {
129  throw std::bad_alloc();
130  }
131 
132  m_data = static_cast<char*>(tmp);
133  m_alloc = nsize;
134  }
135 
136 #if defined(MSGPACK_USE_CPP03)
137 private:
138  sbuffer(const sbuffer&);
139  sbuffer& operator=(const sbuffer&);
140 #endif // defined(MSGPACK_USE_CPP03)
141 
142 private:
143  size_t m_size;
144  char* m_data;
145  size_t m_alloc;
146 };
147 
149 } // MSGPACK_API_VERSION_NAMESPACE(v1)
151 
152 } // namespace msgpack
153 
154 #endif // MSGPACK_V1_SBUFFER_HPP
#define MSGPACK_ASSERT
Definition: assert.hpp:22
Definition: sbuffer.hpp:25
char * release()
Definition: sbuffer.hpp:98
sbuffer(sbuffer &&other)
Definition: sbuffer.hpp:48
sbuffer(const sbuffer &)=delete
sbuffer & operator=(const sbuffer &)=delete
sbuffer(size_t initsz=MSGPACK_SBUFFER_INIT_SIZE)
Definition: sbuffer.hpp:27
char * data()
Definition: sbuffer.hpp:83
~sbuffer()
Definition: sbuffer.hpp:39
void clear()
Definition: sbuffer.hpp:107
const char * data() const
Definition: sbuffer.hpp:88
size_t size() const
Definition: sbuffer.hpp:93
sbuffer & operator=(sbuffer &&other)
Definition: sbuffer.hpp:55
void write(const char *buf, size_t len)
Definition: sbuffer.hpp:70
Definition: adaptor_base.hpp:15
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_SBUFFER_INIT_SIZE
Definition: sbuffer_decl.hpp:16
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66