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