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
00005
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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>
00028 #include <cstddef>
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
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
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 };
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
00104
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)
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)
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)
00158 {
00159 ::operator delete(pv);
00160 }
00161 else if(pv != 0)
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 }
00195
00196 }
00197
00198 #endif // #ifndef BOOST_SMART_PTR_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED