|
| 1 | +// |
| 2 | +// MessagePack for C++ memory pool |
| 3 | +// |
| 4 | +// Copyright (C) 2008-2013 FURUHASHI Sadayuki and KONDO Takatoshi |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +#ifndef MSGPACK_CPP11_ZONE_HPP |
| 19 | +#define MSGPACK_CPP11_ZONE_HPP |
| 20 | + |
| 21 | +#include "msgpack/versioning.hpp" |
| 22 | + |
| 23 | +#include <cstdlib> |
| 24 | +#include <memory> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "msgpack/cpp_config.hpp" |
| 28 | + |
| 29 | +#ifndef MSGPACK_ZONE_CHUNK_SIZE |
| 30 | +#define MSGPACK_ZONE_CHUNK_SIZE 8192 |
| 31 | +#endif |
| 32 | + |
| 33 | +#ifndef MSGPACK_ZONE_ALIGN |
| 34 | +#define MSGPACK_ZONE_ALIGN sizeof(void*) |
| 35 | +#endif |
| 36 | + |
| 37 | +namespace msgpack { |
| 38 | + |
| 39 | +/// @cond |
| 40 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 41 | +/// @endcond |
| 42 | + |
| 43 | +class zone { |
| 44 | +private: |
| 45 | + struct finalizer { |
| 46 | + finalizer(void (*func)(void*), void* data):m_func(func), m_data(data) {} |
| 47 | + void operator()() { m_func(m_data); } |
| 48 | + void (*m_func)(void*); |
| 49 | + void* m_data; |
| 50 | + }; |
| 51 | + struct finalizer_array { |
| 52 | + finalizer_array():m_tail(nullptr), m_end(nullptr), m_array(nullptr) {} |
| 53 | + void call() { |
| 54 | + finalizer* fin = m_tail; |
| 55 | + for(; fin != m_array; --fin) (*(fin-1))(); |
| 56 | + } |
| 57 | + ~finalizer_array() { |
| 58 | + call(); |
| 59 | + ::free(m_array); |
| 60 | + } |
| 61 | + void clear() { |
| 62 | + call(); |
| 63 | + m_tail = m_array; |
| 64 | + } |
| 65 | + void push(void (*func)(void* data), void* data) |
| 66 | + { |
| 67 | + finalizer* fin = m_tail; |
| 68 | + |
| 69 | + if(fin == m_end) { |
| 70 | + push_expand(func, data); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + fin->m_func = func; |
| 75 | + fin->m_data = data; |
| 76 | + |
| 77 | + ++m_tail; |
| 78 | + } |
| 79 | + void push_expand(void (*func)(void*), void* data) { |
| 80 | + const size_t nused = m_end - m_array; |
| 81 | + size_t nnext; |
| 82 | + if(nused == 0) { |
| 83 | + nnext = (sizeof(finalizer) < 72/2) ? |
| 84 | + 72 / sizeof(finalizer) : 8; |
| 85 | + } else { |
| 86 | + nnext = nused * 2; |
| 87 | + } |
| 88 | + finalizer* tmp = |
| 89 | + static_cast<finalizer*>(::realloc(m_array, sizeof(finalizer) * nnext)); |
| 90 | + if(!tmp) { |
| 91 | + throw std::bad_alloc(); |
| 92 | + } |
| 93 | + m_array = tmp; |
| 94 | + m_end = tmp + nnext; |
| 95 | + m_tail = tmp + nused; |
| 96 | + new (m_tail) finalizer(func, data); |
| 97 | + |
| 98 | + ++m_tail; |
| 99 | + } |
| 100 | +#if !defined(MSGPACK_USE_CPP03) |
| 101 | + finalizer_array(finalizer_array&& other) noexcept |
| 102 | + :m_tail(other.m_tail), m_end(other.m_end), m_array(other.m_array) |
| 103 | + { |
| 104 | + other.m_tail = nullptr; |
| 105 | + other.m_end = nullptr; |
| 106 | + other.m_array = nullptr; |
| 107 | + } |
| 108 | + finalizer_array& operator=(finalizer_array&& other) noexcept |
| 109 | + { |
| 110 | + this->~finalizer_array(); |
| 111 | + new (this) finalizer_array(std::move(other)); |
| 112 | + return *this; |
| 113 | + } |
| 114 | +#endif |
| 115 | + finalizer* m_tail; |
| 116 | + finalizer* m_end; |
| 117 | + finalizer* m_array; |
| 118 | + |
| 119 | + private: |
| 120 | + finalizer_array(const finalizer_array&); |
| 121 | + finalizer_array& operator=(const finalizer_array&); |
| 122 | + }; |
| 123 | + struct chunk { |
| 124 | + chunk* m_next; |
| 125 | + }; |
| 126 | + struct chunk_list { |
| 127 | + chunk_list(size_t chunk_size) |
| 128 | + { |
| 129 | + chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + chunk_size)); |
| 130 | + if(!c) { |
| 131 | + throw std::bad_alloc(); |
| 132 | + } |
| 133 | + |
| 134 | + m_head = c; |
| 135 | + m_free = chunk_size; |
| 136 | + m_ptr = reinterpret_cast<char*>(c) + sizeof(chunk); |
| 137 | + c->m_next = nullptr; |
| 138 | + } |
| 139 | + ~chunk_list() |
| 140 | + { |
| 141 | + chunk* c = m_head; |
| 142 | + while(c) { |
| 143 | + chunk* n = c->m_next; |
| 144 | + ::free(c); |
| 145 | + c = n; |
| 146 | + } |
| 147 | + } |
| 148 | + void clear(size_t chunk_size) |
| 149 | + { |
| 150 | + chunk* c = m_head; |
| 151 | + while(true) { |
| 152 | + chunk* n = c->m_next; |
| 153 | + if(n) { |
| 154 | + ::free(c); |
| 155 | + c = n; |
| 156 | + } else { |
| 157 | + m_head = c; |
| 158 | + break; |
| 159 | + } |
| 160 | + } |
| 161 | + m_head->m_next = nullptr; |
| 162 | + m_free = chunk_size; |
| 163 | + m_ptr = reinterpret_cast<char*>(m_head) + sizeof(chunk); |
| 164 | + } |
| 165 | +#if !defined(MSGPACK_USE_CPP03) |
| 166 | + chunk_list(chunk_list&& other) noexcept |
| 167 | + :m_free(other.m_free), m_ptr(other.m_ptr), m_head(other.m_head) |
| 168 | + { |
| 169 | + other.m_head = nullptr; |
| 170 | + } |
| 171 | + chunk_list& operator=(chunk_list&& other) noexcept |
| 172 | + { |
| 173 | + this->~chunk_list(); |
| 174 | + new (this) chunk_list(std::move(other)); |
| 175 | + return *this; |
| 176 | + } |
| 177 | +#endif |
| 178 | + size_t m_free; |
| 179 | + char* m_ptr; |
| 180 | + chunk* m_head; |
| 181 | + private: |
| 182 | + chunk_list(const chunk_list&); |
| 183 | + chunk_list& operator=(const chunk_list&); |
| 184 | + }; |
| 185 | + size_t m_chunk_size; |
| 186 | + chunk_list m_chunk_list; |
| 187 | + finalizer_array m_finalizer_array; |
| 188 | + |
| 189 | +public: |
| 190 | + zone(size_t chunk_size = MSGPACK_ZONE_CHUNK_SIZE) noexcept; |
| 191 | + |
| 192 | +public: |
| 193 | + void* allocate_align(size_t size, size_t align = MSGPACK_ZONE_ALIGN); |
| 194 | + void* allocate_no_align(size_t size); |
| 195 | + |
| 196 | + void push_finalizer(void (*func)(void*), void* data); |
| 197 | + |
| 198 | + template <typename T> |
| 199 | + void push_finalizer(msgpack::unique_ptr<T> obj); |
| 200 | + |
| 201 | + void clear(); |
| 202 | + |
| 203 | + void swap(zone& o); |
| 204 | + |
| 205 | + |
| 206 | + static void* operator new(std::size_t size) throw(std::bad_alloc) |
| 207 | + { |
| 208 | + void* p = ::malloc(size); |
| 209 | + if (!p) throw std::bad_alloc(); |
| 210 | + return p; |
| 211 | + } |
| 212 | + static void operator delete(void *p) throw() |
| 213 | + { |
| 214 | + ::free(p); |
| 215 | + } |
| 216 | + static void* operator new(std::size_t /*size*/, void* mem) throw() |
| 217 | + { |
| 218 | + return mem; |
| 219 | + } |
| 220 | + static void operator delete(void * /*p*/, void* /*mem*/) throw() |
| 221 | + { |
| 222 | + } |
| 223 | + |
| 224 | + template <typename T, typename... Args> |
| 225 | + T* allocate(Args... args); |
| 226 | + |
| 227 | + zone(zone&&) = default; |
| 228 | + zone& operator=(zone&&) = default; |
| 229 | + zone(const zone&) = delete; |
| 230 | + zone& operator=(const zone&) = delete; |
| 231 | + |
| 232 | +private: |
| 233 | + void undo_allocate(size_t size); |
| 234 | + |
| 235 | + template <typename T> |
| 236 | + static void object_destruct(void* obj); |
| 237 | + |
| 238 | + template <typename T> |
| 239 | + static void object_delete(void* obj); |
| 240 | + |
| 241 | + void* allocate_expand(size_t size); |
| 242 | +}; |
| 243 | + |
| 244 | +inline zone::zone(size_t chunk_size) noexcept:m_chunk_size(chunk_size), m_chunk_list(m_chunk_size) |
| 245 | +{ |
| 246 | +} |
| 247 | + |
| 248 | +inline void* zone::allocate_align(size_t size, size_t align) |
| 249 | +{ |
| 250 | + char* aligned = |
| 251 | + reinterpret_cast<char*>( |
| 252 | + reinterpret_cast<size_t>( |
| 253 | + (m_chunk_list.m_ptr + (align - 1))) / align * align); |
| 254 | + size_t adjusted_size = size + (aligned - m_chunk_list.m_ptr); |
| 255 | + if(m_chunk_list.m_free >= adjusted_size) { |
| 256 | + m_chunk_list.m_free -= adjusted_size; |
| 257 | + m_chunk_list.m_ptr += adjusted_size; |
| 258 | + return aligned; |
| 259 | + } |
| 260 | + return reinterpret_cast<char*>( |
| 261 | + reinterpret_cast<size_t>( |
| 262 | + allocate_expand(size + (align - 1))) / align * align); |
| 263 | +} |
| 264 | + |
| 265 | +inline void* zone::allocate_no_align(size_t size) |
| 266 | +{ |
| 267 | + if(m_chunk_list.m_free < size) { |
| 268 | + return allocate_expand(size); |
| 269 | + } |
| 270 | + |
| 271 | + char* ptr = m_chunk_list.m_ptr; |
| 272 | + m_chunk_list.m_free -= size; |
| 273 | + m_chunk_list.m_ptr += size; |
| 274 | + |
| 275 | + return ptr; |
| 276 | +} |
| 277 | + |
| 278 | +inline void* zone::allocate_expand(size_t size) |
| 279 | +{ |
| 280 | + chunk_list* const cl = &m_chunk_list; |
| 281 | + |
| 282 | + size_t sz = m_chunk_size; |
| 283 | + |
| 284 | + while(sz < size) { |
| 285 | + size_t tmp_sz = sz * 2; |
| 286 | + if (tmp_sz <= sz) { |
| 287 | + sz = size; |
| 288 | + break; |
| 289 | + } |
| 290 | + sz = tmp_sz; |
| 291 | + } |
| 292 | + |
| 293 | + chunk* c = static_cast<chunk*>(::malloc(sizeof(chunk) + sz)); |
| 294 | + if (!c) throw std::bad_alloc(); |
| 295 | + |
| 296 | + char* ptr = reinterpret_cast<char*>(c) + sizeof(chunk); |
| 297 | + |
| 298 | + c->m_next = cl->m_head; |
| 299 | + cl->m_head = c; |
| 300 | + cl->m_free = sz - size; |
| 301 | + cl->m_ptr = ptr + size; |
| 302 | + |
| 303 | + return ptr; |
| 304 | +} |
| 305 | + |
| 306 | +inline void zone::push_finalizer(void (*func)(void*), void* data) |
| 307 | +{ |
| 308 | + m_finalizer_array.push(func, data); |
| 309 | +} |
| 310 | + |
| 311 | +template <typename T> |
| 312 | +inline void zone::push_finalizer(msgpack::unique_ptr<T> obj) |
| 313 | +{ |
| 314 | + m_finalizer_array.push(&zone::object_delete<T>, obj.release()); |
| 315 | +} |
| 316 | + |
| 317 | +inline void zone::clear() |
| 318 | +{ |
| 319 | + m_finalizer_array.clear(); |
| 320 | + m_chunk_list.clear(m_chunk_size); |
| 321 | +} |
| 322 | + |
| 323 | +inline void zone::swap(zone& o) |
| 324 | +{ |
| 325 | + std::swap(*this, o); |
| 326 | +} |
| 327 | + |
| 328 | +template <typename T> |
| 329 | +void zone::object_delete(void* obj) |
| 330 | +{ |
| 331 | + delete static_cast<T*>(obj); |
| 332 | +} |
| 333 | + |
| 334 | +template <typename T> |
| 335 | +void zone::object_destruct(void* obj) |
| 336 | +{ |
| 337 | + static_cast<T*>(obj)->~T(); |
| 338 | +} |
| 339 | + |
| 340 | +inline void zone::undo_allocate(size_t size) |
| 341 | +{ |
| 342 | + m_chunk_list.m_ptr -= size; |
| 343 | + m_chunk_list.m_free += size; |
| 344 | +} |
| 345 | + |
| 346 | + |
| 347 | +template <typename T, typename... Args> |
| 348 | +T* zone::allocate(Args... args) |
| 349 | +{ |
| 350 | + void* x = allocate_align(sizeof(T)); |
| 351 | + try { |
| 352 | + m_finalizer_array.push(&zone::object_destruct<T>, x); |
| 353 | + } catch (...) { |
| 354 | + undo_allocate(sizeof(T)); |
| 355 | + throw; |
| 356 | + } |
| 357 | + try { |
| 358 | + return new (x) T(args...); |
| 359 | + } catch (...) { |
| 360 | + --m_finalizer_array.m_tail; |
| 361 | + undo_allocate(sizeof(T)); |
| 362 | + throw; |
| 363 | + } |
| 364 | +} |
| 365 | + |
| 366 | +/// @cond |
| 367 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 368 | +/// @endcond |
| 369 | + |
| 370 | +} // namespace msgpack |
| 371 | + |
| 372 | +#endif // MSGPACK_CPP11_ZONE_HPP |
0 commit comments