libstdc++
|
00001 // Allocator that wraps operator new -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file ext/new_allocator.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _NEW_ALLOCATOR_H 00030 #define _NEW_ALLOCATOR_H 1 00031 00032 #include <bits/c++config.h> 00033 #include <new> 00034 #include <bits/functexcept.h> 00035 #include <bits/move.h> 00036 #if __cplusplus >= 201103L 00037 #include <type_traits> 00038 #endif 00039 00040 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00041 { 00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00043 00044 using std::size_t; 00045 using std::ptrdiff_t; 00046 00047 /** 00048 * @brief An allocator that uses global new, as per [20.4]. 00049 * @ingroup allocators 00050 * 00051 * This is precisely the allocator defined in the C++ Standard. 00052 * - all allocation calls operator new 00053 * - all deallocation calls operator delete 00054 * 00055 * @tparam _Tp Type of allocated object. 00056 */ 00057 template<typename _Tp> 00058 class new_allocator 00059 { 00060 public: 00061 typedef size_t size_type; 00062 typedef ptrdiff_t difference_type; 00063 typedef _Tp* pointer; 00064 typedef const _Tp* const_pointer; 00065 typedef _Tp& reference; 00066 typedef const _Tp& const_reference; 00067 typedef _Tp value_type; 00068 00069 template<typename _Tp1> 00070 struct rebind 00071 { typedef new_allocator<_Tp1> other; }; 00072 00073 #if __cplusplus >= 201103L 00074 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00075 // 2103. propagate_on_container_move_assignment 00076 typedef std::true_type propagate_on_container_move_assignment; 00077 #endif 00078 00079 new_allocator() _GLIBCXX_USE_NOEXCEPT { } 00080 00081 new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { } 00082 00083 template<typename _Tp1> 00084 new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { } 00085 00086 ~new_allocator() _GLIBCXX_USE_NOEXCEPT { } 00087 00088 pointer 00089 address(reference __x) const _GLIBCXX_NOEXCEPT 00090 { return std::__addressof(__x); } 00091 00092 const_pointer 00093 address(const_reference __x) const _GLIBCXX_NOEXCEPT 00094 { return std::__addressof(__x); } 00095 00096 // NB: __n is permitted to be 0. The C++ standard says nothing 00097 // about what the return value is when __n == 0. 00098 pointer 00099 allocate(size_type __n, const void* = 0) 00100 { 00101 if (__n > this->max_size()) 00102 std::__throw_bad_alloc(); 00103 00104 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); 00105 } 00106 00107 // __p is not permitted to be a null pointer. 00108 void 00109 deallocate(pointer __p, size_type) 00110 { ::operator delete(__p); } 00111 00112 size_type 00113 max_size() const _GLIBCXX_USE_NOEXCEPT 00114 { return size_t(-1) / sizeof(_Tp); } 00115 00116 #if __cplusplus >= 201103L 00117 template<typename _Up, typename... _Args> 00118 void 00119 construct(_Up* __p, _Args&&... __args) 00120 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00121 00122 template<typename _Up> 00123 void 00124 destroy(_Up* __p) { __p->~_Up(); } 00125 #else 00126 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00127 // 402. wrong new expression in [some_] allocator::construct 00128 void 00129 construct(pointer __p, const _Tp& __val) 00130 { ::new((void *)__p) _Tp(__val); } 00131 00132 void 00133 destroy(pointer __p) { __p->~_Tp(); } 00134 #endif 00135 }; 00136 00137 template<typename _Tp> 00138 inline bool 00139 operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 00140 { return true; } 00141 00142 template<typename _Tp> 00143 inline bool 00144 operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 00145 { return false; } 00146 00147 _GLIBCXX_END_NAMESPACE_VERSION 00148 } // namespace 00149 00150 #endif