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
00044
00045
00046
00047 #ifndef _POOL_ALLOCATOR_H
00048 #define _POOL_ALLOCATOR_H 1
00049
00050 #include <bits/c++config.h>
00051 #include <cstdlib>
00052 #include <new>
00053 #include <bits/functexcept.h>
00054 #include <bits/atomicity.h>
00055 #include <bits/concurrence.h>
00056
00057 namespace __gnu_cxx
00058 {
00059
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 = _S_max_bytes / _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 _Obj* volatile*
00102 _M_get_free_list(size_t __bytes);
00103
00104 mutex_type&
00105 _M_get_mutex();
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 template<typename _Tp>
00120 class __pool_alloc : private __pool_alloc_base
00121 {
00122 private:
00123 static _Atomic_word _S_force_new;
00124
00125 public:
00126 typedef size_t size_type;
00127 typedef ptrdiff_t difference_type;
00128 typedef _Tp* pointer;
00129 typedef const _Tp* const_pointer;
00130 typedef _Tp& reference;
00131 typedef const _Tp& const_reference;
00132 typedef _Tp value_type;
00133
00134 template<typename _Tp1>
00135 struct rebind
00136 { typedef __pool_alloc<_Tp1> other; };
00137
00138 __pool_alloc() throw() { }
00139
00140 __pool_alloc(const __pool_alloc&) throw() { }
00141
00142 template<typename _Tp1>
00143 __pool_alloc(const __pool_alloc<_Tp1>&) throw() { }
00144
00145 ~__pool_alloc() throw() { }
00146
00147 pointer
00148 address(reference __x) const { return &__x; }
00149
00150 const_pointer
00151 address(const_reference __x) const { return &__x; }
00152
00153 size_type
00154 max_size() const throw()
00155 { return size_t(-1) / sizeof(_Tp); }
00156
00157
00158
00159 void
00160 construct(pointer __p, const _Tp& __val)
00161 { ::new(__p) _Tp(__val); }
00162
00163 void
00164 destroy(pointer __p) { __p->~_Tp(); }
00165
00166 pointer
00167 allocate(size_type __n, const void* = 0);
00168
00169 void
00170 deallocate(pointer __p, size_type __n);
00171 };
00172
00173 template<typename _Tp>
00174 inline bool
00175 operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
00176 { return true; }
00177
00178 template<typename _Tp>
00179 inline bool
00180 operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
00181 { return false; }
00182
00183 template<typename _Tp>
00184 _Atomic_word
00185 __pool_alloc<_Tp>::_S_force_new;
00186
00187 template<typename _Tp>
00188 _Tp*
00189 __pool_alloc<_Tp>::allocate(size_type __n, const void*)
00190 {
00191 pointer __ret = 0;
00192 if (__n)
00193 {
00194 if (__n <= max_size())
00195 {
00196
00197
00198
00199 if (_S_force_new == 0)
00200 {
00201 if (getenv("GLIBCXX_FORCE_NEW"))
00202 __atomic_add(&_S_force_new, 1);
00203 else
00204 __atomic_add(&_S_force_new, -1);
00205 }
00206
00207 const size_t __bytes = __n * sizeof(_Tp);
00208 if (__bytes > size_t(_S_max_bytes) || _S_force_new == 1)
00209 __ret = static_cast<_Tp*>(::operator new(__bytes));
00210 else
00211 {
00212 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
00213
00214 lock sentry(_M_get_mutex());
00215 _Obj* __restrict__ __result = *__free_list;
00216 if (__builtin_expect(__result == 0, 0))
00217 __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
00218 else
00219 {
00220 *__free_list = __result->_M_free_list_link;
00221 __ret = reinterpret_cast<_Tp*>(__result);
00222 }
00223 if (__builtin_expect(__ret == 0, 0))
00224 std::__throw_bad_alloc();
00225 }
00226 }
00227 else
00228 std::__throw_bad_alloc();
00229 }
00230 return __ret;
00231 }
00232
00233 template<typename _Tp>
00234 void
00235 __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
00236 {
00237 if (__n)
00238 {
00239 const size_t __bytes = __n * sizeof(_Tp);
00240 if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new == 1)
00241 ::operator delete(__p);
00242 else
00243 {
00244 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
00245 _Obj* __q = reinterpret_cast<_Obj*>(__p);
00246
00247 lock sentry(_M_get_mutex());
00248 __q ->_M_free_list_link = *__free_list;
00249 *__free_list = __q;
00250 }
00251 }
00252 }
00253 }
00254
00255 #endif