# allocator.h

Go to the documentation of this file.

    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 allocator.h
    00040  *  This is an internal header file, included by other library headers.
    00041  *  You should not attempt to use it directly.
    00042  */
    00043
    00044 #ifndef _ALLOCATOR_H
    00045 #define _ALLOCATOR_H 1
    00046
    00047 // Define the base class to std::allocator.
    00048 #include <bits/c++allocator.h>
    00049
    00050 _GLIBCXX_BEGIN_NAMESPACE(std)
    00051
    00052   /**
    00053    * @defgroup allocators Allocators
    00054    * @ingroup memory
    00055    *
    00056    * Classes encapsulating memory operations.
    00057    */
    00058
    00059   template<typename _Tp>
    00060     class allocator;
    00061
    00062   /// allocator<void> specialization.
    00063   template<>
    00064     class allocator<void>
    00065     {
    00066     public:
    00067       typedef size_t      size_type;
    00068       typedef ptrdiff_t   difference_type;
    00069       typedef void*       pointer;
    00070       typedef const void* const_pointer;
    00071       typedef void        value_type;
    00072
    00073       template<typename _Tp1>
    00074         struct rebind
    00075         { typedef allocator<_Tp1> other; };
    00076     };
    00077
    00078   /**
    00079    * @brief  The @a standard allocator, as per [20.4].
    00080    * @ingroup allocators
    00081    *
    00082    *  Further details:
    00083    *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html
    00084    */
    00085   template<typename _Tp>
    00086     class allocator: public __glibcxx_base_allocator<_Tp>
    00087     {
    00088    public:
    00089       typedef size_t     size_type;
    00090       typedef ptrdiff_t  difference_type;
    00091       typedef _Tp*       pointer;
    00092       typedef const _Tp* const_pointer;
    00093       typedef _Tp&       reference;
    00094       typedef const _Tp& const_reference;
    00095       typedef _Tp        value_type;
    00096
    00097       template<typename _Tp1>
    00098         struct rebind
    00099         { typedef allocator<_Tp1> other; };
    00100
    00101       allocator() throw() { }
    00102
    00103       allocator(const allocator& __a) throw()
    00104       : __glibcxx_base_allocator<_Tp>(__a) { }
    00105
    00106       template<typename _Tp1>
    00107         allocator(const allocator<_Tp1>&) throw() { }
    00108
    00109       ~allocator() throw() { }
    00110
    00111       // Inherit everything else.
    00112     };
    00113
    00114   template<typename _T1, typename _T2>
    00115     inline bool
    00116     operator==(const allocator<_T1>&, const allocator<_T2>&)
    00117     { return true; }
    00118
    00119   template<typename _Tp>
    00120     inline bool
    00121     operator==(const allocator<_Tp>&, const allocator<_Tp>&)
    00122     { return true; }
    00123
    00124   template<typename _T1, typename _T2>
    00125     inline bool
    00126     operator!=(const allocator<_T1>&, const allocator<_T2>&)
    00127     { return false; }
    00128
    00129   template<typename _Tp>
    00130     inline bool
    00131     operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
    00132     { return false; }
    00133
    00134   // Inhibit implicit instantiations for required instantiations,
    00135   // which are defined via explicit instantiations elsewhere.
    00136   // NB: This syntax is a GNU extension.
    00137 #if _GLIBCXX_EXTERN_TEMPLATE
    00138   extern template class allocator<char>;
    00139   extern template class allocator<wchar_t>;
    00140 #endif
    00141
    00142   // Undefine.
    00143 #undef __glibcxx_base_allocator
    00144
    00145   // To implement Option 3 of DR 431.
    00146   template<typename _Alloc, bool = __is_empty(_Alloc)>
    00147     struct __alloc_swap
    00148     { static void _S_do_it(_Alloc&, _Alloc&) { } };
    00149
    00150   template<typename _Alloc>
    00151     struct __alloc_swap<_Alloc, false>
    00152     {
    00153       static void
    00154       _S_do_it(_Alloc& __one, _Alloc& __two)
    00155       {
    00156     // Precondition: swappable allocators.
    00157     if (__one != __two)
    00158       swap(__one, __two);
    00159       }
    00160     };
    00161
    00162   // Optimize for stateless allocators.
    00163   template<typename _Alloc, bool = __is_empty(_Alloc)>
    00164     struct __alloc_neq
    00165     {
    00166       static bool
    00167       _S_do_it(const _Alloc&, const _Alloc&)
    00168       { return false; }
    00169     };
    00170
    00171   template<typename _Alloc>
    00172     struct __alloc_neq<_Alloc, false>
    00173     {
    00174       static bool
    00175       _S_do_it(const _Alloc& __one, const _Alloc& __two)
    00176       { return __one != __two; }
    00177     };
    00178
    00179 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00180    // A very basic implementation for now.  In general we have to wait for
    00181    // the availability of the infrastructure described in N2983:  we should
    00182    // try when either T has a move constructor which cannot throw or T is
    00183    // CopyContructible.
    00184    // NB: This code doesn't properly belong here, we should find a more
    00185    // suited place common to std::vector and std::deque.
    00186    template<typename _Tp,
    00187         bool = __has_trivial_copy(typename _Tp::value_type)>
    00188      struct __shrink_to_fit
    00189      { static void _S_do_it(_Tp&) { } };
    00190
    00191    template<typename _Tp>
    00192      struct __shrink_to_fit<_Tp, true>
    00193      {
    00194        static void
    00195        _S_do_it(_Tp& __v)
    00196        {
    00197      __try
    00198        { _Tp(__v).swap(__v); }
    00199      __catch(...) { }
    00200        }
    00201      };
    00202 #endif
    00203
    00204 _GLIBCXX_END_NAMESPACE
    00205
    00206 #endif