Skip to content

Commit df5f43d

Browse files
committed
[libc++] Mark more types as trivially relocatable
1 parent bbd6a2d commit df5f43d

File tree

20 files changed

+193
-0
lines changed

20 files changed

+193
-0
lines changed

libcxx/include/__exception/exception_ptr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
6464
friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
6565

6666
public:
67+
// exception_ptr is basically a COW string.
68+
using __trivially_relocatable = exception_ptr;
69+
6770
_LIBCPP_HIDE_FROM_ABI exception_ptr() _NOEXCEPT : __ptr_() {}
6871
_LIBCPP_HIDE_FROM_ABI exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
6972

libcxx/include/__expected/expected.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <__type_traits/is_swappable.h>
3232
#include <__type_traits/is_trivially_constructible.h>
3333
#include <__type_traits/is_trivially_destructible.h>
34+
#include <__type_traits/is_trivially_relocatable.h>
3435
#include <__type_traits/is_void.h>
3536
#include <__type_traits/lazy.h>
3637
#include <__type_traits/negation.h>
@@ -463,6 +464,11 @@ class expected : private __expected_base<_Tp, _Err> {
463464
using error_type = _Err;
464465
using unexpected_type = unexpected<_Err>;
465466

467+
using __trivially_relocatable =
468+
__conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value && __libcpp_is_trivially_relocatable<_Err>::value,
469+
expected,
470+
void>;
471+
466472
template <class _Up>
467473
using rebind = expected<_Up, error_type>;
468474

libcxx/include/__locale

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ _LIBCPP_HIDE_FROM_ABI const _Facet& use_facet(const locale&);
4949

5050
class _LIBCPP_EXPORTED_FROM_ABI locale {
5151
public:
52+
// locale is essentially a shared_ptr that doesn't support weak_ptrs and never got a move constructor.
53+
using __trivially_relocatable = locale;
54+
5255
// types:
5356
class _LIBCPP_EXPORTED_FROM_ABI facet;
5457
class _LIBCPP_EXPORTED_FROM_ABI id;

libcxx/include/__memory/shared_ptr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
419419
typedef _Tp element_type;
420420
#endif
421421

422+
// A shared_ptr contains only two raw pointers which point to the heap and move constructing already doesn't require
423+
// any bookkeeping, so it's always trivially relocatable.
424+
using __trivially_relocatable = shared_ptr;
425+
422426
private:
423427
element_type* __ptr_;
424428
__shared_weak_count* __cntrl_;
@@ -1301,6 +1305,10 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS weak_ptr {
13011305
typedef _Tp element_type;
13021306
#endif
13031307

1308+
// A weak_ptr contains only two raw pointers which point to the heap and move constructing already doesn't require
1309+
// any bookkeeping, so it's always trivially relocatable.
1310+
using __trivially_relocatable = weak_ptr;
1311+
13041312
private:
13051313
element_type* __ptr_;
13061314
__shared_weak_count* __cntrl_;

libcxx/include/__split_buffer

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
#include <__memory/pointer_traits.h>
2525
#include <__memory/swap_allocator.h>
2626
#include <__type_traits/add_lvalue_reference.h>
27+
#include <__type_traits/conditional.h>
2728
#include <__type_traits/enable_if.h>
2829
#include <__type_traits/integral_constant.h>
2930
#include <__type_traits/is_nothrow_assignable.h>
3031
#include <__type_traits/is_nothrow_constructible.h>
3132
#include <__type_traits/is_swappable.h>
3233
#include <__type_traits/is_trivially_destructible.h>
34+
#include <__type_traits/is_trivially_relocatable.h>
3335
#include <__type_traits/remove_reference.h>
3436
#include <__utility/forward.h>
3537
#include <__utility/move.h>
@@ -64,6 +66,15 @@ public:
6466
using iterator = pointer;
6567
using const_iterator = const_pointer;
6668

69+
// A __split_buffer containers the following members which may be trivially relocatable:
70+
// - pointer: may be trivially relocatable, so it's checked
71+
// - allocator_type: may be trivially relocatable, so it's checked
72+
// __split_buffer doesn't have any self-references, so it's trivially relocatable if its members are.
73+
using __trivially_relocatable = __conditional_t<
74+
__libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
75+
__split_buffer,
76+
void>;
77+
6778
pointer __first_;
6879
pointer __begin_;
6980
pointer __end_;

libcxx/include/__utility/pair.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <__type_traits/is_nothrow_constructible.h>
3535
#include <__type_traits/is_same.h>
3636
#include <__type_traits/is_swappable.h>
37+
#include <__type_traits/is_trivially_relocatable.h>
3738
#include <__type_traits/nat.h>
3839
#include <__type_traits/remove_cvref.h>
3940
#include <__type_traits/unwrap_ref.h>
@@ -71,6 +72,11 @@ struct _LIBCPP_TEMPLATE_VIS pair
7172
_T1 first;
7273
_T2 second;
7374

75+
using __trivially_relocatable =
76+
__conditional_t<__libcpp_is_trivially_relocatable<_T1>::value && __libcpp_is_trivially_relocatable<_T2>::value,
77+
pair,
78+
void>;
79+
7480
_LIBCPP_HIDE_FROM_ABI pair(pair const&) = default;
7581
_LIBCPP_HIDE_FROM_ABI pair(pair&&) = default;
7682

libcxx/include/array

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexce
130130
#include <__type_traits/is_nothrow_constructible.h>
131131
#include <__type_traits/is_same.h>
132132
#include <__type_traits/is_swappable.h>
133+
#include <__type_traits/is_trivially_relocatable.h>
133134
#include <__type_traits/remove_cv.h>
134135
#include <__utility/empty.h>
135136
#include <__utility/integer_sequence.h>
@@ -166,6 +167,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
166167

167168
template <class _Tp, size_t _Size>
168169
struct _LIBCPP_TEMPLATE_VIS array {
170+
using __trivially_relocatable = __conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, array, void>;
171+
169172
// types:
170173
using __self = array;
171174
using value_type = _Tp;

libcxx/include/deque

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,16 @@ public:
478478
using reverse_iterator = std::reverse_iterator<iterator>;
479479
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
480480

481+
// A deque contains the following members which may be trivially relocatable:
482+
// - __map: is a `__split_buffer`, see `__split_buffer` for more information on when it is trivially relocatable
483+
// - size_type: is always trivially relocatable, since it is required to be an integral type
484+
// - allocator_type: may not be trivially relocatable, so it's checked
485+
// None of these are referencing the `deque` itself, so if all of them are trivially relocatable, `deque` is too.
486+
using __trivially_relocatable = __conditional_t<
487+
__libcpp_is_trivially_relocatable<__map>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
488+
deque,
489+
void>;
490+
481491
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
482492
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
483493
"original allocator");

libcxx/include/optional

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ namespace std {
210210
#include <__type_traits/is_trivially_assignable.h>
211211
#include <__type_traits/is_trivially_constructible.h>
212212
#include <__type_traits/is_trivially_destructible.h>
213+
#include <__type_traits/is_trivially_relocatable.h>
213214
#include <__type_traits/negation.h>
214215
#include <__type_traits/remove_const.h>
215216
#include <__type_traits/remove_cvref.h>
@@ -581,6 +582,8 @@ class _LIBCPP_DECLSPEC_EMPTY_BASES optional
581582
public:
582583
using value_type = _Tp;
583584

585+
using __trivially_relocatable = conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, optional, void>;
586+
584587
private:
585588
// Disable the reference extension using this static assert.
586589
static_assert(!is_same_v<__remove_cvref_t<value_type>, in_place_t>,

libcxx/include/tuple

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ template <class... Types>
241241
#include <__type_traits/is_reference.h>
242242
#include <__type_traits/is_same.h>
243243
#include <__type_traits/is_swappable.h>
244+
#include <__type_traits/is_trivially_relocatable.h>
244245
#include <__type_traits/lazy.h>
245246
#include <__type_traits/maybe_const.h>
246247
#include <__type_traits/nat.h>
@@ -538,6 +539,8 @@ class _LIBCPP_TEMPLATE_VIS tuple {
538539
get(const tuple<_Up...>&&) _NOEXCEPT;
539540

540541
public:
542+
using __trivially_relocatable = __conditional_t<_And<__libcpp_is_trivially_relocatable<_Tp>...>::value, tuple, void>;
543+
541544
// [tuple.cnstr]
542545

543546
// tuple() constructors (including allocator_arg_t variants)

libcxx/include/variant

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ namespace std {
239239
#include <__type_traits/is_trivially_assignable.h>
240240
#include <__type_traits/is_trivially_constructible.h>
241241
#include <__type_traits/is_trivially_destructible.h>
242+
#include <__type_traits/is_trivially_relocatable.h>
242243
#include <__type_traits/is_void.h>
243244
#include <__type_traits/remove_const.h>
244245
#include <__type_traits/remove_cvref.h>
@@ -1165,6 +1166,9 @@ class _LIBCPP_TEMPLATE_VIS _LIBCPP_DECLSPEC_EMPTY_BASES variant
11651166
using __first_type = variant_alternative_t<0, variant>;
11661167

11671168
public:
1169+
using __trivially_relocatable =
1170+
conditional_t<(__libcpp_is_trivially_relocatable<_Types>::value && ...), variant, void>;
1171+
11681172
template <bool _Dummy = true,
11691173
enable_if_t<__dependent_type<is_default_constructible<__first_type>, _Dummy>::value, int> = 0>
11701174
_LIBCPP_HIDE_FROM_ABI constexpr variant() noexcept(is_nothrow_default_constructible_v<__first_type>)

libcxx/include/vector

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ public:
407407
typedef std::reverse_iterator<iterator> reverse_iterator;
408408
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
409409

410+
// A vector containers the following members which may be trivially relocatable:
411+
// - pointer: may be trivially relocatable, so it's checked
412+
// - allocator_type: may be trivially relocatable, so it's checked
413+
// vector doesn't contain any self-references, so it's trivially relocatable if its members are.
414+
using __trivially_relocatable = __conditional_t<
415+
__libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
416+
vector,
417+
void>;
418+
410419
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
411420
"Allocator::value_type must be same type as value_type");
412421

libcxx/test/libcxx/transitive_includes/cxx03.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ thread type_traits
905905
thread version
906906
tuple compare
907907
tuple cstddef
908+
tuple cstdint
908909
tuple exception
909910
tuple iosfwd
910911
tuple new

libcxx/test/libcxx/transitive_includes/cxx11.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ thread type_traits
912912
thread version
913913
tuple compare
914914
tuple cstddef
915+
tuple cstdint
915916
tuple exception
916917
tuple iosfwd
917918
tuple new

libcxx/test/libcxx/transitive_includes/cxx14.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ thread type_traits
915915
thread version
916916
tuple compare
917917
tuple cstddef
918+
tuple cstdint
918919
tuple exception
919920
tuple iosfwd
920921
tuple new

libcxx/test/libcxx/transitive_includes/cxx17.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ thread type_traits
915915
thread version
916916
tuple compare
917917
tuple cstddef
918+
tuple cstdint
918919
tuple exception
919920
tuple iosfwd
920921
tuple new

libcxx/test/libcxx/transitive_includes/cxx20.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ thread type_traits
925925
thread version
926926
tuple compare
927927
tuple cstddef
928+
tuple cstdint
928929
tuple exception
929930
tuple iosfwd
930931
tuple new

libcxx/test/libcxx/transitive_includes/cxx23.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ thread typeinfo
650650
thread version
651651
tuple compare
652652
tuple cstddef
653+
tuple cstdint
653654
tuple version
654655
type_traits cstddef
655656
type_traits cstdint
@@ -684,6 +685,7 @@ unordered_set tuple
684685
unordered_set version
685686
utility compare
686687
utility cstddef
688+
utility cstdint
687689
utility initializer_list
688690
utility limits
689691
utility version

libcxx/test/libcxx/transitive_includes/cxx26.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ exception typeinfo
161161
exception version
162162
execution version
163163
expected cstddef
164+
expected cstdint
164165
expected initializer_list
165166
expected new
166167
expected version
@@ -494,6 +495,7 @@ regex typeinfo
494495
regex vector
495496
regex version
496497
scoped_allocator cstddef
498+
scoped_allocator cstdint
497499
scoped_allocator limits
498500
scoped_allocator new
499501
scoped_allocator tuple
@@ -650,6 +652,7 @@ thread typeinfo
650652
thread version
651653
tuple compare
652654
tuple cstddef
655+
tuple cstdint
653656
tuple version
654657
type_traits cstddef
655658
type_traits cstdint
@@ -684,6 +687,7 @@ unordered_set tuple
684687
unordered_set version
685688
utility compare
686689
utility cstddef
690+
utility cstdint
687691
utility initializer_list
688692
utility limits
689693
utility version

0 commit comments

Comments
 (0)