10 #ifndef MSGPACK_V1_CPP03_ZONE_HPP
11 #define MSGPACK_V1_CPP03_ZONE_HPP
22 #include <boost/assert.hpp>
33 finalizer(
void (*func)(
void*),
void* data):m_func(func), m_data(data) {}
34 void operator()() { m_func(m_data); }
35 void (*m_func)(
void*);
38 struct finalizer_array {
41 finalizer* fin = m_tail;
42 for(; fin != m_array; --fin) (*(fin-1))();
52 void push(
void (*func)(
void* data),
void* data)
54 finalizer* fin = m_tail;
57 push_expand(func, data);
66 void push_expand(
void (*func)(
void*),
void* data) {
67 const size_t nused =
static_cast<size_t>(m_end - m_array);
70 nnext = (
sizeof(finalizer) < 72/2) ?
71 72 /
sizeof(finalizer) : 8;
76 static_cast<finalizer*
>(::realloc(m_array,
sizeof(finalizer) * nnext));
78 throw std::bad_alloc();
83 new (m_tail) finalizer(func, data);
95 chunk_list(
size_t chunk_size)
97 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + chunk_size));
99 throw std::bad_alloc();
104 m_ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
111 chunk* n = c->m_next;
116 void clear(
size_t chunk_size)
120 chunk* n = c->m_next;
131 m_ptr =
reinterpret_cast<char*
>(m_head) +
sizeof(chunk);
138 chunk_list m_chunk_list;
139 finalizer_array m_finalizer_array;
150 template <
typename T>
156 static void*
operator new(std::size_t
size)
158 void* p = ::malloc(
size);
159 if (!p)
throw std::bad_alloc();
162 static void operator delete(
void *p)
166 static void*
operator new(std::size_t
size,
void* place)
168 return ::operator
new(
size, place);
170 static void operator delete(
void* p,
void* place)
172 ::operator
delete(p, place);
176 template <
typename T>
179 template <
typename T,
typename A1>
182 template <
typename T,
typename A1,
typename A2>
185 template <
typename T,
typename A1,
typename A2,
typename A3>
188 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4>
189 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4);
191 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5>
192 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5);
194 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6>
195 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6);
197 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7>
198 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7);
200 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8>
201 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8);
203 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9>
204 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9);
206 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10>
207 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10);
209 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11>
210 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11);
212 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12>
213 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12);
215 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12,
typename A13>
216 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13);
218 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12,
typename A13,
typename A14>
219 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14);
221 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12,
typename A13,
typename A14,
typename A15>
222 T*
allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14, A15 a15);
227 void undo_allocate(
size_t size);
229 template <
typename T>
230 static void object_destruct(
void* obj);
232 template <
typename T>
233 static void object_delete(
void* obj);
235 static char* get_aligned(
char* ptr,
size_t align);
237 char* allocate_expand(
size_t size);
243 inline zone::zone(
size_t chunk_size) :m_chunk_size(chunk_size), m_chunk_list(m_chunk_size)
247 inline char* zone::get_aligned(
char* ptr,
size_t align)
249 BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0);
251 reinterpret_cast<char*
>(
252 reinterpret_cast<uintptr_t
>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
258 char* aligned = get_aligned(m_chunk_list.m_ptr, align);
259 size_t adjusted_size =
size +
static_cast<size_t>(aligned - m_chunk_list.m_ptr);
260 if (m_chunk_list.m_free < adjusted_size) {
261 size_t enough_size =
size + align - 1;
262 char* ptr = allocate_expand(enough_size);
263 aligned = get_aligned(ptr, align);
264 adjusted_size =
size +
static_cast<size_t>(aligned - m_chunk_list.m_ptr);
266 m_chunk_list.m_free -= adjusted_size;
267 m_chunk_list.m_ptr += adjusted_size;
273 char* ptr = m_chunk_list.m_ptr;
274 if(m_chunk_list.m_free <
size) {
275 ptr = allocate_expand(
size);
277 m_chunk_list.m_free -=
size;
278 m_chunk_list.m_ptr +=
size;
283 inline char* zone::allocate_expand(
size_t size)
285 chunk_list*
const cl = &m_chunk_list;
287 size_t sz = m_chunk_size;
290 size_t tmp_sz = sz * 2;
298 chunk* c =
static_cast<chunk*
>(::malloc(
sizeof(chunk) + sz));
299 if (!c)
throw std::bad_alloc();
301 char* ptr =
reinterpret_cast<char*
>(c) +
sizeof(chunk);
303 c->m_next = cl->m_head;
313 m_finalizer_array.push(func, data);
316 template <
typename T>
319 m_finalizer_array.push(&zone::object_delete<T>, obj.release());
324 m_finalizer_array.clear();
325 m_chunk_list.clear(m_chunk_size);
331 swap(m_chunk_size, o.m_chunk_size);
332 swap(m_chunk_list, o.m_chunk_list);
333 swap(m_finalizer_array, o.m_finalizer_array);
336 template <
typename T>
337 void zone::object_destruct(
void* obj)
339 static_cast<T*
>(obj)->~T();
342 template <
typename T>
343 void zone::object_delete(
void* obj)
345 delete static_cast<T*
>(obj);
348 inline void zone::undo_allocate(
size_t size)
350 m_chunk_list.m_ptr -=
size;
351 m_chunk_list.m_free +=
size;
357 return (
size + align - 1) / align * align;
362 template <
typename T>
367 m_finalizer_array.push(&zone::object_destruct<T>, x);
369 undo_allocate(
sizeof(T));
375 --m_finalizer_array.m_tail;
376 undo_allocate(
sizeof(T));
381 template <
typename T,
typename A1>
386 m_finalizer_array.push(&zone::object_destruct<T>, x);
388 undo_allocate(
sizeof(T));
392 return new (x) T(a1);
394 --m_finalizer_array.m_tail;
395 undo_allocate(
sizeof(T));
400 template <
typename T,
typename A1,
typename A2>
405 m_finalizer_array.push(&zone::object_destruct<T>, x);
407 undo_allocate(
sizeof(T));
411 return new (x) T(a1, a2);
413 --m_finalizer_array.m_tail;
414 undo_allocate(
sizeof(T));
419 template <
typename T,
typename A1,
typename A2,
typename A3>
424 m_finalizer_array.push(&zone::object_destruct<T>, x);
426 undo_allocate(
sizeof(T));
430 return new (x) T(a1, a2, a3);
432 --m_finalizer_array.m_tail;
433 undo_allocate(
sizeof(T));
438 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4>
443 m_finalizer_array.push(&zone::object_destruct<T>, x);
445 undo_allocate(
sizeof(T));
449 return new (x) T(a1, a2, a3, a4);
451 --m_finalizer_array.m_tail;
452 undo_allocate(
sizeof(T));
457 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5>
462 m_finalizer_array.push(&zone::object_destruct<T>, x);
464 undo_allocate(
sizeof(T));
468 return new (x) T(a1, a2, a3, a4, a5);
470 --m_finalizer_array.m_tail;
471 undo_allocate(
sizeof(T));
476 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6>
481 m_finalizer_array.push(&zone::object_destruct<T>, x);
483 undo_allocate(
sizeof(T));
487 return new (x) T(a1, a2, a3, a4, a5, a6);
489 --m_finalizer_array.m_tail;
490 undo_allocate(
sizeof(T));
495 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7>
496 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7)
500 m_finalizer_array.push(&zone::object_destruct<T>, x);
502 undo_allocate(
sizeof(T));
506 return new (x) T(a1, a2, a3, a4, a5, a6, a7);
508 --m_finalizer_array.m_tail;
509 undo_allocate(
sizeof(T));
514 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8>
515 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8)
519 m_finalizer_array.push(&zone::object_destruct<T>, x);
521 undo_allocate(
sizeof(T));
525 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8);
527 --m_finalizer_array.m_tail;
528 undo_allocate(
sizeof(T));
533 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9>
534 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9)
538 m_finalizer_array.push(&zone::object_destruct<T>, x);
540 undo_allocate(
sizeof(T));
544 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9);
546 --m_finalizer_array.m_tail;
547 undo_allocate(
sizeof(T));
552 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10>
553 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10)
557 m_finalizer_array.push(&zone::object_destruct<T>, x);
559 undo_allocate(
sizeof(T));
563 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
565 --m_finalizer_array.m_tail;
566 undo_allocate(
sizeof(T));
571 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11>
572 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11)
576 m_finalizer_array.push(&zone::object_destruct<T>, x);
578 undo_allocate(
sizeof(T));
582 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
584 --m_finalizer_array.m_tail;
585 undo_allocate(
sizeof(T));
590 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12>
591 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12)
595 m_finalizer_array.push(&zone::object_destruct<T>, x);
597 undo_allocate(
sizeof(T));
601 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
603 --m_finalizer_array.m_tail;
604 undo_allocate(
sizeof(T));
609 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12,
typename A13>
610 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13)
614 m_finalizer_array.push(&zone::object_destruct<T>, x);
616 undo_allocate(
sizeof(T));
620 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13);
622 --m_finalizer_array.m_tail;
623 undo_allocate(
sizeof(T));
628 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12,
typename A13,
typename A14>
629 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14)
633 m_finalizer_array.push(&zone::object_destruct<T>, x);
635 undo_allocate(
sizeof(T));
639 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
641 --m_finalizer_array.m_tail;
642 undo_allocate(
sizeof(T));
647 template <
typename T,
typename A1,
typename A2,
typename A3,
typename A4,
typename A5,
typename A6,
typename A7,
typename A8,
typename A9,
typename A10,
typename A11,
typename A12,
typename A13,
typename A14,
typename A15>
648 T*
zone::allocate(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6, A7 a7, A8 a8, A9 a9, A10 a10, A11 a11, A12 a12, A13 a13, A14 a14, A15 a15)
652 m_finalizer_array.push(&zone::object_destruct<T>, x);
654 undo_allocate(
sizeof(T));
658 return new (x) T(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15);
660 --m_finalizer_array.m_tail;
661 undo_allocate(
sizeof(T));
Definition: cpp03_zone.hpp:31
void swap(zone &o)
Definition: cpp03_zone.hpp:328
T * allocate(Args... args)
Definition: cpp11_zone.hpp:340
void * allocate_no_align(size_t size)
Definition: cpp03_zone.hpp:271
void clear()
Definition: cpp03_zone.hpp:322
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:256
void push_finalizer(void(*func)(void *), void *data)
Definition: cpp03_zone.hpp:311
zone(size_t chunk_size=MSGPACK_ZONE_CHUNK_SIZE)
Definition: cpp03_zone.hpp:243
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
Definition: adaptor_base.hpp:15
std::size_t aligned_size(std::size_t size, std::size_t align)
Definition: cpp03_zone.hpp:354
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
#define MSGPACK_ZONE_ALIGN
Definition: cpp03_zone_decl.hpp:24
#define MSGPACK_ZONE_CHUNK_SIZE
Definition: cpp03_zone_decl.hpp:20
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66