| 1 | // Allocator that wraps operator new -*- C++ -*- | 
|---|
| 2 |  | 
|---|
| 3 | // Copyright (C) 2001-2021 Free Software Foundation, Inc. | 
|---|
| 4 | // | 
|---|
| 5 | // This file is part of the GNU ISO C++ Library.  This library is free | 
|---|
| 6 | // software; you can redistribute it and/or modify it under the | 
|---|
| 7 | // terms of the GNU General Public License as published by the | 
|---|
| 8 | // Free Software Foundation; either version 3, or (at your option) | 
|---|
| 9 | // any later version. | 
|---|
| 10 |  | 
|---|
| 11 | // This library is distributed in the hope that it will be useful, | 
|---|
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | // GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | // Under Section 7 of GPL version 3, you are granted additional | 
|---|
| 17 | // permissions described in the GCC Runtime Library Exception, version | 
|---|
| 18 | // 3.1, as published by the Free Software Foundation. | 
|---|
| 19 |  | 
|---|
| 20 | // You should have received a copy of the GNU General Public License and | 
|---|
| 21 | // a copy of the GCC Runtime Library Exception along with this program; | 
|---|
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see | 
|---|
| 23 | // <http://www.gnu.org/licenses/>. | 
|---|
| 24 |  | 
|---|
| 25 | /** @file ext/new_allocator.h | 
|---|
| 26 | *  This file is a GNU extension to the Standard C++ Library. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #ifndef _NEW_ALLOCATOR_H | 
|---|
| 30 | #define _NEW_ALLOCATOR_H 1 | 
|---|
| 31 |  | 
|---|
| 32 | #include <bits/c++config.h> | 
|---|
| 33 | #include <new> | 
|---|
| 34 | #include <bits/functexcept.h> | 
|---|
| 35 | #include <bits/move.h> | 
|---|
| 36 | #if __cplusplus >= 201103L | 
|---|
| 37 | #include <type_traits> | 
|---|
| 38 | #endif | 
|---|
| 39 |  | 
|---|
| 40 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) | 
|---|
| 41 | { | 
|---|
| 42 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 | *  @brief  An allocator that uses global new, as per C++03 [20.4.1]. | 
|---|
| 46 | *  @ingroup allocators | 
|---|
| 47 | * | 
|---|
| 48 | *  This is precisely the allocator defined in the C++ Standard. | 
|---|
| 49 | *    - all allocation calls operator new | 
|---|
| 50 | *    - all deallocation calls operator delete | 
|---|
| 51 | * | 
|---|
| 52 | *  @tparam  _Tp  Type of allocated object. | 
|---|
| 53 | */ | 
|---|
| 54 | template<typename _Tp> | 
|---|
| 55 | class new_allocator | 
|---|
| 56 | { | 
|---|
| 57 | public: | 
|---|
| 58 | typedef _Tp        value_type; | 
|---|
| 59 | typedef std::size_t     size_type; | 
|---|
| 60 | typedef std::ptrdiff_t  difference_type; | 
|---|
| 61 | #if __cplusplus <= 201703L | 
|---|
| 62 | typedef _Tp*       pointer; | 
|---|
| 63 | typedef const _Tp* const_pointer; | 
|---|
| 64 | typedef _Tp&       reference; | 
|---|
| 65 | typedef const _Tp& const_reference; | 
|---|
| 66 |  | 
|---|
| 67 | template<typename _Tp1> | 
|---|
| 68 | struct rebind | 
|---|
| 69 | { typedef new_allocator<_Tp1> other; }; | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | #if __cplusplus >= 201103L | 
|---|
| 73 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | 
|---|
| 74 | // 2103. propagate_on_container_move_assignment | 
|---|
| 75 | typedef std::true_type propagate_on_container_move_assignment; | 
|---|
| 76 | #endif | 
|---|
| 77 |  | 
|---|
| 78 | _GLIBCXX20_CONSTEXPR | 
|---|
| 79 | new_allocator() _GLIBCXX_USE_NOEXCEPT { } | 
|---|
| 80 |  | 
|---|
| 81 | _GLIBCXX20_CONSTEXPR | 
|---|
| 82 | new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { } | 
|---|
| 83 |  | 
|---|
| 84 | template<typename _Tp1> | 
|---|
| 85 | _GLIBCXX20_CONSTEXPR | 
|---|
| 86 | new_allocator(const new_allocator<_Tp1>&) _GLIBCXX_USE_NOEXCEPT { } | 
|---|
| 87 |  | 
|---|
| 88 | #if __cplusplus <= 201703L | 
|---|
| 89 | ~new_allocator() _GLIBCXX_USE_NOEXCEPT { } | 
|---|
| 90 |  | 
|---|
| 91 | pointer | 
|---|
| 92 | address(reference __x) const _GLIBCXX_NOEXCEPT | 
|---|
| 93 | { return std::__addressof(__x); } | 
|---|
| 94 |  | 
|---|
| 95 | const_pointer | 
|---|
| 96 | address(const_reference __x) const _GLIBCXX_NOEXCEPT | 
|---|
| 97 | { return std::__addressof(__x); } | 
|---|
| 98 | #endif | 
|---|
| 99 |  | 
|---|
| 100 | // NB: __n is permitted to be 0.  The C++ standard says nothing | 
|---|
| 101 | // about what the return value is when __n == 0. | 
|---|
| 102 | _GLIBCXX_NODISCARD _Tp* | 
|---|
| 103 | allocate(size_type __n, const void* = static_cast<const void*>(0)) | 
|---|
| 104 | { | 
|---|
| 105 | #if __cplusplus >= 201103L | 
|---|
| 106 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | 
|---|
| 107 | // 3308. std::allocator<void>().allocate(n) | 
|---|
| 108 | static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types"); | 
|---|
| 109 | #endif | 
|---|
| 110 |  | 
|---|
| 111 | if (__builtin_expect(__n > this->_M_max_size(), false)) | 
|---|
| 112 | { | 
|---|
| 113 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | 
|---|
| 114 | // 3190. allocator::allocate sometimes returns too little storage | 
|---|
| 115 | if (__n > (std::size_t(-1) / sizeof(_Tp))) | 
|---|
| 116 | std::__throw_bad_array_new_length(); | 
|---|
| 117 | std::__throw_bad_alloc(); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | #if __cpp_aligned_new | 
|---|
| 121 | if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) | 
|---|
| 122 | { | 
|---|
| 123 | std::align_val_t __al = std::align_val_t(alignof(_Tp)); | 
|---|
| 124 | return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), __al)); | 
|---|
| 125 | } | 
|---|
| 126 | #endif | 
|---|
| 127 | return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | // __p is not permitted to be a null pointer. | 
|---|
| 131 | void | 
|---|
| 132 | deallocate(_Tp* __p, size_type __t __attribute__ ((__unused__))) | 
|---|
| 133 | { | 
|---|
| 134 | #if __cpp_aligned_new | 
|---|
| 135 | if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__) | 
|---|
| 136 | { | 
|---|
| 137 | ::operator delete(__p, | 
|---|
| 138 | # if __cpp_sized_deallocation | 
|---|
| 139 | __t * sizeof(_Tp), | 
|---|
| 140 | # endif | 
|---|
| 141 | std::align_val_t(alignof(_Tp))); | 
|---|
| 142 | return; | 
|---|
| 143 | } | 
|---|
| 144 | #endif | 
|---|
| 145 | ::operator delete(__p | 
|---|
| 146 | #if __cpp_sized_deallocation | 
|---|
| 147 | , __t * sizeof(_Tp) | 
|---|
| 148 | #endif | 
|---|
| 149 | ); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | #if __cplusplus <= 201703L | 
|---|
| 153 | size_type | 
|---|
| 154 | max_size() const _GLIBCXX_USE_NOEXCEPT | 
|---|
| 155 | { return _M_max_size(); } | 
|---|
| 156 |  | 
|---|
| 157 | #if __cplusplus >= 201103L | 
|---|
| 158 | template<typename _Up, typename... _Args> | 
|---|
| 159 | void | 
|---|
| 160 | construct(_Up* __p, _Args&&... __args) | 
|---|
| 161 | noexcept(std::is_nothrow_constructible<_Up, _Args...>::value) | 
|---|
| 162 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | 
|---|
| 163 |  | 
|---|
| 164 | template<typename _Up> | 
|---|
| 165 | void | 
|---|
| 166 | destroy(_Up* __p) | 
|---|
| 167 | noexcept(std::is_nothrow_destructible<_Up>::value) | 
|---|
| 168 | { __p->~_Up(); } | 
|---|
| 169 | #else | 
|---|
| 170 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | 
|---|
| 171 | // 402. wrong new expression in [some_] allocator::construct | 
|---|
| 172 | void | 
|---|
| 173 | construct(pointer __p, const _Tp& __val) | 
|---|
| 174 | { ::new((void *)__p) _Tp(__val); } | 
|---|
| 175 |  | 
|---|
| 176 | void | 
|---|
| 177 | destroy(pointer __p) { __p->~_Tp(); } | 
|---|
| 178 | #endif | 
|---|
| 179 | #endif // ! C++20 | 
|---|
| 180 |  | 
|---|
| 181 | template<typename _Up> | 
|---|
| 182 | friend _GLIBCXX20_CONSTEXPR bool | 
|---|
| 183 | operator==(const new_allocator&, const new_allocator<_Up>&) | 
|---|
| 184 | _GLIBCXX_NOTHROW | 
|---|
| 185 | { return true; } | 
|---|
| 186 |  | 
|---|
| 187 | #if __cpp_impl_three_way_comparison < 201907L | 
|---|
| 188 | template<typename _Up> | 
|---|
| 189 | friend _GLIBCXX20_CONSTEXPR bool | 
|---|
| 190 | operator!=(const new_allocator&, const new_allocator<_Up>&) | 
|---|
| 191 | _GLIBCXX_NOTHROW | 
|---|
| 192 | { return false; } | 
|---|
| 193 | #endif | 
|---|
| 194 |  | 
|---|
| 195 | private: | 
|---|
| 196 | _GLIBCXX_CONSTEXPR size_type | 
|---|
| 197 | _M_max_size() const _GLIBCXX_USE_NOEXCEPT | 
|---|
| 198 | { | 
|---|
| 199 | #if __PTRDIFF_MAX__ < __SIZE_MAX__ | 
|---|
| 200 | return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp); | 
|---|
| 201 | #else | 
|---|
| 202 | return std::size_t(-1) / sizeof(_Tp); | 
|---|
| 203 | #endif | 
|---|
| 204 | } | 
|---|
| 205 | }; | 
|---|
| 206 |  | 
|---|
| 207 | _GLIBCXX_END_NAMESPACE_VERSION | 
|---|
| 208 | } // namespace | 
|---|
| 209 |  | 
|---|
| 210 | #endif | 
|---|
| 211 |  | 
|---|