00001 #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED
00002 #define BOOST_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 <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>
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 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
00096
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)
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)
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)
00150 {
00151 ::operator delete(pv);
00152 }
00153 else if(pv != 0)
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 }
00185
00186 }
00187
00188 #endif // #ifndef BOOST_DETAIL_QUICK_ALLOCATOR_HPP_INCLUDED