blob: dff06686bea04d00b8b968456eb01f7a1564fc60 [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
11// detail/quick_allocator.hpp
12//
13// Copyright (c) 2003 David Abrahams
14// Copyright (c) 2003 Peter Dimov
15//
16// Distributed under the Boost Software License, Version 1.0. (See
17// accompanying file LICENSE_1_0.txt or copy at
18// http://www.boost.org/LICENSE_1_0.txt)
19//
20
Jeff Thompson2277ce52013-08-01 17:34:11 -070021#include <ndnboost/config.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070022
Jeff Thompson2277ce52013-08-01 17:34:11 -070023#include <ndnboost/smart_ptr/detail/lightweight_mutex.hpp>
24#include <ndnboost/type_traits/type_with_alignment.hpp>
25#include <ndnboost/type_traits/alignment_of.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070026
27#include <new> // ::operator new, ::operator delete
28#include <cstddef> // std::size_t
29
30namespace ndnboost
31{
32
33namespace detail
34{
35
36template<unsigned size, unsigned align_> union freeblock
37{
38 typedef typename ndnboost::type_with_alignment<align_>::type aligner_type;
39 aligner_type aligner;
40 char bytes[size];
41 freeblock * next;
42};
43
44template<unsigned size, unsigned align_> struct allocator_impl
45{
46 typedef freeblock<size, align_> block;
47
48 // It may seem odd to use such small pages.
49 //
50 // However, on a typical Windows implementation that uses
51 // the OS allocator, "normal size" pages interact with the
52 // "ordinary" operator new, slowing it down dramatically.
53 //
54 // 512 byte pages are handled by the small object allocator,
55 // and don't interfere with ::new.
56 //
57 // The other alternative is to use much bigger pages (1M.)
58 //
59 // It is surprisingly easy to hit pathological behavior by
60 // varying the page size. g++ 2.96 on Red Hat Linux 7.2,
61 // for example, passionately dislikes 496. 512 seems OK.
62
Jeff Thompson3d613fd2013-10-15 15:39:04 -070063#if defined(NDNBOOST_QA_PAGE_SIZE)
Jeff Thompsonf7d49942013-08-01 16:47:40 -070064
Jeff Thompson3d613fd2013-10-15 15:39:04 -070065 enum { items_per_page = NDNBOOST_QA_PAGE_SIZE / size };
Jeff Thompsonf7d49942013-08-01 16:47:40 -070066
67#else
68
69 enum { items_per_page = 512 / size }; // 1048560 / size
70
71#endif
72
Jeff Thompson3d613fd2013-10-15 15:39:04 -070073#ifdef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -070074
75 static lightweight_mutex & mutex()
76 {
77 static freeblock< sizeof( lightweight_mutex ), ndnboost::alignment_of< lightweight_mutex >::value > fbm;
78 static lightweight_mutex * pm = new( &fbm ) lightweight_mutex;
79 return *pm;
80 }
81
82 static lightweight_mutex * mutex_init;
83
84#endif
85
86 static block * free;
87 static block * page;
88 static unsigned last;
89
90 static inline void * alloc()
91 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -070092#ifdef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -070093 lightweight_mutex::scoped_lock lock( mutex() );
94#endif
95 if(block * x = free)
96 {
97 free = x->next;
98 return x;
99 }
100 else
101 {
102 if(last == items_per_page)
103 {
104 // "Listen to me carefully: there is no memory leak"
105 // -- Scott Meyers, Eff C++ 2nd Ed Item 10
106 page = ::new block[items_per_page];
107 last = 0;
108 }
109
110 return &page[last++];
111 }
112 }
113
114 static inline void * alloc(std::size_t n)
115 {
116 if(n != size) // class-specific new called for a derived object
117 {
118 return ::operator new(n);
119 }
120 else
121 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700122#ifdef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700123 lightweight_mutex::scoped_lock lock( mutex() );
124#endif
125 if(block * x = free)
126 {
127 free = x->next;
128 return x;
129 }
130 else
131 {
132 if(last == items_per_page)
133 {
134 page = ::new block[items_per_page];
135 last = 0;
136 }
137
138 return &page[last++];
139 }
140 }
141 }
142
143 static inline void dealloc(void * pv)
144 {
145 if(pv != 0) // 18.4.1.1/13
146 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700147#ifdef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700148 lightweight_mutex::scoped_lock lock( mutex() );
149#endif
150 block * pb = static_cast<block *>(pv);
151 pb->next = free;
152 free = pb;
153 }
154 }
155
156 static inline void dealloc(void * pv, std::size_t n)
157 {
158 if(n != size) // class-specific delete called for a derived object
159 {
160 ::operator delete(pv);
161 }
162 else if(pv != 0) // 18.4.1.1/13
163 {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700164#ifdef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700165 lightweight_mutex::scoped_lock lock( mutex() );
166#endif
167 block * pb = static_cast<block *>(pv);
168 pb->next = free;
169 free = pb;
170 }
171 }
172};
173
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700174#ifdef NDNBOOST_HAS_THREADS
Jeff Thompsonf7d49942013-08-01 16:47:40 -0700175
176template<unsigned size, unsigned align_>
177 lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
178
179#endif
180
181template<unsigned size, unsigned align_>
182 freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
183
184template<unsigned size, unsigned align_>
185 freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
186
187template<unsigned size, unsigned align_>
188 unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
189
190template<class T>
191struct quick_allocator: public allocator_impl< sizeof(T), ndnboost::alignment_of<T>::value >
192{
193};
194
195} // namespace detail
196
197} // namespace ndnboost
198
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700199#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED