1 #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/quick_allocator.hpp
13 // Copyright (c) 2003 David Abrahams
14 // Copyright (c) 2003 Peter Dimov
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)
21 #include <boost/config.hpp>
23 #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
24 #include <boost/type_traits/type_with_alignment.hpp>
25 #include <boost/type_traits/alignment_of.hpp>
27 #include <new> // ::operator new, ::operator delete
28 #include <cstddef> // std::size_t
36 template<unsigned size, unsigned align_> union freeblock
38 typedef typename boost::type_with_alignment<align_>::type aligner_type;
44 template<unsigned size, unsigned align_> struct allocator_impl
46 typedef freeblock<size, align_> block;
48 // It may seem odd to use such small pages.
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.
54 // 512 byte pages are handled by the small object allocator,
55 // and don't interfere with ::new.
57 // The other alternative is to use much bigger pages (1M.)
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.
63 #if defined(BOOST_QA_PAGE_SIZE)
65 enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
69 enum { items_per_page = 512 / size }; // 1048560 / size
73 #ifdef BOOST_HAS_THREADS
75 static lightweight_mutex & mutex()
77 static freeblock< sizeof( lightweight_mutex ), boost::alignment_of< lightweight_mutex >::value > fbm;
78 static lightweight_mutex * pm = new( &fbm ) lightweight_mutex;
82 static lightweight_mutex * mutex_init;
90 static inline void * alloc()
92 #ifdef BOOST_HAS_THREADS
93 lightweight_mutex::scoped_lock lock( mutex() );
102 if(last == items_per_page)
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];
110 return &page[last++];
114 static inline void * alloc(std::size_t n)
116 if(n != size) // class-specific new called for a derived object
118 return ::operator new(n);
122 #ifdef BOOST_HAS_THREADS
123 lightweight_mutex::scoped_lock lock( mutex() );
132 if(last == items_per_page)
134 page = ::new block[items_per_page];
138 return &page[last++];
143 static inline void dealloc(void * pv)
145 if(pv != 0) // 18.4.1.1/13
147 #ifdef BOOST_HAS_THREADS
148 lightweight_mutex::scoped_lock lock( mutex() );
150 block * pb = static_cast<block *>(pv);
156 static inline void dealloc(void * pv, std::size_t n)
158 if(n != size) // class-specific delete called for a derived object
160 ::operator delete(pv);
162 else if(pv != 0) // 18.4.1.1/13
164 #ifdef BOOST_HAS_THREADS
165 lightweight_mutex::scoped_lock lock( mutex() );
167 block * pb = static_cast<block *>(pv);
174 #ifdef BOOST_HAS_THREADS
176 template<unsigned size, unsigned align_>
177 lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
181 template<unsigned size, unsigned align_>
182 freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
184 template<unsigned size, unsigned align_>
185 freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
187 template<unsigned size, unsigned align_>
188 unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
191 struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
195 } // namespace detail
199 #endif // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED