libstdc++
|
00001 // Allocators -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /* 00027 * Copyright (c) 1996-1997 00028 * Silicon Graphics Computer Systems, Inc. 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Silicon Graphics makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 */ 00038 00039 /** @file ext/pool_allocator.h 00040 * This file is a GNU extension to the Standard C++ Library. 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 * @brief Base class for __pool_alloc. 00063 * 00064 * Uses various allocators to fulfill underlying requests (and makes as 00065 * few requests as possible when in default high-speed pool mode). 00066 * 00067 * Important implementation properties: 00068 * 0. If globally mandated, then allocate objects from new 00069 * 1. If the clients request an object of size > _S_max_bytes, the resulting 00070 * object will be obtained directly from new 00071 * 2. In all other cases, we allocate an object of size exactly 00072 * _S_round_up(requested_size). Thus the client has enough size 00073 * information that we can return the object to the proper free list 00074 * without permanently losing part of the object. 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]; // The client sees this. 00088 }; 00089 00090 static _Obj* volatile _S_free_list[_S_free_list_size]; 00091 00092 // Chunk allocation state. 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 // Returns an object of size __n, and optionally adds to size __n 00108 // free list. 00109 void* 00110 _M_refill(size_t __n); 00111 00112 // Allocates a chunk for nobjs of size size. nobjs may be reduced 00113 // if it is inconvenient to allocate the requested number. 00114 char* 00115 _M_allocate_chunk(size_t __n, int& __nobjs); 00116 }; 00117 00118 00119 /** 00120 * @brief Allocator using a memory pool with a single lock. 00121 * @ingroup allocators 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 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00162 // 402. wrong new expression in [some_] allocator::construct 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 // If there is a race through here, assume answer from getenv 00209 // will resolve in same direction. Inspired by techniques 00210 // to efficiently support threading found in basic_string.h. 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 } // namespace 00265 00266 #endif