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

Generated on Tue Apr 21 13:13:29 2009 for libstdc++ by  doxygen 1.5.8