quick_allocator.hpp

Go to the documentation of this file.
00001 #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
00002 #define BOOST_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 <sysc/packages/boost/config.hpp>
00022 
00023 #include <sysc/packages/boost/detail/lightweight_mutex.hpp>
00024 #include <sysc/packages/boost/type_traits/type_with_alignment.hpp>
00025 #include <sysc/packages/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     static lightweight_mutex mutex;
00075 #endif
00076 
00077     static block * free;
00078     static block * page;
00079     static unsigned last;
00080 
00081     static inline void * alloc()
00082     {
00083 #ifdef BOOST_HAS_THREADS
00084         lightweight_mutex::scoped_lock lock(mutex);
00085 #endif
00086         if(block * x = free)
00087         {
00088             free = x->next;
00089             return x;
00090         }
00091         else
00092         {
00093             if(last == items_per_page)
00094             {
00095                 // "Listen to me carefully: there is no memory leak"
00096                 // -- Scott Meyers, Eff C++ 2nd Ed Item 10
00097                 page = ::new block[items_per_page];
00098                 last = 0;
00099             }
00100 
00101             return &page[last++];
00102         }
00103     }
00104 
00105     static inline void * alloc(std::size_t n)
00106     {
00107         if(n != size) // class-specific new called for a derived object
00108         {
00109             return ::operator new(n);
00110         }
00111         else
00112         {
00113 #ifdef BOOST_HAS_THREADS
00114             lightweight_mutex::scoped_lock lock(mutex);
00115 #endif
00116             if(block * x = free)
00117             {
00118                 free = x->next;
00119                 return x;
00120             }
00121             else
00122             {
00123                 if(last == items_per_page)
00124                 {
00125                     page = ::new block[items_per_page];
00126                     last = 0;
00127                 }
00128 
00129                 return &page[last++];
00130             }
00131         }
00132     }
00133 
00134     static inline void dealloc(void * pv)
00135     {
00136         if(pv != 0) // 18.4.1.1/13
00137         {
00138 #ifdef BOOST_HAS_THREADS
00139             lightweight_mutex::scoped_lock lock(mutex);
00140 #endif
00141             block * pb = static_cast<block *>(pv);
00142             pb->next = free;
00143             free = pb;
00144         }
00145     }
00146 
00147     static inline void dealloc(void * pv, std::size_t n)
00148     {
00149         if(n != size) // class-specific delete called for a derived object
00150         {
00151             ::operator delete(pv);
00152         }
00153         else if(pv != 0) // 18.4.1.1/13
00154         {
00155 #ifdef BOOST_HAS_THREADS
00156             lightweight_mutex::scoped_lock lock(mutex);
00157 #endif
00158             block * pb = static_cast<block *>(pv);
00159             pb->next = free;
00160             free = pb;
00161         }
00162     }
00163 };
00164 
00165 #ifdef BOOST_HAS_THREADS
00166 template<unsigned size, unsigned align_>
00167   lightweight_mutex allocator_impl<size, align_>::mutex;
00168 #endif
00169 
00170 template<unsigned size, unsigned align_>
00171   freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
00172 
00173 template<unsigned size, unsigned align_>
00174   freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
00175 
00176 template<unsigned size, unsigned align_>
00177   unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
00178 
00179 template<class T>
00180 struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
00181 {
00182 };
00183 
00184 } // namespace detail
00185 
00186 } // namespace boost
00187 
00188 #endif  // #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5