Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:50:35

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