pool_allocator.h

Go to the documentation of this file.
00001 // Allocators -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 2, 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 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  * Copyright (c) 1996-1997
00033  * Silicon Graphics Computer Systems, Inc.
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Silicon Graphics makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  */
00043 
00044 /** @file ext/pool_allocator.h
00045  *  This file is a GNU extension to the Standard C++ Library.
00046  */
00047 
00048 #ifndef _POOL_ALLOCATOR_H
00049 #define _POOL_ALLOCATOR_H 1
00050 
00051 #include <bits/c++config.h>
00052 #include <cstdlib>
00053 #include <new>
00054 #include <bits/functexcept.h>
00055 #include <ext/atomicity.h>
00056 #include <ext/concurrence.h>
00057 #include <bits/stl_move.h>
00058 
00059 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
00060 
00061   using std::size_t;
00062   using std::ptrdiff_t;
00063 
00064   /**
00065    *  @brief  Base class for __pool_alloc.
00066    *
00067    *  Uses various allocators to fulfill underlying requests (and makes as
00068    *  few requests as possible when in default high-speed pool mode).
00069    *
00070    *  Important implementation properties:
00071    *  0. If globally mandated, then allocate objects from new
00072    *  1. If the clients request an object of size > _S_max_bytes, the resulting
00073    *     object will be obtained directly from new
00074    *  2. In all other cases, we allocate an object of size exactly
00075    *     _S_round_up(requested_size).  Thus the client has enough size
00076    *     information that we can return the object to the proper free list
00077    *     without permanently losing part of the object.
00078    */
00079     class __pool_alloc_base
00080     {
00081     protected:
00082 
00083       enum { _S_align = 8 };
00084       enum { _S_max_bytes = 128 };
00085       enum { _S_free_list_size = (size_t)_S_max_bytes / (size_t)_S_align };
00086       
00087       union _Obj
00088       {
00089     union _Obj* _M_free_list_link;
00090     char        _M_client_data[1];    // The client sees this.
00091       };
00092       
00093       static _Obj* volatile         _S_free_list[_S_free_list_size];
00094 
00095       // Chunk allocation state.
00096       static char*                  _S_start_free;
00097       static char*                  _S_end_free;
00098       static size_t                 _S_heap_size;     
00099       
00100       size_t
00101       _M_round_up(size_t __bytes)
00102       { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
00103       
00104       _Obj* volatile*
00105       _M_get_free_list(size_t __bytes);
00106     
00107       __mutex&
00108       _M_get_mutex();
00109 
00110       // Returns an object of size __n, and optionally adds to size __n
00111       // free list.
00112       void*
00113       _M_refill(size_t __n);
00114       
00115       // Allocates a chunk for nobjs of size size.  nobjs may be reduced
00116       // if it is inconvenient to allocate the requested number.
00117       char*
00118       _M_allocate_chunk(size_t __n, int& __nobjs);
00119     };
00120 
00121 
00122   /// class __pool_alloc.
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 &__x; }
00153 
00154       const_pointer
00155       address(const_reference __x) const { return &__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 (__builtin_expect(__n > this->max_size(), false))
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 (__builtin_expect(__ret == 0, 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
00264 
00265 #endif

Generated on Sat Oct 25 05:09:06 2008 for libstdc++ by  doxygen 1.5.6