aboutsummaryrefslogtreecommitdiff
path: root/libcxx/include/__memory/uninitialized_algorithms.h
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/include/__memory/uninitialized_algorithms.h')
-rw-r--r--libcxx/include/__memory/uninitialized_algorithms.h153
1 files changed, 150 insertions, 3 deletions
diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h
index 40e7c79a51e0..3a8560f080c6 100644
--- a/libcxx/include/__memory/uninitialized_algorithms.h
+++ b/libcxx/include/__memory/uninitialized_algorithms.h
@@ -11,14 +11,18 @@
#define _LIBCPP___MEMORY_UNINITIALIZED_ALGORITHMS_H
#include <__config>
+#include <__iterator/iterator_traits.h>
#include <__memory/addressof.h>
+#include <__memory/allocator_traits.h>
#include <__memory/construct_at.h>
#include <__memory/voidify.h>
-#include <iterator>
-#include <utility>
+#include <__utility/move.h>
+#include <__utility/pair.h>
+#include <__utility/transaction.h>
+#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-#pragma GCC system_header
+# pragma GCC system_header
#endif
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -343,6 +347,149 @@ uninitialized_move_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __ofir
__unreachable_sentinel(), __iter_move);
}
+// Destroys every element in the range [first, last) FROM RIGHT TO LEFT using allocator
+// destruction. If elements are themselves C-style arrays, they are recursively destroyed
+// in the same manner.
+//
+// This function assumes that destructors do not throw, and that the allocator is bound to
+// the correct type.
+template<class _Alloc, class _BidirIter, class = __enable_if_t<
+ __is_cpp17_bidirectional_iterator<_BidirIter>::value
+>>
+_LIBCPP_HIDE_FROM_ABI
+constexpr void __allocator_destroy_multidimensional(_Alloc& __alloc, _BidirIter __first, _BidirIter __last) noexcept {
+ using _ValueType = typename iterator_traits<_BidirIter>::value_type;
+ static_assert(is_same_v<typename allocator_traits<_Alloc>::value_type, _ValueType>,
+ "The allocator should already be rebound to the correct type");
+
+ if (__first == __last)
+ return;
+
+ if constexpr (is_array_v<_ValueType>) {
+ static_assert(!__libcpp_is_unbounded_array<_ValueType>::value,
+ "arrays of unbounded arrays don't exist, but if they did we would mess up here");
+
+ using _Element = remove_extent_t<_ValueType>;
+ __allocator_traits_rebind_t<_Alloc, _Element> __elem_alloc(__alloc);
+ do {
+ --__last;
+ decltype(auto) __array = *__last;
+ std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + extent_v<_ValueType>);
+ } while (__last != __first);
+ } else {
+ do {
+ --__last;
+ allocator_traits<_Alloc>::destroy(__alloc, std::addressof(*__last));
+ } while (__last != __first);
+ }
+}
+
+// Constructs the object at the given location using the allocator's construct method.
+//
+// If the object being constructed is an array, each element of the array is allocator-constructed,
+// recursively. If an exception is thrown during the construction of an array, the initialized
+// elements are destroyed in reverse order of initialization using allocator destruction.
+//
+// This function assumes that the allocator is bound to the correct type.
+template<class _Alloc, class _Tp>
+_LIBCPP_HIDE_FROM_ABI
+constexpr void __allocator_construct_at(_Alloc& __alloc, _Tp* __loc) {
+ static_assert(is_same_v<typename allocator_traits<_Alloc>::value_type, _Tp>,
+ "The allocator should already be rebound to the correct type");
+
+ if constexpr (is_array_v<_Tp>) {
+ using _Element = remove_extent_t<_Tp>;
+ __allocator_traits_rebind_t<_Alloc, _Element> __elem_alloc(__alloc);
+ size_t __i = 0;
+ _Tp& __array = *__loc;
+
+ // If an exception is thrown, destroy what we have constructed so far in reverse order.
+ __transaction __guard([&]() { std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i); });
+ for (; __i != extent_v<_Tp>; ++__i) {
+ std::__allocator_construct_at(__elem_alloc, std::addressof(__array[__i]));
+ }
+ __guard.__complete();
+ } else {
+ allocator_traits<_Alloc>::construct(__alloc, __loc);
+ }
+}
+
+// Constructs the object at the given location using the allocator's construct method, passing along
+// the provided argument.
+//
+// If the object being constructed is an array, the argument is also assumed to be an array. Each
+// each element of the array being constructed is allocator-constructed from the corresponding
+// element of the argument array. If an exception is thrown during the construction of an array,
+// the initialized elements are destroyed in reverse order of initialization using allocator
+// destruction.
+//
+// This function assumes that the allocator is bound to the correct type.
+template<class _Alloc, class _Tp, class _Arg>
+_LIBCPP_HIDE_FROM_ABI
+constexpr void __allocator_construct_at(_Alloc& __alloc, _Tp* __loc, _Arg const& __arg) {
+ static_assert(is_same_v<typename allocator_traits<_Alloc>::value_type, _Tp>,
+ "The allocator should already be rebound to the correct type");
+
+ if constexpr (is_array_v<_Tp>) {
+ static_assert(is_array_v<_Arg>,
+ "Provided non-array initialization argument to __allocator_construct_at when "
+ "trying to construct an array.");
+
+ using _Element = remove_extent_t<_Tp>;
+ __allocator_traits_rebind_t<_Alloc, _Element> __elem_alloc(__alloc);
+ size_t __i = 0;
+ _Tp& __array = *__loc;
+
+ // If an exception is thrown, destroy what we have constructed so far in reverse order.
+ __transaction __guard([&]() { std::__allocator_destroy_multidimensional(__elem_alloc, __array, __array + __i); });
+ for (; __i != extent_v<_Tp>; ++__i) {
+ std::__allocator_construct_at(__elem_alloc, std::addressof(__array[__i]), __arg[__i]);
+ }
+ __guard.__complete();
+ } else {
+ allocator_traits<_Alloc>::construct(__alloc, __loc, __arg);
+ }
+}
+
+// Given a range starting at it and containing n elements, initializes each element in the
+// range from left to right using the construct method of the allocator (rebound to the
+// correct type).
+//
+// If an exception is thrown, the initialized elements are destroyed in reverse order of
+// initialization using allocator_traits destruction. If the elements in the range are C-style
+// arrays, they are initialized element-wise using allocator construction, and recursively so.
+template<class _Alloc, class _BidirIter, class _Tp, class _Size = typename iterator_traits<_BidirIter>::difference_type>
+_LIBCPP_HIDE_FROM_ABI
+constexpr void __uninitialized_allocator_fill_n(_Alloc& __alloc, _BidirIter __it, _Size __n, _Tp const& __value) {
+ using _ValueType = typename iterator_traits<_BidirIter>::value_type;
+ __allocator_traits_rebind_t<_Alloc, _ValueType> __value_alloc(__alloc);
+ _BidirIter __begin = __it;
+
+ // If an exception is thrown, destroy what we have constructed so far in reverse order.
+ __transaction __guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });
+ for (; __n != 0; --__n, ++__it) {
+ std::__allocator_construct_at(__value_alloc, std::addressof(*__it), __value);
+ }
+ __guard.__complete();
+}
+
+// Same as __uninitialized_allocator_fill_n, but doesn't pass any initialization argument
+// to the allocator's construct method, which results in value initialization.
+template<class _Alloc, class _BidirIter, class _Size = typename iterator_traits<_BidirIter>::difference_type>
+_LIBCPP_HIDE_FROM_ABI
+constexpr void __uninitialized_allocator_value_construct_n(_Alloc& __alloc, _BidirIter __it, _Size __n) {
+ using _ValueType = typename iterator_traits<_BidirIter>::value_type;
+ __allocator_traits_rebind_t<_Alloc, _ValueType> __value_alloc(__alloc);
+ _BidirIter __begin = __it;
+
+ // If an exception is thrown, destroy what we have constructed so far in reverse order.
+ __transaction __guard([&]() { std::__allocator_destroy_multidimensional(__value_alloc, __begin, __it); });
+ for (; __n != 0; --__n, ++__it) {
+ std::__allocator_construct_at(__value_alloc, std::addressof(*__it));
+ }
+ __guard.__complete();
+}
+
#endif // _LIBCPP_STD_VER > 14
_LIBCPP_END_NAMESPACE_STD