Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:42

0001 #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
0003 
0004 // MS compatible compilers support #pragma once
0005 
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009 
0010 //
0011 //  detail/quick_allocator.hpp
0012 //
0013 //  Copyright (c) 2003 David Abrahams
0014 //  Copyright (c) 2003 Peter Dimov
0015 //
0016 // Distributed under the Boost Software License, Version 1.0. (See
0017 // accompanying file LICENSE_1_0.txt or copy at
0018 // http://www.boost.org/LICENSE_1_0.txt)
0019 //
0020 
0021 #include <boost/config.hpp>
0022 
0023 #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
0024 #include <boost/type_traits/type_with_alignment.hpp>
0025 #include <boost/type_traits/alignment_of.hpp>
0026 
0027 #include <new>              // ::operator new, ::operator delete
0028 #include <cstddef>          // std::size_t
0029 
0030 namespace boost
0031 {
0032 
0033 namespace detail
0034 {
0035 
0036 template<unsigned size, unsigned align_> union freeblock
0037 {
0038     typedef typename boost::type_with_alignment<align_>::type aligner_type;
0039     aligner_type aligner;
0040     char bytes[size];
0041     freeblock * next;
0042 };
0043 
0044 template<unsigned size, unsigned align_> struct allocator_impl
0045 {
0046     typedef freeblock<size, align_> block;
0047 
0048     // It may seem odd to use such small pages.
0049     //
0050     // However, on a typical Windows implementation that uses
0051     // the OS allocator, "normal size" pages interact with the
0052     // "ordinary" operator new, slowing it down dramatically.
0053     //
0054     // 512 byte pages are handled by the small object allocator,
0055     // and don't interfere with ::new.
0056     //
0057     // The other alternative is to use much bigger pages (1M.)
0058     //
0059     // It is surprisingly easy to hit pathological behavior by
0060     // varying the page size. g++ 2.96 on Red Hat Linux 7.2,
0061     // for example, passionately dislikes 496. 512 seems OK.
0062 
0063 #if defined(BOOST_QA_PAGE_SIZE)
0064 
0065     enum { items_per_page = BOOST_QA_PAGE_SIZE / size };
0066 
0067 #else
0068 
0069     enum { items_per_page = 512 / size }; // 1048560 / size
0070 
0071 #endif
0072 
0073 #ifdef BOOST_HAS_THREADS
0074 
0075     static lightweight_mutex & mutex()
0076     {
0077         static freeblock< sizeof( lightweight_mutex ), boost::alignment_of< lightweight_mutex >::value > fbm;
0078         static lightweight_mutex * pm = new( &fbm ) lightweight_mutex;
0079         return *pm;
0080     }
0081 
0082     static lightweight_mutex * mutex_init;
0083 
0084 #endif
0085 
0086     static block * free;
0087     static block * page;
0088     static unsigned last;
0089 
0090     static inline void * alloc()
0091     {
0092 #ifdef BOOST_HAS_THREADS
0093         lightweight_mutex::scoped_lock lock( mutex() );
0094 #endif
0095         if(block * x = free)
0096         {
0097             free = x->next;
0098             return x;
0099         }
0100         else
0101         {
0102             if(last == items_per_page)
0103             {
0104                 // "Listen to me carefully: there is no memory leak"
0105                 // -- Scott Meyers, Eff C++ 2nd Ed Item 10
0106                 page = ::new block[items_per_page];
0107                 last = 0;
0108             }
0109 
0110             return &page[last++];
0111         }
0112     }
0113 
0114     static inline void * alloc(std::size_t n)
0115     {
0116         if(n != size) // class-specific new called for a derived object
0117         {
0118             return ::operator new(n);
0119         }
0120         else
0121         {
0122 #ifdef BOOST_HAS_THREADS
0123             lightweight_mutex::scoped_lock lock( mutex() );
0124 #endif
0125             if(block * x = free)
0126             {
0127                 free = x->next;
0128                 return x;
0129             }
0130             else
0131             {
0132                 if(last == items_per_page)
0133                 {
0134                     page = ::new block[items_per_page];
0135                     last = 0;
0136                 }
0137 
0138                 return &page[last++];
0139             }
0140         }
0141     }
0142 
0143     static inline void dealloc(void * pv)
0144     {
0145         if(pv != 0) // 18.4.1.1/13
0146         {
0147 #ifdef BOOST_HAS_THREADS
0148             lightweight_mutex::scoped_lock lock( mutex() );
0149 #endif
0150             block * pb = static_cast<block *>(pv);
0151             pb->next = free;
0152             free = pb;
0153         }
0154     }
0155 
0156     static inline void dealloc(void * pv, std::size_t n)
0157     {
0158         if(n != size) // class-specific delete called for a derived object
0159         {
0160             ::operator delete(pv);
0161         }
0162         else if(pv != 0) // 18.4.1.1/13
0163         {
0164 #ifdef BOOST_HAS_THREADS
0165             lightweight_mutex::scoped_lock lock( mutex() );
0166 #endif
0167             block * pb = static_cast<block *>(pv);
0168             pb->next = free;
0169             free = pb;
0170         }
0171     }
0172 };
0173 
0174 #ifdef BOOST_HAS_THREADS
0175 
0176 template<unsigned size, unsigned align_>
0177   lightweight_mutex * allocator_impl<size, align_>::mutex_init = &allocator_impl<size, align_>::mutex();
0178 
0179 #endif
0180 
0181 template<unsigned size, unsigned align_>
0182   freeblock<size, align_> * allocator_impl<size, align_>::free = 0;
0183 
0184 template<unsigned size, unsigned align_>
0185   freeblock<size, align_> * allocator_impl<size, align_>::page = 0;
0186 
0187 template<unsigned size, unsigned align_>
0188   unsigned allocator_impl<size, align_>::last = allocator_impl<size, align_>::items_per_page;
0189 
0190 template<class T>
0191 struct quick_allocator: public allocator_impl< sizeof(T), boost::alignment_of<T>::value >
0192 {
0193 };
0194 
0195 } // namespace detail
0196 
0197 } // namespace boost
0198 
0199 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED