/usr/include/msgpack/v1/vrefbuffer.hpp is in libmsgpack-dev 2.1.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | //
// MessagePack for C++ zero-copy buffer implementation
//
// Copyright (C) 2008-2017 FURUHASHI Sadayuki and KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MSGPACK_V1_VREFBUFFER_HPP
#define MSGPACK_V1_VREFBUFFER_HPP
#include "msgpack/v1/vrefbuffer_decl.hpp"
#include <stdexcept>
#include <algorithm>
#if defined(_MSC_VER)
// avoiding confliction std::max, std::min, and macro in windows.h
#ifndef NOMINMAX
#define NOMINMAX
#endif
#endif // defined(_MSC_VER)
#if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
#include <sys/uio.h>
#else
struct iovec {
void *iov_base;
size_t iov_len;
};
#endif
namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace detail {
// int64, uint64, double
std::size_t const packer_max_buffer_size = 9;
} // detail
class vrefbuffer {
private:
struct chunk {
chunk* next;
};
struct inner_buffer {
size_t free;
char* ptr;
chunk* head;
};
public:
vrefbuffer(size_t ref_size = MSGPACK_VREFBUFFER_REF_SIZE,
size_t chunk_size = MSGPACK_VREFBUFFER_CHUNK_SIZE)
:m_ref_size(std::max(ref_size, detail::packer_max_buffer_size + 1)),
m_chunk_size(chunk_size)
{
size_t nfirst = (sizeof(iovec) < 72/2) ?
72 / sizeof(iovec) : 8;
iovec* array = static_cast<iovec*>(::malloc(
sizeof(iovec) * nfirst));
if(!array) {
throw std::bad_alloc();
}
m_tail = array;
m_end = array + nfirst;
m_array = array;
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + chunk_size));
if(!c) {
::free(array);
throw std::bad_alloc();
}
inner_buffer* const ib = &m_inner_buffer;
ib->free = chunk_size;
ib->ptr = reinterpret_cast<char*>(c) + sizeof(chunk);
ib->head = c;
c->next = MSGPACK_NULLPTR;
}
~vrefbuffer()
{
chunk* c = m_inner_buffer.head;
while(true) {
chunk* n = c->next;
::free(c);
if(n != NULL) {
c = n;
} else {
break;
}
}
::free(m_array);
}
public:
void write(const char* buf, size_t len)
{
if(len < m_ref_size) {
append_copy(buf, len);
} else {
append_ref(buf, len);
}
}
void append_ref(const char* buf, size_t len)
{
if(m_tail == m_end) {
const size_t nused = m_tail - m_array;
const size_t nnext = nused * 2;
iovec* nvec = static_cast<iovec*>(::realloc(
m_array, sizeof(iovec)*nnext));
if(!nvec) {
throw std::bad_alloc();
}
m_array = nvec;
m_end = nvec + nnext;
m_tail = nvec + nused;
}
m_tail->iov_base = const_cast<char*>(buf);
m_tail->iov_len = len;
++m_tail;
}
void append_copy(const char* buf, size_t len)
{
inner_buffer* const ib = &m_inner_buffer;
if(ib->free < len) {
size_t sz = m_chunk_size;
if(sz < len) {
sz = len;
}
chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz));
if(!c) {
throw std::bad_alloc();
}
c->next = ib->head;
ib->head = c;
ib->free = sz;
ib->ptr = reinterpret_cast<char*>(c) + sizeof(chunk);
}
char* m = ib->ptr;
std::memcpy(m, buf, len);
ib->free -= len;
ib->ptr += len;
if(m_tail != m_array && m ==
static_cast<const char*>(
const_cast<const void *>((m_tail - 1)->iov_base)
) + (m_tail - 1)->iov_len) {
(m_tail - 1)->iov_len += len;
return;
} else {
append_ref( m, len);
}
}
const struct iovec* vector() const
{
return m_array;
}
size_t vector_size() const
{
return m_tail - m_array;
}
void migrate(vrefbuffer* to)
{
size_t sz = m_chunk_size;
chunk* empty = static_cast<chunk*>(::malloc(sizeof(chunk) + sz));
if(!empty) {
throw std::bad_alloc();
}
empty->next = MSGPACK_NULLPTR;
const size_t nused = m_tail - m_array;
if(to->m_tail + nused < m_end) {
const size_t tosize = to->m_tail - to->m_array;
const size_t reqsize = nused + tosize;
size_t nnext = (to->m_end - to->m_array) * 2;
while(nnext < reqsize) {
size_t tmp_nnext = nnext * 2;
if (tmp_nnext <= nnext) {
nnext = reqsize;
break;
}
nnext = tmp_nnext;
}
iovec* nvec = static_cast<iovec*>(::realloc(
to->m_array, sizeof(iovec)*nnext));
if(!nvec) {
::free(empty);
throw std::bad_alloc();
}
to->m_array = nvec;
to->m_end = nvec + nnext;
to->m_tail = nvec + tosize;
}
std::memcpy(to->m_tail, m_array, sizeof(iovec)*nused);
to->m_tail += nused;
m_tail = m_array;
inner_buffer* const ib = &m_inner_buffer;
inner_buffer* const toib = &to->m_inner_buffer;
chunk* last = ib->head;
while(last->next) {
last = last->next;
}
last->next = toib->head;
toib->head = ib->head;
if(toib->free < ib->free) {
toib->free = ib->free;
toib->ptr = ib->ptr;
}
ib->head = empty;
ib->free = sz;
ib->ptr = reinterpret_cast<char*>(empty) + sizeof(chunk);
}
void clear()
{
chunk* c = m_inner_buffer.head->next;
chunk* n;
while(c) {
n = c->next;
::free(c);
c = n;
}
inner_buffer* const ib = &m_inner_buffer;
c = ib->head;
c->next = MSGPACK_NULLPTR;
ib->free = m_chunk_size;
ib->ptr = reinterpret_cast<char*>(c) + sizeof(chunk);
m_tail = m_array;
}
#if defined(MSGPACK_USE_CPP03)
private:
vrefbuffer(const vrefbuffer&);
vrefbuffer& operator=(const vrefbuffer&);
#else // defined(MSGPACK_USE_CPP03)
vrefbuffer(const vrefbuffer&) = delete;
vrefbuffer& operator=(const vrefbuffer&) = delete;
#endif // defined(MSGPACK_USE_CPP03)
private:
iovec* m_tail;
iovec* m_end;
iovec* m_array;
size_t m_ref_size;
size_t m_chunk_size;
inner_buffer m_inner_buffer;
};
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // MSGPACK_V1_VREFBUFFER_HPP
|