1 #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
\r
2 #define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
\r
4 // MS compatible compilers support #pragma once
\r
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
\r
11 // detail/quick_allocator.hpp
\r
13 // Copyright (c) 2003 David Abrahams
\r
14 // Copyright (c) 2003 Peter Dimov
\r
16 // Distributed under the Boost Software License, Version 1.0. (See
\r
17 // accompanying file LICENSE_1_0.txt or copy at
\r
18 // http://www.boost.org/LICENSE_1_0.txt)
\r
21 #include <boost/config.hpp>
\r
23 #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
\r
24 #include <boost/type_traits/type_with_alignment.hpp>
\r
25 #include <boost/type_traits/alignment_of.hpp>
\r
27 #include <new> // ::operator new, ::operator delete
\r
28 #include <cstddef> // std::size_t
\r
36 template<unsigned size, unsigned align_> union freeblock
\r
38 typedef typename boost::type_with_alignment<align_>::type aligner_type;
\r
39 aligner_type aligner;
\r
44 template<unsigned size, unsigned align_> struct allocator_impl
\r
46 typedef freeblock<size, align_> block;
\r
48 // It may seem odd to use such small pages.
\r
50 // However, on a typical Windows implementation that uses
\r
51 // the OS allocator, "normal size" pages interact with the
\r
52 // "ordinary" operator new, slowing it down dramatically.
\r
54 // 512 byte pages are handled by the small object allocator,
\r
55 // and don't interfere with ::new.
\r
57 // The other alternative is to use much bigger pages (1M.)
\r
59 // It is surprisingly easy to hit pathological behavior by
\r
60 // varying the page size. g++ 2.96 on Red Hat Linux 7.2,
\r
61 // for example, passionately dislikes 496. 512 seems OK.
\r
63 #if defined(BOOST_QA_PAGE_SIZE)
\r
65 enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
\r
69 enum { items_per_page = 512 / size }; // 1048560 / size
\r
73 #ifdef BOOST_HAS_THREADS
\r
75 static lightweight_mutex & mutex()
\r
77 static freeblock< sizeof( lightweight_mutex ), boost::alignment_of< lightweight_mutex >::value > fbm;
\r
78 static lightweight_mutex * pm = new( &fbm ) lightweight_mutex;
\r
82 static lightweight_mutex * mutex_init;
\r
86 static block * free;
\r
87 static block * page;
\r
88 static unsigned last;
\r
90 static inline void * alloc()
\r
92 #ifdef BOOST_HAS_THREADS
\r
93 lightweight_mutex::scoped_lock lock( mutex() );
\r
95 if(block * x = free)
\r
102 if(last == items_per_page)
\r
104 // "Listen to me carefully: there is no memory leak"
\r
105 // -- Scott Meyers, Eff C++ 2nd Ed Item 10
\r
106 page = ::new block[items_per_page];
\r
110 return &page[last++];
\r
114 static inline void * alloc(std::size_t n)
\r
116 if(n != size) // class-specific new called for a derived object
\r
118 return ::operator new(n);
\r
122 #ifdef BOOST_HAS_THREADS
\r
123 lightweight_mutex::scoped_lock lock( mutex() );
\r
125 if(block * x = free)
\r
132 if(last == items_per_page)
\r
134 page = ::new block[items_per_page];
\r
138 return &page[last++];
\r
143 static inline void dealloc(void * pv)
\r
145 if(pv != 0) // 18.4.1.1/13
\r
147 #ifdef BOOST_HAS_THREADS
\r
148 lightweight_mutex::scoped_lock lock( mutex() );
\r
150 block * pb = static_cast<block *>(pv);
\r
156 static inline void dealloc(void * pv, std::size_t n)
\r
158 if(n != size) // class-specific delete called for a derived object
\r
160 ::operator delete(pv);
\r
162 else if(pv != 0) // 18.4.1.1/13
\r
164 #ifdef BOOST_HAS_THREADS
\r
165 lightweight_mutex::scoped_lock lock( mutex() );
\r
167 block * pb = static_cast<block *>(pv);
\r
174 #ifdef BOOST_HAS_THREADS
\r
176 template<unsigned size, unsigned align_>
\r
177 lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
\r
181 template<unsigned size, unsigned align_>
\r
182 freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
\r
184 template<unsigned size, unsigned align_>
\r
185 freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
\r
187 template<unsigned size, unsigned align_>
\r
188 unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
\r
191 struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
\r
195 } // namespace detail
\r
197 } // namespace boost
\r
199 #endif // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
\r