# 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 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    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 /** @file ext/new_allocator.h
    00031  *  This file is a GNU extension to the Standard C++ Library.
    00032  */
    00033
    00034 #ifndef _NEW_ALLOCATOR_H
    00035 #define _NEW_ALLOCATOR_H 1
    00036
    00037 #include <new>
    00038 #include <bits/functexcept.h>
    00039 #include <bits/stl_move.h>
    00040
    00041 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00042
    00043   using std::size_t;
    00044   using std::ptrdiff_t;
    00045
    00046   /**
    00047    *  @brief  An allocator that uses global new, as per [20.4].
    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() throw() { }
    00070
    00071       new_allocator(const new_allocator&) throw() { }
    00072
    00073       template<typename _Tp1>
    00074         new_allocator(const new_allocator<_Tp1>&) throw() { }
    00075
    00076       ~new_allocator() throw() { }
    00077
    00078       pointer
    00079       address(reference __x) const { return &__x; }
    00080
    00081       const_pointer
    00082       address(const_reference __x) const { return &__x; }
    00083
    00084       // NB: __n is permitted to be 0.  The C++ standard says nothing
    00085       // about what the return value is when __n == 0.
    00086       pointer
    00087       allocate(size_type __n, const void* = 0)
    00088       {
    00089     if (__builtin_expect(__n > this->max_size(), false))
    00090       std::__throw_bad_alloc();
    00091
    00092     return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
    00093       }
    00094
    00095       // __p is not permitted to be a null pointer.
    00096       void
    00097       deallocate(pointer __p, size_type)
    00098       { ::operator delete(__p); }
    00099
    00100       size_type
    00101       max_size() const throw()
    00102       { return size_t(-1) / sizeof(_Tp); }
    00103
    00104       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00105       // 402. wrong new expression in [some_] allocator::construct
    00106       void
    00107       construct(pointer __p, const _Tp& __val)
    00108       { ::new((void *)__p) _Tp(__val); }
    00109
    00110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00111       template<typename... _Args>
    00112         void
    00113         construct(pointer __p, _Args&&... __args)
    00114     { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
    00115 #endif
    00116
    00117       void
    00118       destroy(pointer __p) { __p->~_Tp(); }
    00119     };
    00120
    00121   template<typename _Tp>
    00122     inline bool
    00123     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
    00124     { return true; }
    00125
    00126   template<typename _Tp>
    00127     inline bool
    00128     operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
    00129     { return false; }
    00130
    00131 _GLIBCXX_END_NAMESPACE
    00132
    00133 #endif