Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef _POOL_ALLOCATOR_H
00044 #define _POOL_ALLOCATOR_H 1
00045
00046 #include <bits/c++config.h>
00047 #include <cstdlib>
00048 #include <new>
00049 #include <bits/functexcept.h>
00050 #include <ext/atomicity.h>
00051 #include <ext/concurrence.h>
00052 #include <bits/move.h>
00053
00054 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00055 {
00056 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00057
00058 using std::size_t;
00059 using std::ptrdiff_t;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 class __pool_alloc_base
00077 {
00078 protected:
00079
00080 enum { _S_align = 8 };
00081 enum { _S_max_bytes = 128 };
00082 enum { _S_free_list_size = (size_t)_S_max_bytes / (size_t)_S_align };
00083
00084 union _Obj
00085 {
00086 union _Obj* _M_free_list_link;
00087 char _M_client_data[1];
00088 };
00089
00090 static _Obj* volatile _S_free_list[_S_free_list_size];
00091
00092
00093 static char* _S_start_free;
00094 static char* _S_end_free;
00095 static size_t _S_heap_size;
00096
00097 size_t
00098 _M_round_up(size_t __bytes)
00099 { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
00100
00101 _GLIBCXX_CONST _Obj* volatile*
00102 _M_get_free_list(size_t __bytes) throw ();
00103
00104 __mutex&
00105 _M_get_mutex() throw ();
00106
00107
00108
00109 void*
00110 _M_refill(size_t __n);
00111
00112
00113
00114 char*
00115 _M_allocate_chunk(size_t __n, int& __nobjs);
00116 };
00117
00118
00119
00120
00121
00122
00123 template<typename _Tp>
00124 class __pool_alloc : private __pool_alloc_base
00125 {
00126 private:
00127 static _Atomic_word _S_force_new;
00128
00129 public:
00130 typedef size_t size_type;
00131 typedef ptrdiff_t difference_type;
00132 typedef _Tp* pointer;
00133 typedef const _Tp* const_pointer;
00134 typedef _Tp& reference;
00135 typedef const _Tp& const_reference;
00136 typedef _Tp value_type;
00137
00138 template<typename _Tp1>
00139 struct rebind
00140 { typedef __pool_alloc<_Tp1> other; };
00141
00142 __pool_alloc() throw() { }
00143
00144 __pool_alloc(const __pool_alloc&) throw() { }
00145
00146 template<typename _Tp1>
00147 __pool_alloc(const __pool_alloc<_Tp1>&) throw() { }
00148
00149 ~__pool_alloc() throw() { }
00150
00151 pointer
00152 address(reference __x) const { return std::__addressof(__x); }
00153
00154 const_pointer
00155 address(const_reference __x) const { return std::__addressof(__x); }
00156
00157 size_type
00158 max_size() const throw()
00159 { return size_t(-1) / sizeof(_Tp); }
00160
00161
00162
00163 void
00164 construct(pointer __p, const _Tp& __val)
00165 { ::new((void *)__p) _Tp(__val); }
00166
00167 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00168 template<typename... _Args>
00169 void
00170 construct(pointer __p, _Args&&... __args)
00171 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
00172 #endif
00173
00174 void
00175 destroy(pointer __p) { __p->~_Tp(); }
00176
00177 pointer
00178 allocate(size_type __n, const void* = 0);
00179
00180 void
00181 deallocate(pointer __p, size_type __n);
00182 };
00183
00184 template<typename _Tp>
00185 inline bool
00186 operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
00187 { return true; }
00188
00189 template<typename _Tp>
00190 inline bool
00191 operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
00192 { return false; }
00193
00194 template<typename _Tp>
00195 _Atomic_word
00196 __pool_alloc<_Tp>::_S_force_new;
00197
00198 template<typename _Tp>
00199 _Tp*
00200 __pool_alloc<_Tp>::allocate(size_type __n, const void*)
00201 {
00202 pointer __ret = 0;
00203 if (__builtin_expect(__n != 0, true))
00204 {
00205 if (__n > this->max_size())
00206 std::__throw_bad_alloc();
00207
00208
00209
00210
00211 if (_S_force_new == 0)
00212 {
00213 if (std::getenv("GLIBCXX_FORCE_NEW"))
00214 __atomic_add_dispatch(&_S_force_new, 1);
00215 else
00216 __atomic_add_dispatch(&_S_force_new, -1);
00217 }
00218
00219 const size_t __bytes = __n * sizeof(_Tp);
00220 if (__bytes > size_t(_S_max_bytes) || _S_force_new > 0)
00221 __ret = static_cast<_Tp*>(::operator new(__bytes));
00222 else
00223 {
00224 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
00225
00226 __scoped_lock sentry(_M_get_mutex());
00227 _Obj* __restrict__ __result = *__free_list;
00228 if (__builtin_expect(__result == 0, 0))
00229 __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
00230 else
00231 {
00232 *__free_list = __result->_M_free_list_link;
00233 __ret = reinterpret_cast<_Tp*>(__result);
00234 }
00235 if (__ret == 0)
00236 std::__throw_bad_alloc();
00237 }
00238 }
00239 return __ret;
00240 }
00241
00242 template<typename _Tp>
00243 void
00244 __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
00245 {
00246 if (__builtin_expect(__n != 0 && __p != 0, true))
00247 {
00248 const size_t __bytes = __n * sizeof(_Tp);
00249 if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new > 0)
00250 ::operator delete(__p);
00251 else
00252 {
00253 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
00254 _Obj* __q = reinterpret_cast<_Obj*>(__p);
00255
00256 __scoped_lock sentry(_M_get_mutex());
00257 __q ->_M_free_list_link = *__free_list;
00258 *__free_list = __q;
00259 }
00260 }
00261 }
00262
00263 _GLIBCXX_END_NAMESPACE_VERSION
00264 }
00265
00266 #endif