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 
00047 #ifndef _POOL_ALLOCATOR_H
00048 #define _POOL_ALLOCATOR_H 1
00049 
00050 #include <bits/c++config.h>
00051 #include <cstdlib>
00052 #include <new>
00053 #include <bits/functexcept.h>
00054 #include <bits/atomicity.h>
00055 #include <bits/concurrence.h>
00056 
00057 namespace __gnu_cxx
00058 {
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 = _S_max_bytes / _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       _Obj* volatile*
00102       _M_get_free_list(size_t __bytes);
00103     
00104       mutex_type&
00105       _M_get_mutex();
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   template<typename _Tp>
00120     class __pool_alloc : private __pool_alloc_base
00121     {
00122     private:
00123       static _Atomic_word       _S_force_new;
00124 
00125     public:
00126       typedef size_t     size_type;
00127       typedef ptrdiff_t  difference_type;
00128       typedef _Tp*       pointer;
00129       typedef const _Tp* const_pointer;
00130       typedef _Tp&       reference;
00131       typedef const _Tp& const_reference;
00132       typedef _Tp        value_type;
00133 
00134       template<typename _Tp1>
00135         struct rebind
00136         { typedef __pool_alloc<_Tp1> other; };
00137 
00138       __pool_alloc() throw() { }
00139 
00140       __pool_alloc(const __pool_alloc&) throw() { }
00141 
00142       template<typename _Tp1>
00143         __pool_alloc(const __pool_alloc<_Tp1>&) throw() { }
00144 
00145       ~__pool_alloc() throw() { }
00146 
00147       pointer
00148       address(reference __x) const { return &__x; }
00149 
00150       const_pointer
00151       address(const_reference __x) const { return &__x; }
00152 
00153       size_type
00154       max_size() const throw() 
00155       { return size_t(-1) / sizeof(_Tp); }
00156 
00157       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00158       // 402. wrong new expression in [some_] allocator::construct
00159       void 
00160       construct(pointer __p, const _Tp& __val) 
00161       { ::new(__p) _Tp(__val); }
00162 
00163       void 
00164       destroy(pointer __p) { __p->~_Tp(); }
00165 
00166       pointer
00167       allocate(size_type __n, const void* = 0);
00168 
00169       void
00170       deallocate(pointer __p, size_type __n);      
00171     };
00172 
00173   template<typename _Tp>
00174     inline bool
00175     operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
00176     { return true; }
00177 
00178   template<typename _Tp>
00179     inline bool
00180     operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
00181     { return false; }
00182 
00183   template<typename _Tp>
00184     _Atomic_word
00185     __pool_alloc<_Tp>::_S_force_new;
00186 
00187   template<typename _Tp>
00188     _Tp*
00189     __pool_alloc<_Tp>::allocate(size_type __n, const void*)
00190     {
00191       pointer __ret = 0;
00192       if (__n)
00193     {
00194       if (__n <= max_size())
00195         {
00196           // If there is a race through here, assume answer from getenv
00197           // will resolve in same direction.  Inspired by techniques
00198           // to efficiently support threading found in basic_string.h.
00199           if (_S_force_new == 0)
00200         {
00201           if (getenv("GLIBCXX_FORCE_NEW"))
00202             __atomic_add(&_S_force_new, 1);
00203           else
00204             __atomic_add(&_S_force_new, -1);
00205         }
00206 
00207           const size_t __bytes = __n * sizeof(_Tp);       
00208           if (__bytes > size_t(_S_max_bytes) || _S_force_new == 1)
00209         __ret = static_cast<_Tp*>(::operator new(__bytes));
00210           else
00211         {
00212           _Obj* volatile* __free_list = _M_get_free_list(__bytes);
00213 
00214           lock sentry(_M_get_mutex());
00215           _Obj* __restrict__ __result = *__free_list;
00216           if (__builtin_expect(__result == 0, 0))
00217             __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
00218           else
00219             {
00220               *__free_list = __result->_M_free_list_link;
00221               __ret = reinterpret_cast<_Tp*>(__result);
00222             }
00223           if (__builtin_expect(__ret == 0, 0))
00224             std::__throw_bad_alloc();
00225         }
00226         }
00227       else
00228         std::__throw_bad_alloc();
00229     }
00230       return __ret;
00231     }
00232 
00233   template<typename _Tp>
00234     void
00235     __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
00236     {
00237       if (__n)
00238     {
00239       const size_t __bytes = __n * sizeof(_Tp);
00240       if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new == 1)
00241         ::operator delete(__p);
00242       else
00243         {
00244           _Obj* volatile* __free_list = _M_get_free_list(__bytes);
00245           _Obj* __q = reinterpret_cast<_Obj*>(__p);
00246 
00247           lock sentry(_M_get_mutex());
00248           __q ->_M_free_list_link = *__free_list;
00249           *__free_list = __q;
00250         }
00251     }
00252     }
00253 } // namespace __gnu_cxx
00254 
00255 #endif

Generated on Tue Jan 30 17:31:52 2007 for GNU C++ STL by doxygen 1.3.6