quick_allocator.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  detail/quick_allocator.hpp
00012 //
00013 //  Copyright (c) 2003 David Abrahams
00014 //  Copyright (c) 2003 Peter Dimov
00015 //
00016 // Distributed under the Boost Software License, Version 1.0. (See
00017 // accompanying file LICENSE_1_0.txt or copy at
00018 // http://www.boost.org/LICENSE_1_0.txt)
00019 //
00020 
00021 #include <boost/config.hpp>
00022 
00023 #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
00024 #include <boost/type_traits/type_with_alignment.hpp>
00025 #include <boost/type_traits/alignment_of.hpp>
00026 
00027 #include <new>              // ::operator new, ::operator delete
00028 #include <cstddef>          // std::size_t
00029 
00030 namespace boost
00031 {
00032 
00033 namespace detail
00034 {
00035 
00036 template<unsigned size, unsigned align_> union freeblock
00037 {
00038     typedef typename boost::type_with_alignment<align_>::type aligner_type;
00039     aligner_type aligner;
00040     char bytes[size];
00041     freeblock * next;
00042 };
00043 
00044 template<unsigned size, unsigned align_> struct allocator_impl
00045 {
00046     typedef freeblock<size, align_> block;
00047 
00048     // It may seem odd to use such small pages.
00049     //
00050     // However, on a typical Windows implementation that uses
00051     // the OS allocator, "normal size" pages interact with the
00052     // "ordinary" operator new, slowing it down dramatically.
00053     //
00054     // 512 byte pages are handled by the small object allocator,
00055     // and don't interfere with ::new.
00056     //
00057     // The other alternative is to use much bigger pages (1M.)
00058     //
00059     // It is surprisingly easy to hit pathological behavior by
00060     // varying the page size. g++ 2.96 on Red Hat Linux 7.2,
00061     // for example, passionately dislikes 496. 512 seems OK.
00062 
00063 #if defined(BOOST_QA_PAGE_SIZE)
00064 
00065     enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
00066 
00067 #else
00068 
00069     enum { items_per_page = 512 / size }; // 1048560 / size
00070 
00071 #endif
00072 
00073 #ifdef BOOST_HAS_THREADS
00074 
00075     static lightweight_mutex & mutex()
00076     {
00077         static lightweight_mutex m;
00078         return m;
00079     }
00080 
00081     static lightweight_mutex * mutex_init;
00082 
00083 #endif
00084 
00085     static block * free;
00086     static block * page;
00087     static unsigned last;
00088 
00089     static inline void * alloc()
00090     {
00091 #ifdef BOOST_HAS_THREADS
00092         lightweight_mutex::scoped_lock lock( mutex() );
00093 #endif
00094         if(block * x = free)
00095         {
00096             free = x->next;
00097             return x;
00098         }
00099         else
00100         {
00101             if(last == items_per_page)
00102             {
00103                 // "Listen to me carefully: there is no memory leak"
00104                 // -- Scott Meyers, Eff C++ 2nd Ed Item 10
00105                 page = ::new block[items_per_page];
00106                 last = 0;
00107             }
00108 
00109             return &page[last++];
00110         }
00111     }
00112 
00113     static inline void * alloc(std::size_t n)
00114     {
00115         if(n != size) // class-specific new called for a derived object
00116         {
00117             return ::operator new(n);
00118         }
00119         else
00120         {
00121 #ifdef BOOST_HAS_THREADS
00122             lightweight_mutex::scoped_lock lock( mutex() );
00123 #endif
00124             if(block * x = free)
00125             {
00126                 free = x->next;
00127                 return x;
00128             }
00129             else
00130             {
00131                 if(last == items_per_page)
00132                 {
00133                     page = ::new block[items_per_page];
00134                     last = 0;
00135                 }
00136 
00137                 return &page[last++];
00138             }
00139         }
00140     }
00141 
00142     static inline void dealloc(void * pv)
00143     {
00144         if(pv != 0) // 18.4.1.1/13
00145         {
00146 #ifdef BOOST_HAS_THREADS
00147             lightweight_mutex::scoped_lock lock( mutex() );
00148 #endif
00149             block * pb = static_cast<block *>(pv);
00150             pb->next = free;
00151             free = pb;
00152         }
00153     }
00154 
00155     static inline void dealloc(void * pv, std::size_t n)
00156     {
00157         if(n != size) // class-specific delete called for a derived object
00158         {
00159             ::operator delete(pv);
00160         }
00161         else if(pv != 0) // 18.4.1.1/13
00162         {
00163 #ifdef BOOST_HAS_THREADS
00164             lightweight_mutex::scoped_lock lock( mutex() );
00165 #endif
00166             block * pb = static_cast<block *>(pv);
00167             pb->next = free;
00168             free = pb;
00169         }
00170     }
00171 };
00172 
00173 #ifdef BOOST_HAS_THREADS
00174 
00175 template<unsigned size, unsigned align_>
00176   lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
00177 
00178 #endif
00179 
00180 template<unsigned size, unsigned align_>
00181   freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
00182 
00183 template<unsigned size, unsigned align_>
00184   freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
00185 
00186 template<unsigned size, unsigned align_>
00187   unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
00188 
00189 template<class T>
00190 struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
00191 {
00192 };
00193 
00194 } // namespace detail
00195 
00196 } // namespace boost
00197 
00198 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29