libstdc++
|
00001 // Allocator that wraps operator new -*- C++ -*- 00002 00003 // Copyright (C) 2001-2019 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 _GLIBCXX20_CONSTEXPR 00080 new_allocator() _GLIBCXX_USE_NOEXCEPT { } 00081 00082 _GLIBCXX20_CONSTEXPR 00083 new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { } 00084 00085 template<typename _Tp1> 00086 _GLIBCXX20_CONSTEXPR 00087 new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { } 00088 00089 ~new_allocator() _GLIBCXX_USE_NOEXCEPT { } 00090 00091 pointer 00092 address(reference __x) const _GLIBCXX_NOEXCEPT 00093 { return std::__addressof(__x); } 00094 00095 const_pointer 00096 address(const_reference __x) const _GLIBCXX_NOEXCEPT 00097 { return std::__addressof(__x); } 00098 00099 // NB: __n is permitted to be 0. The C++ standard says nothing 00100 // about what the return value is when __n == 0. 00101 _GLIBCXX_NODISCARD pointer 00102 allocate(size_type __n, const void* = static_cast<const void*>(0)) 00103 { 00104 if (__n > this->max_size()) 00105 std::__throw_bad_alloc(); 00106 00107 #if __cpp_aligned_new 00108 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) 00109 { 00110 std::align_val_t __al = std::align_val_t(alignof(_Tp)); 00111 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al)); 00112 } 00113 #endif 00114 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); 00115 } 00116 00117 // __p is not permitted to be a null pointer. 00118 void 00119 deallocate(pointer __p, size_type) 00120 { 00121 #if __cpp_aligned_new 00122 if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) 00123 { 00124 ::operator delete(__p, std::align_val_t(alignof(_Tp))); 00125 return; 00126 } 00127 #endif 00128 ::operator delete(__p); 00129 } 00130 00131 size_type 00132 max_size() const _GLIBCXX_USE_NOEXCEPT 00133 { 00134 #if __PTRDIFF_MAX__ < __SIZE_MAX__ 00135 return size_t(__PTRDIFF_MAX__) / sizeof(_Tp); 00136 #else 00137 return size_t(-1) / sizeof(_Tp); 00138 #endif 00139 } 00140 00141 #if __cplusplus >= 201103L 00142 template<typename _Up, typename... _Args> 00143 void 00144 construct(_Up* __p, _Args&&... __args) 00145 noexcept(noexcept(::new((void *)__p) 00146 _Up(std::forward<_Args>(__args)...))) 00147 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } 00148 00149 template<typename _Up> 00150 void 00151 destroy(_Up* __p) 00152 noexcept(noexcept( __p->~_Up())) 00153 { __p->~_Up(); } 00154 #else 00155 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00156 // 402. wrong new expression in [some_] allocator::construct 00157 void 00158 construct(pointer __p, const _Tp& __val) 00159 { ::new((void *)__p) _Tp(__val); } 00160 00161 void 00162 destroy(pointer __p) { __p->~_Tp(); } 00163 #endif 00164 00165 template<typename _Up> 00166 friend bool 00167 operator==(const new_allocator&, const new_allocator<_Up>&) 00168 _GLIBCXX_NOTHROW 00169 { return true; } 00170 00171 template<typename _Up> 00172 friend bool 00173 operator!=(const new_allocator&, const new_allocator<_Up>&) 00174 _GLIBCXX_NOTHROW 00175 { return false; } 00176 }; 00177 00178 _GLIBCXX_END_NAMESPACE_VERSION 00179 } // namespace 00180 00181 #endif