libstdc++
new_allocator.h
Go to the documentation of this file.
00001 // Allocator that wraps operator new -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 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 /** @file ext/new_allocator.h
00027  *  This file is a GNU extension to the Standard C++ Library.
00028  */
00029 
00030 #ifndef _NEW_ALLOCATOR_H
00031 #define _NEW_ALLOCATOR_H 1
00032 
00033 #include <bits/c++config.h>
00034 #include <new>
00035 #include <bits/functexcept.h>
00036 #include <bits/move.h>
00037 
00038 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041 
00042   using std::size_t;
00043   using std::ptrdiff_t;
00044 
00045   /**
00046    *  @brief  An allocator that uses global new, as per [20.4].
00047    *  @ingroup allocators
00048    *
00049    *  This is precisely the allocator defined in the C++ Standard. 
00050    *    - all allocation calls operator new
00051    *    - all deallocation calls operator delete
00052    */
00053   template<typename _Tp>
00054     class new_allocator
00055     {
00056     public:
00057       typedef size_t     size_type;
00058       typedef ptrdiff_t  difference_type;
00059       typedef _Tp*       pointer;
00060       typedef const _Tp* const_pointer;
00061       typedef _Tp&       reference;
00062       typedef const _Tp& const_reference;
00063       typedef _Tp        value_type;
00064 
00065       template<typename _Tp1>
00066         struct rebind
00067         { typedef new_allocator<_Tp1> other; };
00068 
00069       new_allocator() _GLIBCXX_USE_NOEXCEPT { }
00070 
00071       new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { }
00072 
00073       template<typename _Tp1>
00074         new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { }
00075 
00076       ~new_allocator() _GLIBCXX_USE_NOEXCEPT { }
00077 
00078       pointer
00079       address(reference __x) const _GLIBCXX_NOEXCEPT
00080       { return std::__addressof(__x); }
00081 
00082       const_pointer
00083       address(const_reference __x) const _GLIBCXX_NOEXCEPT
00084       { return std::__addressof(__x); }
00085 
00086       // NB: __n is permitted to be 0.  The C++ standard says nothing
00087       // about what the return value is when __n == 0.
00088       pointer
00089       allocate(size_type __n, const void* = 0)
00090       { 
00091     if (__n > this->max_size())
00092       std::__throw_bad_alloc();
00093 
00094     return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
00095       }
00096 
00097       // __p is not permitted to be a null pointer.
00098       void
00099       deallocate(pointer __p, size_type)
00100       { ::operator delete(__p); }
00101 
00102       size_type
00103       max_size() const _GLIBCXX_USE_NOEXCEPT
00104       { return size_t(-1) / sizeof(_Tp); }
00105 
00106 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00107       template<typename _Up, typename... _Args>
00108         void
00109         construct(_Up* __p, _Args&&... __args)
00110     { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
00111 
00112       template<typename _Up>
00113         void 
00114         destroy(_Up* __p) { __p->~_Up(); }
00115 #else
00116       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00117       // 402. wrong new expression in [some_] allocator::construct
00118       void 
00119       construct(pointer __p, const _Tp& __val) 
00120       { ::new((void *)__p) _Tp(__val); }
00121 
00122       void 
00123       destroy(pointer __p) { __p->~_Tp(); }
00124 #endif
00125     };
00126 
00127   template<typename _Tp>
00128     inline bool
00129     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
00130     { return true; }
00131   
00132   template<typename _Tp>
00133     inline bool
00134     operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
00135     { return false; }
00136 
00137 _GLIBCXX_END_NAMESPACE_VERSION
00138 } // namespace
00139 
00140 #endif