44 #ifndef _POOL_ALLOCATOR_H
45 #define _POOL_ALLOCATOR_H 1
55 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
81 enum { _S_align = 8 };
82 enum { _S_max_bytes = 128 };
83 enum { _S_free_list_size = (size_t)_S_max_bytes / (
size_t)_S_align };
87 union _Obj* _M_free_list_link;
88 char _M_client_data[1];
91 static _Obj*
volatile _S_free_list[_S_free_list_size];
94 static char* _S_start_free;
95 static char* _S_end_free;
96 static size_t _S_heap_size;
99 _M_round_up(
size_t __bytes)
100 {
return ((__bytes + (
size_t)_S_align - 1) & ~((
size_t)_S_align - 1)); }
102 _GLIBCXX_CONST _Obj*
volatile*
103 _M_get_free_list(
size_t __bytes)
throw ();
106 _M_get_mutex()
throw ();
111 _M_refill(
size_t __n);
116 _M_allocate_chunk(
size_t __n,
int& __nobjs);
124 template<
typename _Tp>
128 static _Atomic_word _S_force_new;
131 typedef size_t size_type;
132 typedef ptrdiff_t difference_type;
133 typedef _Tp* pointer;
134 typedef const _Tp* const_pointer;
135 typedef _Tp& reference;
136 typedef const _Tp& const_reference;
137 typedef _Tp value_type;
139 template<
typename _Tp1>
147 template<
typename _Tp1>
153 address(reference __x)
const _GLIBCXX_NOEXCEPT
157 address(const_reference __x)
const _GLIBCXX_NOEXCEPT
161 max_size()
const _GLIBCXX_USE_NOEXCEPT
162 {
return size_t(-1) /
sizeof(_Tp); }
164 #ifdef __GXX_EXPERIMENTAL_CXX0X__
165 template<
typename _Up,
typename... _Args>
167 construct(_Up* __p, _Args&&... __args)
168 { ::new((
void *)__p) _Up(std::forward<_Args>(__args)...); }
170 template<
typename _Up>
172 destroy(_Up* __p) { __p->~_Up(); }
177 construct(pointer __p,
const _Tp& __val)
178 { ::new((
void *)__p) _Tp(__val); }
181 destroy(pointer __p) { __p->~_Tp(); }
185 allocate(size_type __n,
const void* = 0);
188 deallocate(pointer __p, size_type __n);
191 template<
typename _Tp>
196 template<
typename _Tp>
198 operator!=(
const __pool_alloc<_Tp>&,
const __pool_alloc<_Tp>&)
201 template<
typename _Tp>
203 __pool_alloc<_Tp>::_S_force_new;
205 template<
typename _Tp>
207 __pool_alloc<_Tp>::allocate(size_type __n,
const void*)
210 if (__builtin_expect(__n != 0,
true))
212 if (__n > this->max_size())
213 std::__throw_bad_alloc();
218 if (_S_force_new == 0)
220 if (std::getenv(
"GLIBCXX_FORCE_NEW"))
221 __atomic_add_dispatch(&_S_force_new, 1);
223 __atomic_add_dispatch(&_S_force_new, -1);
226 const size_t __bytes = __n *
sizeof(_Tp);
227 if (__bytes >
size_t(_S_max_bytes) || _S_force_new > 0)
228 __ret = static_cast<_Tp*>(::
operator new(__bytes));
231 _Obj*
volatile* __free_list = _M_get_free_list(__bytes);
233 __scoped_lock sentry(_M_get_mutex());
234 _Obj* __restrict__ __result = *__free_list;
235 if (__builtin_expect(__result == 0, 0))
236 __ret =
static_cast<_Tp*
>(_M_refill(_M_round_up(__bytes)));
239 *__free_list = __result->_M_free_list_link;
240 __ret =
reinterpret_cast<_Tp*
>(__result);
243 std::__throw_bad_alloc();
249 template<
typename _Tp>
251 __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
253 if (__builtin_expect(__n != 0 && __p != 0,
true))
255 const size_t __bytes = __n *
sizeof(_Tp);
256 if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new > 0)
257 ::
operator delete(__p);
260 _Obj*
volatile* __free_list = _M_get_free_list(__bytes);
261 _Obj* __q =
reinterpret_cast<_Obj*
>(__p);
263 __scoped_lock sentry(_M_get_mutex());
264 __q ->_M_free_list_link = *__free_list;
270 _GLIBCXX_END_NAMESPACE_VERSION
Base class for __pool_alloc.
_Tp * __addressof(_Tp &__r) _GLIBCXX_NOEXCEPT
Same as C++11 std::addressof.
Allocator using a memory pool with a single lock.