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 <new>
00052
#include <bits/functexcept.h>
00053
#include <bits/atomicity.h>
00054
#include <bits/concurrence.h>
00055
00056
namespace __gnu_cxx
00057 {
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 class __pool_alloc_base
00076 {
00077
protected:
00078
00079
enum { _S_align = 8 };
00080
enum { _S_max_bytes = 128 };
00081
enum { _S_free_list_size = _S_max_bytes / _S_align };
00082
00083
union _Obj
00084 {
00085
union _Obj* _M_free_list_link;
00086
char _M_client_data[1];
00087 };
00088
00089
static _Obj*
volatile _S_free_list[_S_free_list_size];
00090
00091
00092
static char* _S_start_free;
00093
static char* _S_end_free;
00094
static size_t _S_heap_size;
00095
00096 size_t
00097 _M_round_up(size_t __bytes)
00098 {
return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
00099
00100 _Obj*
volatile*
00101 _M_get_free_list(size_t __bytes);
00102
00103 mutex_type&
00104 _M_get_mutex();
00105
00106
00107
00108
void*
00109 _M_refill(size_t __n);
00110
00111
00112
00113
char*
00114 _M_allocate_chunk(size_t __n,
int& __nobjs);
00115 };
00116
00117
00118
template<
typename _Tp>
00119
class __pool_alloc :
private __pool_alloc_base
00120 {
00121
private:
00122
static _Atomic_word _S_force_new;
00123
00124
public:
00125
typedef size_t size_type;
00126
typedef ptrdiff_t difference_type;
00127
typedef _Tp* pointer;
00128
typedef const _Tp* const_pointer;
00129
typedef _Tp& reference;
00130
typedef const _Tp& const_reference;
00131
typedef _Tp value_type;
00132
00133
template<
typename _Tp1>
00134
struct rebind
00135 {
typedef __pool_alloc<_Tp1> other; };
00136
00137 __pool_alloc() throw() { }
00138
00139 __pool_alloc(
const __pool_alloc&) throw() { }
00140
00141
template<
typename _Tp1>
00142 __pool_alloc(
const __pool_alloc<_Tp1>&) throw() { }
00143
00144 ~__pool_alloc() throw() { }
00145
00146 pointer
00147 address(reference __x)
const {
return &__x; }
00148
00149 const_pointer
00150 address(const_reference __x)
const {
return &__x; }
00151
00152 size_type
00153 max_size() const throw()
00154 {
return size_t(-1) /
sizeof(_Tp); }
00155
00156
00157
00158
void
00159 construct(pointer __p,
const _Tp& __val)
00160 { ::new(__p) _Tp(__val); }
00161
00162
void
00163 destroy(pointer __p) { __p->~_Tp(); }
00164
00165 pointer
00166 allocate(size_type __n,
const void* = 0);
00167
00168
void
00169 deallocate(pointer __p, size_type __n);
00170 };
00171
00172
template<
typename _Tp>
00173
inline bool
00174 operator==(
const __pool_alloc<_Tp>&,
const __pool_alloc<_Tp>&)
00175 {
return true; }
00176
00177
template<
typename _Tp>
00178
inline bool
00179 operator!=(
const __pool_alloc<_Tp>&,
const __pool_alloc<_Tp>&)
00180 {
return false; }
00181
00182
template<
typename _Tp>
00183 _Atomic_word
00184 __pool_alloc<_Tp>::_S_force_new;
00185
00186
template<
typename _Tp>
00187 _Tp*
00188 __pool_alloc<_Tp>::allocate(size_type __n,
const void*)
00189 {
00190 pointer __ret = 0;
00191
if (__n)
00192 {
00193
if (__n <= max_size())
00194 {
00195
00196
00197
00198
if (_S_force_new == 0)
00199 {
00200
if (getenv(
"GLIBCXX_FORCE_NEW"))
00201 __atomic_add(&_S_force_new, 1);
00202
else
00203 __atomic_add(&_S_force_new, -1);
00204 }
00205
00206
const size_t __bytes = __n *
sizeof(_Tp);
00207
if (__bytes > size_t(_S_max_bytes) || _S_force_new == 1)
00208 __ret = static_cast<_Tp*>(::operator
new(__bytes));
00209
else
00210 {
00211 _Obj*
volatile* __free_list = _M_get_free_list(__bytes);
00212
00213 lock sentry(_M_get_mutex());
00214 _Obj* __restrict__ __result = *__free_list;
00215
if (__builtin_expect(__result == 0, 0))
00216 __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
00217
else
00218 {
00219 *__free_list = __result->_M_free_list_link;
00220 __ret = reinterpret_cast<_Tp*>(__result);
00221 }
00222
if (__builtin_expect(__ret == 0, 0))
00223 std::__throw_bad_alloc();
00224 }
00225 }
00226
else
00227 std::__throw_bad_alloc();
00228 }
00229
return __ret;
00230 }
00231
00232
template<
typename _Tp>
00233
void
00234 __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
00235 {
00236
if (__n)
00237 {
00238
const size_t __bytes = __n *
sizeof(_Tp);
00239
if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new == 1)
00240 ::operator
delete(__p);
00241
else
00242 {
00243 _Obj*
volatile* __free_list = _M_get_free_list(__bytes);
00244 _Obj* __q = reinterpret_cast<_Obj*>(__p);
00245
00246 lock sentry(_M_get_mutex());
00247 __q ->_M_free_list_link = *__free_list;
00248 *__free_list = __q;
00249 }
00250 }
00251 }
00252 }
00253
00254
#endif