| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___VECTOR_LAYOUT_H |
| 10 | #define _LIBCPP___VECTOR_LAYOUT_H |
| 11 | |
| 12 | #include <__assert> |
| 13 | #include <__config> |
| 14 | #include <__debug_utils/sanitizers.h> |
| 15 | #include <__memory/allocator_traits.h> |
| 16 | #include <__memory/compressed_pair.h> |
| 17 | #include <__memory/pointer_traits.h> |
| 18 | #include <__memory/swap_allocator.h> |
| 19 | #include <__memory/uninitialized_algorithms.h> |
| 20 | #include <__split_buffer> |
| 21 | #include <__type_traits/is_nothrow_constructible.h> |
| 22 | #include <__utility/exchange.h> |
| 23 | #include <__utility/move.h> |
| 24 | #include <__utility/swap.h> |
| 25 | |
| 26 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 27 | # pragma GCC system_header |
| 28 | #endif |
| 29 | |
| 30 | _LIBCPP_PUSH_MACROS |
| 31 | #include <__undef_macros> |
| 32 | |
| 33 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 34 | |
| 35 | /// Defines `std::vector`'s storage layout and any operations that are affected by a change in the |
| 36 | /// layout. |
| 37 | /// |
| 38 | /// `std::vector` can be represented in a variety of ways. Each representation strongly influences |
| 39 | /// the codegen when calling vector operations, which can significantly impact runtime performance |
| 40 | /// and memory utilisation. libc++ provides two alternative layouts for `std::vector`, although only |
| 41 | /// one can be active for an entire binary: |
| 42 | /// |
| 43 | /// * pointer-based layout (stable ABI default) |
| 44 | /// * size-based layout (unstable ABI alternative) |
| 45 | // |
| 46 | /// We describe these layouts below. All vector representations have a pointer that points to where |
| 47 | /// the memory is allocated (called `__begin_`). |
| 48 | /// |
| 49 | /// **Pointer-based layout** |
| 50 | /// |
| 51 | /// The pointer-based layout uses two more pointers in addition to `__begin_`. The second pointer |
| 52 | /// (called `__end_`) points past the end of the part of the buffer that holds valid elements. |
| 53 | /// Another pointer (called `__capacity_`) points past the end of the allocated buffer. The original |
| 54 | /// libc++ `std::vector` implementation only provided the pointer-based layout. libc++ continues to |
| 55 | /// use the pointer-based layout, by default, in order to maintain binary compatibility with |
| 56 | /// existing software. |
| 57 | /// |
| 58 | /// The `__end_` pointer has three primary use-cases: |
| 59 | /// * to compute the size of the vector; and |
| 60 | /// * to construct the past-the-end iterator; and |
| 61 | /// * to indicate where the next element should be appended. |
| 62 | /// |
| 63 | /// The `__capacity_` is used to compute the capacity of the vector, which lets the vector know how |
| 64 | /// many elements can be added to the vector before a reallocation is necessary. |
| 65 | /// |
| 66 | /// __begin_ = 0xE4FD0, __end_ = 0xE4FF0, __capacity_ = 0xE5000 |
| 67 | /// 0xE4FD0 0xE4FF0 0xE5000 |
| 68 | /// v v v |
| 69 | /// +---------------+--------+--------+--------+--------+--------+--------+---------------------+ |
| 70 | /// | ????????????? | 3174 | 5656 | 648 | 489 | ------ | ------ | ??????????????????? | |
| 71 | /// +---------------+--------+--------+--------+--------+--------+--------+---------------------+ |
| 72 | /// ^ ^ ^ |
| 73 | /// __begin_ __end_ __capacity_ |
| 74 | /// |
| 75 | /// Figure 1: A visual representation of a pointer-based `std::vector<short>`. This vector has |
| 76 | /// four elements, with the capacity to store six. Boxes with numbers are valid elements within |
| 77 | /// the vector, and boxes with `xx` have been allocated, but aren't being used as elements right |
| 78 | /// now. |
| 79 | /// |
| 80 | /// This is the default layout for libc++. |
| 81 | /// |
| 82 | /// **Size-based layout** |
| 83 | /// |
| 84 | /// The size-based layout uses integers to track its size and capacity, and computes pointers to |
| 85 | /// past-the-end of the valid range and the whole buffer only when it's necessary. Programs using |
| 86 | /// the size-based layout have been measured to yield improved compute and memory performance over |
| 87 | /// the pointer-based layout. Despite these promising measurements, the size-based layout is opt-in, |
| 88 | /// to preserve ABI compatibility with prebuilt binaries. Given the improved performance, we |
| 89 | /// recommend preferring the size-based layout in the absence of such ABI constraints. |
| 90 | /// |
| 91 | /// __begin_ = 0xE4FD0, __size_ = 4, __capacity_ = 6 |
| 92 | /// 0xE4FD0 |
| 93 | /// v |
| 94 | /// +---------------+--------+--------+--------+--------+--------+--------+---------------------+ |
| 95 | /// | ????????????? | 3174 | 5656 | 648 | 489 | ------ | ------ | ??????????????????? | |
| 96 | /// +---------------+--------+--------+--------+--------+--------+--------+---------------------+ |
| 97 | /// ^ |
| 98 | /// __begin_ |
| 99 | /// |
| 100 | /// Figure 2: A visual representation of this a size-based layout. Blank boxes are not a part |
| 101 | /// of the vector's allocated buffer. |
| 102 | /// |
| 103 | /// **Class design** |
| 104 | /// |
| 105 | /// __vector_layout was designed with the following goals: |
| 106 | /// 1. to abstractly represent the buffer's boundaries; and |
| 107 | /// 2. to limit the number of `#ifdef` blocks that a reader needs to pass through; and |
| 108 | /// 3. given (1) and (2), to have no logically identical components in multiple `#ifdef` clauses. |
| 109 | /// |
| 110 | /// To facilitate these goals, there is a single `__vector_layout` definition. Users must choose |
| 111 | /// their vector's layout when libc++ is being configured, so there is no need to manage multiple |
| 112 | /// vector layout types (e.g. `__vector_size_layout`, `__vector_pointer_layout`, etc.). In doing so, |
| 113 | /// we reduce a significant portion of duplicate code. |
| 114 | template <class _Tp, class _Allocator> |
| 115 | class __vector_layout { |
| 116 | public: |
| 117 | using value_type _LIBCPP_NODEBUG = _Tp; |
| 118 | using allocator_type _LIBCPP_NODEBUG = _Allocator; |
| 119 | using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>; |
| 120 | using size_type _LIBCPP_NODEBUG = typename __alloc_traits::size_type; |
| 121 | using pointer _LIBCPP_NODEBUG = typename __alloc_traits::pointer; |
| 122 | using const_pointer _LIBCPP_NODEBUG = typename __alloc_traits::const_pointer; |
| 123 | #ifdef _LIBCPP_ABI_VECTOR_LAYOUT_SIZE_BASED |
| 124 | using _SplitBuffer _LIBCPP_NODEBUG = __split_buffer<_Tp, _Allocator, __split_buffer_size_layout>; |
| 125 | using __bound_type _LIBCPP_NODEBUG = size_type; |
| 126 | #else |
| 127 | using _SplitBuffer _LIBCPP_NODEBUG = __split_buffer<_Tp, _Allocator, __split_buffer_pointer_layout>; |
| 128 | using __bound_type _LIBCPP_NODEBUG = pointer; |
| 129 | #endif |
| 130 | |
| 131 | // Cannot be defaulted, since `_LIBCPP_COMPRESSED_PAIR` isn't an aggregate before C++14. |
| 132 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __vector_layout() |
| 133 | _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) |
| 134 | : __capacity_() {} |
| 135 | |
| 136 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __vector_layout(allocator_type const& __a) |
| 137 | _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value) |
| 138 | : __capacity_(), __alloc_(__a) {} |
| 139 | |
| 140 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __vector_layout(allocator_type&& __a) |
| 141 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) |
| 142 | : __capacity_(), __alloc_(std::move(__a)) {} |
| 143 | |
| 144 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __vector_layout(__vector_layout&& __other) |
| 145 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); |
| 146 | |
| 147 | /// Returns a reference to the stored allocator. |
| 148 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { |
| 149 | return __alloc_; |
| 150 | } |
| 151 | |
| 152 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type const& |
| 153 | __alloc() const _NOEXCEPT { |
| 154 | return __alloc_; |
| 155 | } |
| 156 | |
| 157 | /// Returns a pointer to the beginning of the buffer. |
| 158 | /// |
| 159 | /// `__begin_ptr()` is not called `data()` because `vector::data()` returns `T*`, but `__begin_` |
| 160 | /// is allowed to be a fancy pointer. |
| 161 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __begin_ptr() _NOEXCEPT { |
| 162 | return __begin_; |
| 163 | } |
| 164 | |
| 165 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer __begin_ptr() const _NOEXCEPT { |
| 166 | return __begin_; |
| 167 | } |
| 168 | |
| 169 | /// Returns a built-in pointer to the beginning of the buffer. |
| 170 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _Tp* __data() _NOEXCEPT { |
| 171 | return std::__to_address(__begin_); |
| 172 | } |
| 173 | |
| 174 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI _Tp const* __data() const _NOEXCEPT { |
| 175 | return std::__to_address(__begin_); |
| 176 | } |
| 177 | |
| 178 | /// Returns how many elements can be added before a reallocation occurs. |
| 179 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type |
| 180 | __remaining_capacity() const _NOEXCEPT; |
| 181 | |
| 182 | /// Sets the member pointing to the first element in the vector to `__new_begin`, the member used |
| 183 | /// to obtain the vector's bound to the equivalent of `__new_size`, and the member that represents |
| 184 | /// the vector's capacity to the equivalent to `__new_capacity`. |
| 185 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void |
| 186 | __set_layout(pointer __new_begin, size_type __new_size, size_type __new_capacity) _NOEXCEPT; |
| 187 | |
| 188 | /// Sets the member used to obtain the vector's bound to the equivalent of `__ptr`. |
| 189 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_bound_using_pointer(pointer __ptr) _NOEXCEPT; |
| 190 | |
| 191 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __reset_without_allocator() _NOEXCEPT; |
| 192 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __swap(__vector_layout& __other) _NOEXCEPT; |
| 193 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void |
| 194 | __move_assign_without_allocator(__vector_layout& __other) _NOEXCEPT; |
| 195 | |
| 196 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __relocate(_SplitBuffer& __buffer); |
| 197 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer |
| 198 | __relocate_with_pivot(_SplitBuffer& __buffer, pointer __pivot); |
| 199 | |
| 200 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __size() const _NOEXCEPT; |
| 201 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __capacity() const _NOEXCEPT; |
| 202 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __empty() const _NOEXCEPT; |
| 203 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __end_ptr() _NOEXCEPT; |
| 204 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer __end_ptr() const _NOEXCEPT; |
| 205 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __capacity_ptr() _NOEXCEPT; |
| 206 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer __capacity_ptr() const _NOEXCEPT; |
| 207 | [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const _NOEXCEPT; |
| 208 | |
| 209 | private: |
| 210 | pointer __begin_ = nullptr; |
| 211 | |
| 212 | #ifdef _LIBCPP_ABI_VECTOR_LAYOUT_SIZE_BASED |
| 213 | size_type __size_ = 0; |
| 214 | size_type __capacity_ = 0; |
| 215 | _LIBCPP_NO_UNIQUE_ADDRESS allocator_type __alloc_; |
| 216 | #else |
| 217 | pointer __end_ = nullptr; |
| 218 | _LIBCPP_COMPRESSED_PAIR(pointer, __capacity_ = nullptr, allocator_type, __alloc_); |
| 219 | #endif |
| 220 | |
| 221 | _LIBCPP_CONSTEXPR_SINCE_CXX20 |
| 222 | _LIBCPP_HIDE_FROM_ABI void __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const { |
| 223 | std::__annotate_contiguous_container<_Allocator>(__data(), __data() + __capacity(), __old_mid, __new_mid); |
| 224 | } |
| 225 | |
| 226 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT { |
| 227 | __annotate_contiguous_container(old_mid: __data() + __capacity(), new_mid: __data() + __current_size); |
| 228 | } |
| 229 | |
| 230 | _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT { |
| 231 | __annotate_contiguous_container(old_mid: __data() + __size(), new_mid: __data() + __capacity()); |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | #ifdef _LIBCPP_ABI_VECTOR_LAYOUT_SIZE_BASED |
| 236 | template <class _Tp, class _Alloc> |
| 237 | _LIBCPP_CONSTEXPR_SINCE_CXX20 __vector_layout<_Tp, _Alloc>::__vector_layout(__vector_layout&& __other) |
| 238 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) |
| 239 | : __begin_(std::__exchange(__other.__begin_, nullptr)), |
| 240 | __size_(std::__exchange(__other.__size_, 0)), |
| 241 | __capacity_(std::__exchange(__other.__capacity_, 0)), |
| 242 | __alloc_(std::move(__other.__alloc_)) {} |
| 243 | |
| 244 | template <class _Tp, class _Alloc> |
| 245 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::size_type |
| 246 | __vector_layout<_Tp, _Alloc>::__remaining_capacity() const _NOEXCEPT { |
| 247 | return __capacity_ - __size_; |
| 248 | } |
| 249 | |
| 250 | template <class _Tp, class _Alloc> |
| 251 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__set_layout( |
| 252 | pointer __new_begin, size_type __new_size, size_type __new_capacity) _NOEXCEPT { |
| 253 | __begin_ = __new_begin; |
| 254 | __size_ = __new_size; |
| 255 | __capacity_ = __new_capacity; |
| 256 | } |
| 257 | |
| 258 | template <class _Tp, class _Alloc> |
| 259 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__set_bound_using_pointer(pointer __ptr) _NOEXCEPT { |
| 260 | __size_ = static_cast<size_type>(__ptr - __begin_); |
| 261 | } |
| 262 | |
| 263 | template <class _Tp, class _Alloc> |
| 264 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__reset_without_allocator() _NOEXCEPT { |
| 265 | __begin_ = nullptr; |
| 266 | __size_ = 0; |
| 267 | __capacity_ = 0; |
| 268 | } |
| 269 | |
| 270 | template <class _Tp, class _Alloc> |
| 271 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__swap(__vector_layout& __other) _NOEXCEPT { |
| 272 | using std::swap; |
| 273 | swap(__begin_, __other.__begin_); |
| 274 | swap(__size_, __other.__size_); |
| 275 | swap(__capacity_, __other.__capacity_); |
| 276 | std::__swap_allocator(__alloc_, __other.__alloc_); |
| 277 | } |
| 278 | |
| 279 | template <class _Tp, class _Alloc> |
| 280 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 281 | __vector_layout<_Tp, _Alloc>::__move_assign_without_allocator(__vector_layout& __other) _NOEXCEPT { |
| 282 | __begin_ = __other.__begin_; |
| 283 | __size_ = __other.__size_; |
| 284 | __capacity_ = __other.__capacity_; |
| 285 | |
| 286 | __other.__reset_without_allocator(); |
| 287 | } |
| 288 | |
| 289 | template <class _Tp, class _Alloc> |
| 290 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__relocate(_SplitBuffer& __buffer) { |
| 291 | __annotate_delete(); |
| 292 | __buffer.__relocate(__begin_, __size_, __capacity_); |
| 293 | __annotate_new(__size_); |
| 294 | } |
| 295 | |
| 296 | template <class _Tp, class _Alloc> |
| 297 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::pointer |
| 298 | __vector_layout<_Tp, _Alloc>::__relocate_with_pivot(_SplitBuffer& __buffer, pointer __pivot) { |
| 299 | __annotate_delete(); |
| 300 | auto __result = __buffer.__relocate_with_pivot(__pivot, __begin_, __size_, __capacity_); |
| 301 | __annotate_new(__size_); |
| 302 | return __result; |
| 303 | } |
| 304 | |
| 305 | template <class _Tp, class _Alloc> |
| 306 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::size_type |
| 307 | __vector_layout<_Tp, _Alloc>::__size() const _NOEXCEPT { |
| 308 | return __size_; |
| 309 | } |
| 310 | |
| 311 | template <class _Tp, class _Alloc> |
| 312 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::size_type |
| 313 | __vector_layout<_Tp, _Alloc>::__capacity() const _NOEXCEPT { |
| 314 | return __capacity_; |
| 315 | } |
| 316 | |
| 317 | template <class _Tp, class _Alloc> |
| 318 | _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __vector_layout<_Tp, _Alloc>::__empty() const _NOEXCEPT { |
| 319 | return __size_ == 0; |
| 320 | } |
| 321 | |
| 322 | template <class _Tp, class _Alloc> |
| 323 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::pointer |
| 324 | __vector_layout<_Tp, _Alloc>::__end_ptr() _NOEXCEPT { |
| 325 | return __begin_ + __size_; |
| 326 | } |
| 327 | |
| 328 | template <class _Tp, class _Alloc> |
| 329 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::const_pointer |
| 330 | __vector_layout<_Tp, _Alloc>::__end_ptr() const _NOEXCEPT { |
| 331 | return __begin_ + __size_; |
| 332 | } |
| 333 | |
| 334 | template <class _Tp, class _Alloc> |
| 335 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::pointer |
| 336 | __vector_layout<_Tp, _Alloc>::__capacity_ptr() _NOEXCEPT { |
| 337 | return __begin_ + __capacity_; |
| 338 | } |
| 339 | |
| 340 | template <class _Tp, class _Alloc> |
| 341 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::const_pointer |
| 342 | __vector_layout<_Tp, _Alloc>::__capacity_ptr() const _NOEXCEPT { |
| 343 | return __begin_ + __capacity_; |
| 344 | } |
| 345 | |
| 346 | template <class _Tp, class _Alloc> |
| 347 | _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __vector_layout<_Tp, _Alloc>::__invariants() const _NOEXCEPT { |
| 348 | if (__begin_ == nullptr) |
| 349 | return __size_ == 0 && __capacity_ == 0; |
| 350 | return __size_ <= __capacity_; |
| 351 | } |
| 352 | #else // !defined(_LIBCPP_ABI_VECTOR_LAYOUT_SIZE_BASED) |
| 353 | template <class _Tp, class _Alloc> |
| 354 | _LIBCPP_CONSTEXPR_SINCE_CXX20 __vector_layout<_Tp, _Alloc>::__vector_layout(__vector_layout&& __other) |
| 355 | _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) |
| 356 | : __begin_(std::__exchange(__other.__begin_, nullptr)), |
| 357 | __end_(std::__exchange(__other.__end_, nullptr)), |
| 358 | __capacity_(std::__exchange(__other.__capacity_, nullptr)), |
| 359 | __alloc_(std::move(__other.__alloc_)) {} |
| 360 | |
| 361 | template <class _Tp, class _Alloc> |
| 362 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::size_type |
| 363 | __vector_layout<_Tp, _Alloc>::__remaining_capacity() const _NOEXCEPT { |
| 364 | return __capacity_ - __end_; |
| 365 | } |
| 366 | |
| 367 | template <class _Tp, class _Alloc> |
| 368 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__set_layout( |
| 369 | pointer __new_begin, size_type __new_size, size_type __new_capacity) _NOEXCEPT { |
| 370 | __begin_ = __new_begin; |
| 371 | __end_ = __new_begin + __new_size; |
| 372 | __capacity_ = __new_begin + __new_capacity; |
| 373 | } |
| 374 | |
| 375 | template <class _Tp, class _Alloc> |
| 376 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__set_bound_using_pointer(pointer __ptr) _NOEXCEPT { |
| 377 | __end_ = __ptr; |
| 378 | } |
| 379 | |
| 380 | template <class _Tp, class _Alloc> |
| 381 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__reset_without_allocator() _NOEXCEPT { |
| 382 | __begin_ = nullptr; |
| 383 | __end_ = nullptr; |
| 384 | __capacity_ = nullptr; |
| 385 | } |
| 386 | |
| 387 | template <class _Tp, class _Alloc> |
| 388 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__swap(__vector_layout& __other) _NOEXCEPT { |
| 389 | using std::swap; |
| 390 | swap(__begin_, __other.__begin_); |
| 391 | swap(__end_, __other.__end_); |
| 392 | swap(__capacity_, __other.__capacity_); |
| 393 | std::__swap_allocator(__alloc_, __other.__alloc_); |
| 394 | } |
| 395 | |
| 396 | template <class _Tp, class _Alloc> |
| 397 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void |
| 398 | __vector_layout<_Tp, _Alloc>::__move_assign_without_allocator(__vector_layout& __other) _NOEXCEPT { |
| 399 | __begin_ = __other.__begin_; |
| 400 | __end_ = __other.__end_; |
| 401 | __capacity_ = __other.__capacity_; |
| 402 | |
| 403 | __other.__reset_without_allocator(); |
| 404 | } |
| 405 | |
| 406 | template <class _Tp, class _Alloc> |
| 407 | _LIBCPP_CONSTEXPR_SINCE_CXX20 void __vector_layout<_Tp, _Alloc>::__relocate(_SplitBuffer& __buffer) { |
| 408 | __annotate_delete(); |
| 409 | __buffer.__relocate(__begin_, __end_, __capacity_); |
| 410 | __annotate_new(current_size: __size()); |
| 411 | } |
| 412 | |
| 413 | template <class _Tp, class _Alloc> |
| 414 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::pointer |
| 415 | __vector_layout<_Tp, _Alloc>::__relocate_with_pivot(_SplitBuffer& __buffer, pointer __pivot) { |
| 416 | __annotate_delete(); |
| 417 | auto __result = __buffer.__relocate_with_pivot(__pivot, __begin_, __end_, __capacity_); |
| 418 | __annotate_new(current_size: __size()); |
| 419 | return __result; |
| 420 | } |
| 421 | |
| 422 | template <class _Tp, class _Alloc> |
| 423 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::size_type |
| 424 | __vector_layout<_Tp, _Alloc>::__size() const _NOEXCEPT { |
| 425 | return static_cast<size_type>(__end_ - __begin_); |
| 426 | } |
| 427 | |
| 428 | template <class _Tp, class _Alloc> |
| 429 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::size_type |
| 430 | __vector_layout<_Tp, _Alloc>::__capacity() const _NOEXCEPT { |
| 431 | return static_cast<size_type>(__capacity_ - __begin_); |
| 432 | } |
| 433 | |
| 434 | template <class _Tp, class _Alloc> |
| 435 | _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __vector_layout<_Tp, _Alloc>::__empty() const _NOEXCEPT { |
| 436 | return __begin_ == __end_; |
| 437 | } |
| 438 | |
| 439 | template <class _Tp, class _Alloc> |
| 440 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::pointer |
| 441 | __vector_layout<_Tp, _Alloc>::__end_ptr() _NOEXCEPT { |
| 442 | return __end_; |
| 443 | } |
| 444 | |
| 445 | template <class _Tp, class _Alloc> |
| 446 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::const_pointer |
| 447 | __vector_layout<_Tp, _Alloc>::__end_ptr() const _NOEXCEPT { |
| 448 | return __end_; |
| 449 | } |
| 450 | |
| 451 | template <class _Tp, class _Alloc> |
| 452 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::pointer |
| 453 | __vector_layout<_Tp, _Alloc>::__capacity_ptr() _NOEXCEPT { |
| 454 | return __capacity_; |
| 455 | } |
| 456 | |
| 457 | template <class _Tp, class _Alloc> |
| 458 | _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __vector_layout<_Tp, _Alloc>::const_pointer |
| 459 | __vector_layout<_Tp, _Alloc>::__capacity_ptr() const _NOEXCEPT { |
| 460 | return __capacity_; |
| 461 | } |
| 462 | |
| 463 | template <class _Tp, class _Alloc> |
| 464 | _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __vector_layout<_Tp, _Alloc>::__invariants() const _NOEXCEPT { |
| 465 | if (__begin_ == nullptr) |
| 466 | return __end_ == nullptr && __capacity_ == nullptr; |
| 467 | if (__begin_ > __end_) |
| 468 | return false; |
| 469 | if (__begin_ == __capacity_) |
| 470 | return false; |
| 471 | return __end_ <= __capacity_; |
| 472 | } |
| 473 | #endif // _LIBCPP_ABI_VECTOR_LAYOUT_SIZE_BASED |
| 474 | |
| 475 | _LIBCPP_END_NAMESPACE_STD |
| 476 | |
| 477 | _LIBCPP_POP_MACROS |
| 478 | |
| 479 | #endif // _LIBCPP___VECTOR_LAYOUT_H |
| 480 | |