Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libstdc++-v3 / include / ext / pool_allocator.h
blob41ae4f4adfdc8f5ff639a7e2c7482da5c7ca93ca
1 // Allocators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * Copyright (c) 1996-1997
33 * Silicon Graphics Computer Systems, Inc.
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 /** @file ext/pool_allocator.h
45 * This file is a GNU extension to the Standard C++ Library.
48 #ifndef _POOL_ALLOCATOR_H
49 #define _POOL_ALLOCATOR_H 1
51 #include <bits/c++config.h>
52 #include <cstdlib>
53 #include <new>
54 #include <bits/functexcept.h>
55 #include <ext/atomicity.h>
56 #include <ext/concurrence.h>
57 #include <bits/move.h>
59 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
61 using std::size_t;
62 using std::ptrdiff_t;
64 /**
65 * @brief Base class for __pool_alloc.
67 * Uses various allocators to fulfill underlying requests (and makes as
68 * few requests as possible when in default high-speed pool mode).
70 * Important implementation properties:
71 * 0. If globally mandated, then allocate objects from new
72 * 1. If the clients request an object of size > _S_max_bytes, the resulting
73 * object will be obtained directly from new
74 * 2. In all other cases, we allocate an object of size exactly
75 * _S_round_up(requested_size). Thus the client has enough size
76 * information that we can return the object to the proper free list
77 * without permanently losing part of the object.
79 class __pool_alloc_base
81 protected:
83 enum { _S_align = 8 };
84 enum { _S_max_bytes = 128 };
85 enum { _S_free_list_size = (size_t)_S_max_bytes / (size_t)_S_align };
87 union _Obj
89 union _Obj* _M_free_list_link;
90 char _M_client_data[1]; // The client sees this.
93 static _Obj* volatile _S_free_list[_S_free_list_size];
95 // Chunk allocation state.
96 static char* _S_start_free;
97 static char* _S_end_free;
98 static size_t _S_heap_size;
100 size_t
101 _M_round_up(size_t __bytes)
102 { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
104 _Obj* volatile*
105 _M_get_free_list(size_t __bytes);
107 __mutex&
108 _M_get_mutex();
110 // Returns an object of size __n, and optionally adds to size __n
111 // free list.
112 void*
113 _M_refill(size_t __n);
115 // Allocates a chunk for nobjs of size size. nobjs may be reduced
116 // if it is inconvenient to allocate the requested number.
117 char*
118 _M_allocate_chunk(size_t __n, int& __nobjs);
123 * @brief Allocator using a memory pool with a single lock.
124 * @ingroup allocators
126 template<typename _Tp>
127 class __pool_alloc : private __pool_alloc_base
129 private:
130 static _Atomic_word _S_force_new;
132 public:
133 typedef size_t size_type;
134 typedef ptrdiff_t difference_type;
135 typedef _Tp* pointer;
136 typedef const _Tp* const_pointer;
137 typedef _Tp& reference;
138 typedef const _Tp& const_reference;
139 typedef _Tp value_type;
141 template<typename _Tp1>
142 struct rebind
143 { typedef __pool_alloc<_Tp1> other; };
145 __pool_alloc() throw() { }
147 __pool_alloc(const __pool_alloc&) throw() { }
149 template<typename _Tp1>
150 __pool_alloc(const __pool_alloc<_Tp1>&) throw() { }
152 ~__pool_alloc() throw() { }
154 pointer
155 address(reference __x) const { return &__x; }
157 const_pointer
158 address(const_reference __x) const { return &__x; }
160 size_type
161 max_size() const throw()
162 { return size_t(-1) / sizeof(_Tp); }
164 // _GLIBCXX_RESOLVE_LIB_DEFECTS
165 // 402. wrong new expression in [some_] allocator::construct
166 void
167 construct(pointer __p, const _Tp& __val)
168 { ::new((void *)__p) _Tp(__val); }
170 #ifdef __GXX_EXPERIMENTAL_CXX0X__
171 template<typename... _Args>
172 void
173 construct(pointer __p, _Args&&... __args)
174 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
175 #endif
177 void
178 destroy(pointer __p) { __p->~_Tp(); }
180 pointer
181 allocate(size_type __n, const void* = 0);
183 void
184 deallocate(pointer __p, size_type __n);
187 template<typename _Tp>
188 inline bool
189 operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
190 { return true; }
192 template<typename _Tp>
193 inline bool
194 operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
195 { return false; }
197 template<typename _Tp>
198 _Atomic_word
199 __pool_alloc<_Tp>::_S_force_new;
201 template<typename _Tp>
202 _Tp*
203 __pool_alloc<_Tp>::allocate(size_type __n, const void*)
205 pointer __ret = 0;
206 if (__builtin_expect(__n != 0, true))
208 if (__builtin_expect(__n > this->max_size(), false))
209 std::__throw_bad_alloc();
211 // If there is a race through here, assume answer from getenv
212 // will resolve in same direction. Inspired by techniques
213 // to efficiently support threading found in basic_string.h.
214 if (_S_force_new == 0)
216 if (std::getenv("GLIBCXX_FORCE_NEW"))
217 __atomic_add_dispatch(&_S_force_new, 1);
218 else
219 __atomic_add_dispatch(&_S_force_new, -1);
222 const size_t __bytes = __n * sizeof(_Tp);
223 if (__bytes > size_t(_S_max_bytes) || _S_force_new > 0)
224 __ret = static_cast<_Tp*>(::operator new(__bytes));
225 else
227 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
229 __scoped_lock sentry(_M_get_mutex());
230 _Obj* __restrict__ __result = *__free_list;
231 if (__builtin_expect(__result == 0, 0))
232 __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
233 else
235 *__free_list = __result->_M_free_list_link;
236 __ret = reinterpret_cast<_Tp*>(__result);
238 if (__builtin_expect(__ret == 0, 0))
239 std::__throw_bad_alloc();
242 return __ret;
245 template<typename _Tp>
246 void
247 __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
249 if (__builtin_expect(__n != 0 && __p != 0, true))
251 const size_t __bytes = __n * sizeof(_Tp);
252 if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new > 0)
253 ::operator delete(__p);
254 else
256 _Obj* volatile* __free_list = _M_get_free_list(__bytes);
257 _Obj* __q = reinterpret_cast<_Obj*>(__p);
259 __scoped_lock sentry(_M_get_mutex());
260 __q ->_M_free_list_link = *__free_list;
261 *__free_list = __q;
266 _GLIBCXX_END_NAMESPACE
268 #endif