pool_allocator.h

Go to the documentation of this file.
00001 // Allocators -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * Copyright (c) 1996-1997 00032 * Silicon Graphics Computer Systems, Inc. 00033 * 00034 * Permission to use, copy, modify, distribute and sell this software 00035 * and its documentation for any purpose is hereby granted without fee, 00036 * provided that the above copyright notice appear in all copies and 00037 * that both that copyright notice and this permission notice appear 00038 * in supporting documentation. Silicon Graphics makes no 00039 * representations about the suitability of this software for any 00040 * purpose. It is provided "as is" without express or implied warranty. 00041 */ 00042 00043 /** @file ext/pool_allocator.h 00044 * This file is a GNU extension to the Standard C++ Library. 00045 * You should only include this header if you are using GCC 3 or later. 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 * @if maint 00060 * Uses various allocators to fulfill underlying requests (and makes as 00061 * few requests as possible when in default high-speed pool mode). 00062 * 00063 * Important implementation properties: 00064 * 0. If globally mandated, then allocate objects from new 00065 * 1. If the clients request an object of size > _S_max_bytes, the resulting 00066 * object will be obtained directly from new 00067 * 2. In all other cases, we allocate an object of size exactly 00068 * _S_round_up(requested_size). Thus the client has enough size 00069 * information that we can return the object to the proper free list 00070 * without permanently losing part of the object. 00071 * 00072 * @endif 00073 * (See @link Allocators allocators info @endlink for more.) 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]; // The client sees this. 00087 }; 00088 00089 static _Obj* volatile _S_free_list[_S_free_list_size]; 00090 00091 // Chunk allocation state. 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 // Returns an object of size __n, and optionally adds to size __n 00107 // free list. 00108 void* 00109 _M_refill(size_t __n); 00110 00111 // Allocates a chunk for nobjs of size size. nobjs may be reduced 00112 // if it is inconvenient to allocate the requested number. 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 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00157 // 402. wrong new expression in [some_] allocator::construct 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 // If there is a race through here, assume answer from getenv 00196 // will resolve in same direction. Inspired by techniques 00197 // to efficiently support threading found in basic_string.h. 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 } // namespace __gnu_cxx 00253 00254 #endif

Generated on Sun Sep 12 15:49:58 2004 for libstdc++ source by doxygen 1.3.8