10 #ifndef MSGPACK_V1_VREFBUFFER_HPP
11 #define MSGPACK_V1_VREFBUFFER_HPP
18 #include <boost/assert.hpp>
27 #if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
30 typedef ::iovec iovec;
66 m_chunk_size(chunk_size)
68 if((
sizeof(chunk) + chunk_size) < chunk_size) {
69 throw std::bad_alloc();
72 size_t nfirst = (
sizeof(
iovec) < 72/2) ?
73 72 /
sizeof(
iovec) : 8;
76 sizeof(
iovec) * nfirst));
78 throw std::bad_alloc();
82 m_end = array + nfirst;
85 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + chunk_size));
88 throw std::bad_alloc();
90 inner_buffer*
const ib = &m_inner_buffer;
92 ib->free = chunk_size;
93 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
101 chunk* c = m_inner_buffer.head;
115 void write(
const char* buf,
size_t len)
117 BOOST_ASSERT(buf || len == 0);
121 if(len < m_ref_size) {
130 if(m_tail == m_end) {
131 const size_t nused =
static_cast<size_t>(m_tail - m_array);
132 const size_t nnext = nused * 2;
135 m_array,
sizeof(
iovec)*nnext));
137 throw std::bad_alloc();
141 m_end = nvec + nnext;
142 m_tail = nvec + nused;
145 m_tail->
iov_base =
const_cast<char*
>(buf);
152 inner_buffer*
const ib = &m_inner_buffer;
155 size_t sz = m_chunk_size;
160 if(
sizeof(chunk) + sz < sz){
161 throw std::bad_alloc();
164 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
166 throw std::bad_alloc();
172 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
176 std::memcpy(m, buf, len);
180 if(m_tail != m_array && m ==
181 static_cast<const char*
>(
182 const_cast<const void *
>((m_tail - 1)->iov_base)
183 ) + (m_tail - 1)->iov_len) {
184 (m_tail - 1)->iov_len += len;
198 return static_cast<size_t>(m_tail - m_array);
203 size_t sz = m_chunk_size;
205 if((
sizeof(chunk) + sz) < sz){
206 throw std::bad_alloc();
209 chunk* empty =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
211 throw std::bad_alloc();
216 const size_t nused =
static_cast<size_t>(m_tail - m_array);
217 if(to->m_tail + nused < m_end) {
218 const size_t tosize =
static_cast<size_t>(to->m_tail - to->m_array);
219 const size_t reqsize = nused + tosize;
220 size_t nnext =
static_cast<size_t>(to->m_end - to->m_array) * 2;
221 while(nnext < reqsize) {
222 size_t tmp_nnext = nnext * 2;
223 if (tmp_nnext <= nnext) {
231 to->m_array,
sizeof(
iovec)*nnext));
234 throw std::bad_alloc();
238 to->m_end = nvec + nnext;
239 to->m_tail = nvec + tosize;
242 std::memcpy(to->m_tail, m_array,
sizeof(
iovec)*nused);
248 inner_buffer*
const ib = &m_inner_buffer;
249 inner_buffer*
const toib = &to->m_inner_buffer;
251 chunk* last = ib->head;
255 last->next = toib->head;
256 toib->head = ib->head;
258 if(toib->free < ib->free) {
259 toib->free = ib->free;
265 ib->ptr =
reinterpret_cast<char*
>(empty) +
sizeof(chunk);
271 chunk* c = m_inner_buffer.head->next;
279 inner_buffer*
const ib = &m_inner_buffer;
282 ib->free = m_chunk_size;
283 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
288 #if defined(MSGPACK_USE_CPP03)
305 inner_buffer m_inner_buffer;
Definition: vrefbuffer.hpp:52
vrefbuffer(const vrefbuffer &)=delete
~vrefbuffer()
Definition: vrefbuffer.hpp:99
void clear()
Definition: vrefbuffer.hpp:269
void write(const char *buf, size_t len)
Definition: vrefbuffer.hpp:115
vrefbuffer & operator=(const vrefbuffer &)=delete
vrefbuffer(size_t ref_size=MSGPACK_VREFBUFFER_REF_SIZE, size_t chunk_size=MSGPACK_VREFBUFFER_CHUNK_SIZE)
Definition: vrefbuffer.hpp:63
void append_ref(const char *buf, size_t len)
Definition: vrefbuffer.hpp:128
void migrate(vrefbuffer *to)
Definition: vrefbuffer.hpp:201
const iovec * vector() const
Definition: vrefbuffer.hpp:191
size_t vector_size() const
Definition: vrefbuffer.hpp:196
void append_copy(const char *buf, size_t len)
Definition: vrefbuffer.hpp:150
std::size_t const packer_max_buffer_size
Definition: vrefbuffer.hpp:49
Definition: adaptor_base.hpp:15
Definition: vrefbuffer.hpp:34
size_t iov_len
Definition: vrefbuffer.hpp:36
void * iov_base
Definition: vrefbuffer.hpp:35
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66
#define MSGPACK_VREFBUFFER_CHUNK_SIZE
Definition: vrefbuffer_decl.hpp:22
#define MSGPACK_VREFBUFFER_REF_SIZE
Definition: vrefbuffer_decl.hpp:18