# 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  */
    00046
    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 {
    00059   /**
    00060    *  @brief  Base class for __pool_alloc.
    00061    *
    00062    *  @if maint
    00063    *  Uses various allocators to fulfill underlying requests (and makes as
    00064    *  few requests as possible when in default high-speed pool mode).
    00065    *
    00066    *  Important implementation properties:
    00067    *  0. If globally mandated, then allocate objects from new
    00068    *  1. If the clients request an object of size > _S_max_bytes, the resulting
    00069    *     object will be obtained directly from new
    00070    *  2. In all other cases, we allocate an object of size exactly
    00071    *     _S_round_up(requested_size).  Thus the client has enough size
    00072    *     information that we can return the object to the proper free list
    00073    *     without permanently losing part of the object.
    00074    *
    00075    *  @endif
    00076    */
    00077     class __pool_alloc_base
    00078     {
    00079     protected:
    00080
    00081       enum { _S_align = 8 };
    00082       enum { _S_max_bytes = 128 };
    00083       enum { _S_free_list_size = _S_max_bytes / _S_align };
    00084
    00085       union _Obj
    00086       {
    00087     union _Obj* _M_free_list_link;
    00088     char        _M_client_data[1];    // The client sees this.
    00089       };
    00090
    00091       static _Obj* volatile         _S_free_list[_S_free_list_size];
    00092
    00093       // Chunk allocation state.
    00094       static char*                  _S_start_free;
    00095       static char*                  _S_end_free;
    00096       static size_t                 _S_heap_size;
    00097
    00098       size_t
    00099       _M_round_up(size_t __bytes)
    00100       { return ((__bytes + (size_t)_S_align - 1) & ~((size_t)_S_align - 1)); }
    00101
    00102       _Obj* volatile*
    00103       _M_get_free_list(size_t __bytes);
    00104
    00105       mutex_type&
    00106       _M_get_mutex();
    00107
    00108       // Returns an object of size __n, and optionally adds to size __n
    00109       // free list.
    00110       void*
    00111       _M_refill(size_t __n);
    00112
    00113       // Allocates a chunk for nobjs of size size.  nobjs may be reduced
    00114       // if it is inconvenient to allocate the requested number.
    00115       char*
    00116       _M_allocate_chunk(size_t __n, int& __nobjs);
    00117     };
    00118
    00119
    00120   /// @brief  class __pool_alloc.
    00121   template<typename _Tp>
    00122     class __pool_alloc : private __pool_alloc_base
    00123     {
    00124     private:
    00125       static _Atomic_word       _S_force_new;
    00126
    00127     public:
    00128       typedef size_t     size_type;
    00129       typedef ptrdiff_t  difference_type;
    00130       typedef _Tp*       pointer;
    00131       typedef const _Tp* const_pointer;
    00132       typedef _Tp&       reference;
    00133       typedef const _Tp& const_reference;
    00134       typedef _Tp        value_type;
    00135
    00136       template<typename _Tp1>
    00137         struct rebind
    00138         { typedef __pool_alloc<_Tp1> other; };
    00139
    00140       __pool_alloc() throw() { }
    00141
    00142       __pool_alloc(const __pool_alloc&) throw() { }
    00143
    00144       template<typename _Tp1>
    00145         __pool_alloc(const __pool_alloc<_Tp1>&) throw() { }
    00146
    00147       ~__pool_alloc() throw() { }
    00148
    00149       pointer
    00150       address(reference __x) const { return &__x; }
    00151
    00152       const_pointer
    00153       address(const_reference __x) const { return &__x; }
    00154
    00155       size_type
    00156       max_size() const throw()
    00157       { return size_t(-1) / sizeof(_Tp); }
    00158
    00159       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00160       // 402. wrong new expression in [some_] allocator::construct
    00161       void
    00162       construct(pointer __p, const _Tp& __val)
    00163       { ::new(__p) _Tp(__val); }
    00164
    00165       void
    00166       destroy(pointer __p) { __p->~_Tp(); }
    00167
    00168       pointer
    00169       allocate(size_type __n, const void* = 0);
    00170
    00171       void
    00172       deallocate(pointer __p, size_type __n);
    00173     };
    00174
    00175   template<typename _Tp>
    00176     inline bool
    00177     operator==(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
    00178     { return true; }
    00179
    00180   template<typename _Tp>
    00181     inline bool
    00182     operator!=(const __pool_alloc<_Tp>&, const __pool_alloc<_Tp>&)
    00183     { return false; }
    00184
    00185   template<typename _Tp>
    00186     _Atomic_word
    00187     __pool_alloc<_Tp>::_S_force_new;
    00188
    00189   template<typename _Tp>
    00190     _Tp*
    00191     __pool_alloc<_Tp>::allocate(size_type __n, const void*)
    00192     {
    00193       pointer __ret = 0;
    00194       if (__builtin_expect(__n != 0, true))
    00195     {
    00196       if (__builtin_expect(__n > this->max_size(), false))
    00197         std::__throw_bad_alloc();
    00198
    00199       // If there is a race through here, assume answer from getenv
    00200       // will resolve in same direction.  Inspired by techniques
    00201       // to efficiently support threading found in basic_string.h.
    00202       if (_S_force_new == 0)
    00203         {
    00204           if (getenv("GLIBCXX_FORCE_NEW"))
    00205         __atomic_add(&_S_force_new, 1);
    00206           else
    00207         __atomic_add(&_S_force_new, -1);
    00208         }
    00209
    00210       const size_t __bytes = __n * sizeof(_Tp);
    00211       if (__bytes > size_t(_S_max_bytes) || _S_force_new == 1)
    00212         __ret = static_cast<_Tp*>(::operator new(__bytes));
    00213       else
    00214         {
    00215           _Obj* volatile* __free_list = _M_get_free_list(__bytes);
    00216
    00217           lock sentry(_M_get_mutex());
    00218           _Obj* __restrict__ __result = *__free_list;
    00219           if (__builtin_expect(__result == 0, 0))
    00220         __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
    00221           else
    00222         {
    00223           *__free_list = __result->_M_free_list_link;
    00224           __ret = reinterpret_cast<_Tp*>(__result);
    00225         }
    00226           if (__builtin_expect(__ret == 0, 0))
    00227         std::__throw_bad_alloc();
    00228         }
    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 (__builtin_expect(__n != 0 && __p != 0, true))
    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