10#ifndef MSGPACK_V1_VREFBUFFER_HPP
11#define MSGPACK_V1_VREFBUFFER_HPP
26#if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
64 :m_ref_size(std::max(ref_size, detail::packer_max_buffer_size + 1)),
65 m_chunk_size(chunk_size)
67 if((
sizeof(chunk) + chunk_size) < chunk_size) {
68 throw std::bad_alloc();
71 size_t nfirst = (
sizeof(
iovec) < 72/2) ?
72 72 /
sizeof(
iovec) : 8;
75 sizeof(
iovec) * nfirst));
77 throw std::bad_alloc();
81 m_end = array + nfirst;
84 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + chunk_size));
87 throw std::bad_alloc();
89 inner_buffer*
const ib = &m_inner_buffer;
91 ib->free = chunk_size;
92 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
100 chunk* c = m_inner_buffer.head;
114 void write(
const char* buf,
size_t len)
120 if(len < m_ref_size) {
129 if(m_tail == m_end) {
130 const size_t nused =
static_cast<size_t>(m_tail - m_array);
131 const size_t nnext = nused * 2;
134 m_array,
sizeof(
iovec)*nnext));
136 throw std::bad_alloc();
140 m_end = nvec + nnext;
141 m_tail = nvec + nused;
144 m_tail->
iov_base =
const_cast<char*
>(buf);
151 inner_buffer*
const ib = &m_inner_buffer;
154 size_t sz = m_chunk_size;
159 if(
sizeof(chunk) + sz < sz){
160 throw std::bad_alloc();
163 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
165 throw std::bad_alloc();
171 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
175 std::memcpy(m, buf, len);
179 if(m_tail != m_array && m ==
180 static_cast<const char*
>(
181 const_cast<const void *
>((m_tail - 1)->iov_base)
182 ) + (m_tail - 1)->iov_len) {
183 (m_tail - 1)->iov_len += len;
197 return static_cast<size_t>(m_tail - m_array);
202 size_t sz = m_chunk_size;
204 if((
sizeof(chunk) + sz) < sz){
205 throw std::bad_alloc();
208 chunk* empty =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
210 throw std::bad_alloc();
215 const size_t nused =
static_cast<size_t>(m_tail - m_array);
216 if(to->m_tail + nused < m_end) {
217 const size_t tosize =
static_cast<size_t>(to->m_tail - to->m_array);
218 const size_t reqsize = nused + tosize;
219 size_t nnext =
static_cast<size_t>(to->m_end - to->m_array) * 2;
220 while(nnext < reqsize) {
221 size_t tmp_nnext = nnext * 2;
222 if (tmp_nnext <= nnext) {
230 to->m_array,
sizeof(
iovec)*nnext));
233 throw std::bad_alloc();
237 to->m_end = nvec + nnext;
238 to->m_tail = nvec + tosize;
241 std::memcpy(to->m_tail, m_array,
sizeof(
iovec)*nused);
247 inner_buffer*
const ib = &m_inner_buffer;
248 inner_buffer*
const toib = &to->m_inner_buffer;
250 chunk* last = ib->head;
254 last->next = toib->head;
255 toib->head = ib->head;
257 if(toib->free < ib->free) {
258 toib->free = ib->free;
264 ib->ptr =
reinterpret_cast<char*
>(empty) +
sizeof(chunk);
270 chunk* c = m_inner_buffer.head->next;
278 inner_buffer*
const ib = &m_inner_buffer;
281 ib->free = m_chunk_size;
282 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
287#if defined(MSGPACK_USE_CPP03)
304 inner_buffer m_inner_buffer;
#define MSGPACK_ASSERT
Definition assert.hpp:22
Definition vrefbuffer.hpp:51
vrefbuffer(const vrefbuffer &)=delete
~vrefbuffer()
Definition vrefbuffer.hpp:98
const iovec * vector() const
Definition vrefbuffer.hpp:190
void clear()
Definition vrefbuffer.hpp:268
void write(const char *buf, size_t len)
Definition vrefbuffer.hpp:114
vrefbuffer(size_t ref_size=MSGPACK_VREFBUFFER_REF_SIZE, size_t chunk_size=MSGPACK_VREFBUFFER_CHUNK_SIZE)
Definition vrefbuffer.hpp:62
vrefbuffer & operator=(const vrefbuffer &)=delete
void append_ref(const char *buf, size_t len)
Definition vrefbuffer.hpp:127
void migrate(vrefbuffer *to)
Definition vrefbuffer.hpp:200
size_t vector_size() const
Definition vrefbuffer.hpp:195
void append_copy(const char *buf, size_t len)
Definition vrefbuffer.hpp:149
std::size_t const packer_max_buffer_size
Definition vrefbuffer.hpp:48
Definition adaptor_base.hpp:15
Definition vrefbuffer.hpp:33
size_t iov_len
Definition vrefbuffer.hpp:35
void * iov_base
Definition vrefbuffer.hpp:34
#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