10#ifndef MSGPACK_V1_VREFBUFFER_HPP
11#define MSGPACK_V1_VREFBUFFER_HPP
25#if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
58 :m_ref_size(std::max(ref_size, detail::packer_max_buffer_size + 1)),
59 m_chunk_size(chunk_size)
61 if((
sizeof(chunk) + chunk_size) < chunk_size) {
62 throw std::bad_alloc();
65 size_t nfirst = (
sizeof(
iovec) < 72/2) ?
66 72 /
sizeof(
iovec) : 8;
69 sizeof(
iovec) * nfirst));
71 throw std::bad_alloc();
75 m_end = array + nfirst;
78 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + chunk_size));
81 throw std::bad_alloc();
83 inner_buffer*
const ib = &m_inner_buffer;
85 ib->free = chunk_size;
86 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
94 chunk* c = m_inner_buffer.head;
108 void write(
const char* buf,
size_t len)
110 if(len < m_ref_size) {
119 if(m_tail == m_end) {
120 const size_t nused =
static_cast<size_t>(m_tail - m_array);
121 const size_t nnext = nused * 2;
124 m_array,
sizeof(
iovec)*nnext));
126 throw std::bad_alloc();
130 m_end = nvec + nnext;
131 m_tail = nvec + nused;
134 m_tail->
iov_base =
const_cast<char*
>(buf);
141 inner_buffer*
const ib = &m_inner_buffer;
144 size_t sz = m_chunk_size;
149 if(
sizeof(chunk) + sz < sz){
150 throw std::bad_alloc();
153 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
155 throw std::bad_alloc();
161 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
165 std::memcpy(m, buf, len);
169 if(m_tail != m_array && m ==
170 static_cast<const char*
>(
171 const_cast<const void *
>((m_tail - 1)->iov_base)
172 ) + (m_tail - 1)->iov_len) {
173 (m_tail - 1)->iov_len += len;
187 return static_cast<size_t>(m_tail - m_array);
192 size_t sz = m_chunk_size;
194 if((
sizeof(chunk) + sz) < sz){
195 throw std::bad_alloc();
198 chunk* empty =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
200 throw std::bad_alloc();
205 const size_t nused =
static_cast<size_t>(m_tail - m_array);
206 if(to->m_tail + nused < m_end) {
207 const size_t tosize =
static_cast<size_t>(to->m_tail - to->m_array);
208 const size_t reqsize = nused + tosize;
209 size_t nnext =
static_cast<size_t>(to->m_end - to->m_array) * 2;
210 while(nnext < reqsize) {
211 size_t tmp_nnext = nnext * 2;
212 if (tmp_nnext <= nnext) {
220 to->m_array,
sizeof(
iovec)*nnext));
223 throw std::bad_alloc();
227 to->m_end = nvec + nnext;
228 to->m_tail = nvec + tosize;
231 std::memcpy(to->m_tail, m_array,
sizeof(
iovec)*nused);
237 inner_buffer*
const ib = &m_inner_buffer;
238 inner_buffer*
const toib = &to->m_inner_buffer;
240 chunk* last = ib->head;
244 last->next = toib->head;
245 toib->head = ib->head;
247 if(toib->free < ib->free) {
248 toib->free = ib->free;
254 ib->ptr =
reinterpret_cast<char*
>(empty) +
sizeof(chunk);
260 chunk* c = m_inner_buffer.head->next;
268 inner_buffer*
const ib = &m_inner_buffer;
271 ib->free = m_chunk_size;
272 ib->ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
277#if defined(MSGPACK_USE_CPP03)
294 inner_buffer m_inner_buffer;
Definition vrefbuffer.hpp:45
vrefbuffer(const vrefbuffer &)=delete
~vrefbuffer()
Definition vrefbuffer.hpp:92
void clear()
Definition vrefbuffer.hpp:258
void write(const char *buf, size_t len)
Definition vrefbuffer.hpp:108
const struct iovec * vector() const
Definition vrefbuffer.hpp:180
vrefbuffer(size_t ref_size=MSGPACK_VREFBUFFER_REF_SIZE, size_t chunk_size=MSGPACK_VREFBUFFER_CHUNK_SIZE)
Definition vrefbuffer.hpp:56
vrefbuffer & operator=(const vrefbuffer &)=delete
void append_ref(const char *buf, size_t len)
Definition vrefbuffer.hpp:117
void migrate(vrefbuffer *to)
Definition vrefbuffer.hpp:190
size_t vector_size() const
Definition vrefbuffer.hpp:185
void append_copy(const char *buf, size_t len)
Definition vrefbuffer.hpp:139
std::size_t const packer_max_buffer_size
Definition vrefbuffer.hpp:42
Definition adaptor_base.hpp:15
Definition vrefbuffer.hpp:28
void * iov_base
Definition vrefbuffer.hpp:29
size_t iov_len
Definition vrefbuffer.hpp:30
#define MSGPACK_NULLPTR
Definition cpp_config_decl.hpp:35
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:58
#define MSGPACK_VREFBUFFER_CHUNK_SIZE
Definition vrefbuffer_decl.hpp:22
#define MSGPACK_VREFBUFFER_REF_SIZE
Definition vrefbuffer_decl.hpp:18