Skip to content

Commit ae510f0

Browse files
committed
[libc++] Add tombstone traits to use in optional, variant
1 parent f43ef53 commit ae510f0

File tree

16 files changed

+440
-3
lines changed

16 files changed

+440
-3
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ set(files
559559
__memory/swap_allocator.h
560560
__memory/temp_value.h
561561
__memory/temporary_buffer.h
562+
__memory/tombstone_traits.h
562563
__memory/uninitialized_algorithms.h
563564
__memory/unique_ptr.h
564565
__memory/unique_temporary_buffer.h

libcxx/include/__expected/expected.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <__functional/invoke.h>
1818
#include <__memory/addressof.h>
1919
#include <__memory/construct_at.h>
20+
#include <__memory/tombstone_traits.h>
2021
#include <__type_traits/conditional.h>
2122
#include <__type_traits/conjunction.h>
2223
#include <__type_traits/disjunction.h>

libcxx/include/__functional/reference_wrapper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@
1313
#include <__compare/synth_three_way.h>
1414
#include <__concepts/boolean_testable.h>
1515
#include <__config>
16+
#include <__cstddef/size_t.h>
1617
#include <__functional/weak_result_type.h>
1718
#include <__memory/addressof.h>
19+
#include <__memory/tombstone_traits.h>
1820
#include <__type_traits/enable_if.h>
1921
#include <__type_traits/invoke.h>
2022
#include <__type_traits/is_const.h>
2123
#include <__type_traits/remove_cvref.h>
2224
#include <__type_traits/void_t.h>
2325
#include <__utility/declval.h>
2426
#include <__utility/forward.h>
27+
#include <cstdint>
2528

2629
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2730
# pragma GCC system_header
@@ -122,6 +125,12 @@ template <class _Tp>
122125
reference_wrapper(_Tp&) -> reference_wrapper<_Tp>;
123126
#endif
124127

128+
template <class _Tp>
129+
struct __tombstone_traits<reference_wrapper<_Tp>> {
130+
static constexpr uintptr_t __disengaged_value_ = 0;
131+
static constexpr size_t __is_disengaged_offset_ = 0;
132+
};
133+
125134
template <class _Tp>
126135
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference_wrapper<_Tp> ref(_Tp& __t) _NOEXCEPT {
127136
return reference_wrapper<_Tp>(__t);

libcxx/include/__locale

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <__config>
1414
#include <__locale_dir/locale_base_api.h>
1515
#include <__memory/shared_ptr.h> // __shared_count
16+
#include <__memory/tombstone_traits.h>
1617
#include <__mutex/once_flag.h>
1718
#include <__type_traits/make_unsigned.h>
1819
#include <__utility/no_destroy.h>
@@ -114,6 +115,9 @@ private:
114115
friend const _Facet& use_facet(const locale&);
115116
};
116117

118+
template <>
119+
struct __tombstone_traits<locale> : __tombstone_traits_assume_aligned_pointer {};
120+
117121
class _LIBCPP_EXPORTED_FROM_ABI locale::facet : public __shared_count {
118122
protected:
119123
_LIBCPP_HIDE_FROM_ABI explicit facet(size_t __refs = 0) : __shared_count(static_cast<long>(__refs) - 1) {}

libcxx/include/__memory/shared_ptr.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <__memory/compressed_pair.h>
3131
#include <__memory/construct_at.h>
3232
#include <__memory/pointer_traits.h>
33+
#include <__memory/tombstone_traits.h>
3334
#include <__memory/uninitialized_algorithms.h>
3435
#include <__memory/unique_ptr.h>
3536
#include <__type_traits/add_lvalue_reference.h>
@@ -833,6 +834,13 @@ template <class _Tp, class _Dp>
833834
shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
834835
#endif
835836

837+
template <class _Tp>
838+
struct __tombstone_traits<shared_ptr<_Tp>> {
839+
static constexpr auto __disengaged_value_ = __tombstone_traits_assume_aligned_pointer::__disengaged_value_;
840+
static constexpr size_t __is_disengaged_offset_ =
841+
sizeof(void*) + __tombstone_traits_assume_aligned_pointer::__is_disengaged_offset_;
842+
};
843+
836844
//
837845
// std::allocate_shared and std::make_shared
838846
//
@@ -1383,6 +1391,13 @@ template <class _Tp>
13831391
weak_ptr(shared_ptr<_Tp>) -> weak_ptr<_Tp>;
13841392
#endif
13851393

1394+
template <class _Tp>
1395+
struct __tombstone_traits<weak_ptr<_Tp>> {
1396+
static constexpr auto __disengaged_value_ = __tombstone_traits_assume_aligned_pointer::__disengaged_value_;
1397+
static constexpr size_t __is_disengaged_offset_ =
1398+
sizeof(void*) + __tombstone_traits_assume_aligned_pointer::__is_disengaged_offset_;
1399+
};
1400+
13861401
template <class _Tp>
13871402
inline _LIBCPP_CONSTEXPR weak_ptr<_Tp>::weak_ptr() _NOEXCEPT : __ptr_(nullptr), __cntrl_(nullptr) {}
13881403

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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___TYPE_TRAITS_DISENGAGED_TRAITS_H
10+
#define _LIBCPP___TYPE_TRAITS_DISENGAGED_TRAITS_H
11+
12+
#include <__assert>
13+
#include <__config>
14+
#include <__cstddef/size_t.h>
15+
#include <__memory/construct_at.h>
16+
#include <__type_traits/datasizeof.h>
17+
#include <__type_traits/enable_if.h>
18+
#include <__type_traits/is_constant_evaluated.h>
19+
#include <__type_traits/is_fundamental.h>
20+
#include <__type_traits/is_integral.h>
21+
#include <__type_traits/is_trivial.h>
22+
#include <__type_traits/is_trivially_destructible.h>
23+
#include <__type_traits/remove_cv.h>
24+
#include <__type_traits/void_t.h>
25+
#include <__utility/forward.h>
26+
#include <__utility/in_place.h>
27+
#include <__utility/piecewise_construct.h>
28+
#include <cstdint>
29+
30+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31+
# pragma GCC system_header
32+
#endif
33+
34+
#if _LIBCPP_STD_VER >= 17
35+
36+
_LIBCPP_BEGIN_NAMESPACE_STD
37+
38+
template <class>
39+
struct __tombstone_traits;
40+
41+
// bools have always exactly one bit set. If there is more than one set it's disengaged.
42+
template <>
43+
struct __tombstone_traits<bool> {
44+
static constexpr uint8_t __disengaged_value_ = 3;
45+
static constexpr size_t __is_disengaged_offset_ = 0;
46+
};
47+
48+
struct __tombstone_traits_assume_aligned_pointer {
49+
static constexpr uint8_t __disengaged_value_ = 1;
50+
#ifdef _LIBCPP_LITTLE_ENDIAN
51+
static constexpr size_t __is_disengaged_offset_ = 0;
52+
#else
53+
static constexpr size_t __is_disengaged_offset_ = sizeof(void*) - 1;
54+
#endif
55+
};
56+
57+
// TODO: Look into
58+
// - filesystem::directory_iterator
59+
// - vector<T> with alignof(T) == 1
60+
// - string_view (basic_string_view<T> works with alignof(T) >= 2)
61+
62+
// This is constrained on fundamental types because we might not always know the alignment of a user-defined type.
63+
// For example, in one TU there may only be a forward declaration and in another there is already the definition
64+
// available. If we made this optimization conditional on the completeness of the type this would result in a non-benign
65+
// ODR violation.
66+
template <class _Tp>
67+
struct __tombstone_traits<__enable_specialization_if<is_fundamental_v<_Tp> && alignof(_Tp) >= 2, _Tp*>>
68+
: __tombstone_traits_assume_aligned_pointer {};
69+
70+
template <class _Tp>
71+
struct __tombstone_traits<_Tp**> : __tombstone_traits_assume_aligned_pointer {
72+
static_assert(alignof(_Tp*) >= 2, "alignment of a pointer isn't at least 2!?");
73+
};
74+
75+
inline constexpr struct __init_engaged_t {
76+
} __init_engaged;
77+
inline constexpr struct __init_disengaged_t {
78+
} __init_disengaged;
79+
80+
template <class _Tp, class _Payload>
81+
struct __tombstone_is_disengaged {
82+
using _TombstoneLayout = __tombstone_traits<_Tp>;
83+
using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>;
84+
85+
char __padding_[_TombstoneLayout::__is_disengaged_offset_];
86+
_IsDisengagedT __is_disengaged_;
87+
};
88+
89+
template <class _Tp, class _Payload>
90+
requires(__tombstone_traits<_Tp>::__is_disengaged_offset_ == 0)
91+
struct __tombstone_is_disengaged<_Tp, _Payload> {
92+
using _TombstoneLayout = __tombstone_traits<_Tp>;
93+
using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>;
94+
95+
_IsDisengagedT __is_disengaged_;
96+
};
97+
98+
template <class _Tp, class _Payload>
99+
struct __tombstone_data {
100+
using _TombstoneLayout = __tombstone_traits<_Tp>;
101+
using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>;
102+
103+
static_assert(is_trivial<_IsDisengagedT>::value, "disengaged type has to be trivial!");
104+
static_assert(_TombstoneLayout::__is_disengaged_offset_ >= __datasizeof_v<_Payload>);
105+
106+
_LIBCPP_NO_UNIQUE_ADDRESS _Payload __payload_;
107+
char __padding_[_TombstoneLayout::__is_disengaged_offset_ - __datasizeof_v<_Payload>];
108+
_IsDisengagedT __is_disengaged_;
109+
110+
template <class... _Args>
111+
_LIBCPP_HIDE_FROM_ABI constexpr __tombstone_data(_Args&&... __args)
112+
: __payload_(std::forward<_Args>(__args)...), __is_disengaged_(_TombstoneLayout::__disengaged_value_) {}
113+
};
114+
115+
template <class _Tp, class _Payload>
116+
requires(__tombstone_traits<_Tp>::__is_disengaged_offset_ == 0)
117+
struct __tombstone_data<_Tp, _Payload> {
118+
using _TombstoneLayout = __tombstone_traits<_Tp>;
119+
using _IsDisengagedT = remove_cv_t<decltype(_TombstoneLayout::__disengaged_value_)>;
120+
121+
_IsDisengagedT __is_disengaged_;
122+
_LIBCPP_NO_UNIQUE_ADDRESS _Payload __payload_;
123+
124+
template <class... _Args>
125+
_LIBCPP_HIDE_FROM_ABI constexpr __tombstone_data(_Args&&... __args)
126+
: __is_disengaged_(_TombstoneLayout::__disengaged_value_), __payload_(std::forward<_Args>(__args)...) {}
127+
};
128+
129+
template <class _Tp, class _Payload>
130+
struct __tombstoned_value final {
131+
using _TombstoneLayout = __tombstone_traits<_Tp>;
132+
using _TombstoneData = __tombstone_data<_Tp, _Payload>;
133+
134+
union _MaybeTombstone {
135+
_Tp __value_;
136+
_TombstoneData __tombstone_;
137+
138+
template <class... _Args>
139+
constexpr _MaybeTombstone(__init_disengaged_t, _Args&&... __args) : __tombstone_(std::forward<_Args>(__args)...) {}
140+
141+
template <class... _Args>
142+
constexpr _MaybeTombstone(__init_engaged_t, _Args&&... __args) : __value_(std::forward<_Args>(__args)...) {}
143+
144+
_MaybeTombstone(const _MaybeTombstone&) = default;
145+
_MaybeTombstone(_MaybeTombstone&&) = default;
146+
_MaybeTombstone& operator=(const _MaybeTombstone&) = default;
147+
_MaybeTombstone& operator=(_MaybeTombstone&&) = default;
148+
149+
_LIBCPP_HIDE_FROM_ABI constexpr bool __is_engaged() const noexcept {
150+
if (__libcpp_is_constant_evaluated())
151+
return !__builtin_constant_p(__tombstone_.__is_disengaged_ == _TombstoneLayout::__disengaged_value_);
152+
__tombstone_is_disengaged<_Tp, _Payload> __is_disengaged;
153+
static_assert(sizeof(__tombstone_is_disengaged<_Tp, _Payload>) <= sizeof(_MaybeTombstone));
154+
__builtin_memcpy(&__is_disengaged, this, sizeof(__tombstone_is_disengaged<_Tp, _Payload>));
155+
156+
return __is_disengaged.__is_disengaged_ != _TombstoneLayout::__disengaged_value_;
157+
}
158+
159+
_LIBCPP_HIDE_FROM_ABI constexpr ~_MaybeTombstone() {
160+
if (__is_engaged()) {
161+
std::destroy_at(&__value_);
162+
} else {
163+
std::destroy_at(&__tombstone_);
164+
}
165+
}
166+
167+
~_MaybeTombstone()
168+
requires is_trivially_destructible_v<_Tp> && is_trivially_destructible_v<_Payload>
169+
= default;
170+
};
171+
172+
_MaybeTombstone __data_;
173+
174+
static_assert(sizeof(__tombstone_data<_Tp, _Payload>) <= sizeof(_Tp));
175+
static_assert(is_integral_v<decltype(_TombstoneLayout::__disengaged_value_)>);
176+
static_assert(__builtin_offsetof(_TombstoneData, __is_disengaged_) == _TombstoneLayout::__is_disengaged_offset_);
177+
178+
template <class... _Args>
179+
_LIBCPP_HIDE_FROM_ABI constexpr __tombstoned_value(__init_disengaged_t, _Args&&... __args)
180+
: __data_(__init_disengaged, std::forward<_Args>(__args)...) {}
181+
182+
template <class... _Args>
183+
_LIBCPP_HIDE_FROM_ABI constexpr __tombstoned_value(__init_engaged_t, _Args&&... __args)
184+
: __data_(__init_engaged, std::forward<_Args>(__args)...) {}
185+
186+
_LIBCPP_HIDE_FROM_ABI constexpr bool __is_engaged() const noexcept { return __data_.__is_engaged(); }
187+
188+
template <class _Class>
189+
_LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_value(this _Class&& __self) noexcept {
190+
_LIBCPP_ASSERT_INTERNAL(__self.__is_engaged(), "Trying to get the value of a disenagaged tombstoned value");
191+
return std::forward<_Class>(__self).__data_.__value_;
192+
}
193+
194+
template <class _Class>
195+
_LIBCPP_HIDE_FROM_ABI constexpr auto&& __get_payload(this _Class&& __self) noexcept {
196+
_LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to get the payload of an enagaged tombstoned value");
197+
return std::forward<_Class>(__self).__data_.__tombstone_.__payload_;
198+
}
199+
200+
template <class _Class, class... _Args>
201+
_LIBCPP_HIDE_FROM_ABI constexpr void __engage(this _Class& __self, piecewise_construct_t, _Args&&... __args) {
202+
_LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to enage a already engaged tombstoned value");
203+
std::destroy_at(&__self.__data_.__tombstone_);
204+
std::construct_at(&__self.__data_.__value_, std::forward<_Args>(__args)...);
205+
}
206+
207+
template <class _Class, class... _Args>
208+
_LIBCPP_HIDE_FROM_ABI constexpr void __disengage(this _Class& __self, piecewise_construct_t, _Args&&... __args) {
209+
_LIBCPP_ASSERT_INTERNAL(!__self.__is_engaged(), "Trying to disenage a disengaged tombstoned value");
210+
std::destroy_at(&__self.__data_.__value_);
211+
std::construct_at(&__self.__data_.__tombstone_, std::forward<_Args>(__args)...);
212+
}
213+
};
214+
215+
template <class _Tp, class = void>
216+
inline constexpr bool __has_tombstone_v = false;
217+
218+
template <class _Tp>
219+
inline constexpr bool __has_tombstone_v<_Tp, void_t<decltype(sizeof(__tombstone_traits<_Tp>))>> = true;
220+
221+
_LIBCPP_END_NAMESPACE_STD
222+
223+
#endif // _LIBCPP_STD_VER >= 17
224+
225+
#endif // _LIBCPP___TYPE_TRAITS_DISENGAGED_TRAITS_H

libcxx/include/__memory/unique_ptr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <__memory/auto_ptr.h>
2525
#include <__memory/compressed_pair.h>
2626
#include <__memory/pointer_traits.h>
27+
#include <__memory/tombstone_traits.h>
2728
#include <__type_traits/add_lvalue_reference.h>
2829
#include <__type_traits/common_type.h>
2930
#include <__type_traits/conditional.h>
@@ -405,6 +406,14 @@ struct __unique_ptr_array_bounds_stored {
405406
size_t __size_;
406407
};
407408

409+
template <class _Tp>
410+
struct __tombstone_traits<__enable_specialization_if<__has_tombstone_v<_Tp*>, unique_ptr<_Tp>>>
411+
: __tombstone_traits<_Tp*> {};
412+
413+
template <class _Tp>
414+
struct __tombstone_traits<__enable_specialization_if<__has_tombstone_v<_Tp*>, unique_ptr<_Tp[]>>>
415+
: __tombstone_traits<_Tp*> {};
416+
408417
template <class _Tp, class _Dp>
409418
class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
410419
public:

libcxx/include/__type_traits/enable_if.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ template <bool _Bp, class _Tp = void>
3232
using enable_if_t = typename enable_if<_Bp, _Tp>::type;
3333
#endif
3434

35+
template <bool _Bp, class _Tp, class = enable_if_t<_Bp> >
36+
using __enable_specialization_if = _Tp;
37+
3538
_LIBCPP_END_NAMESPACE_STD
3639

3740
#endif // _LIBCPP___TYPE_TRAITS_ENABLE_IF_H

libcxx/include/__utility/pair.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <__fwd/array.h>
1818
#include <__fwd/pair.h>
1919
#include <__fwd/tuple.h>
20+
#include <__memory/tombstone_traits.h>
2021
#include <__tuple/tuple_indices.h>
2122
#include <__tuple/tuple_like_no_subrange.h>
2223
#include <__tuple/tuple_size.h>
@@ -446,6 +447,17 @@ template <class _T1, class _T2>
446447
pair(_T1, _T2) -> pair<_T1, _T2>;
447448
#endif
448449

450+
template <class _Tp, class _Up>
451+
requires __has_tombstone_v<_Up>
452+
struct __tombstone_traits<pair<_Tp, _Up>> {
453+
static constexpr auto __disengaged_value_ = __tombstone_traits<_Up>::__disengaged_value_;
454+
static constexpr size_t __is_disengaged_offset_ = sizeof(_Tp) + __tombstone_traits<_Up>::__is_disengaged_offset_;
455+
};
456+
457+
template <class _Tp, class _Up>
458+
requires(!__has_tombstone_v<_Up> && __has_tombstone_v<_Tp>)
459+
struct __tombstone_traits<pair<_Tp, _Up>> : __tombstone_traits<_Tp> {};
460+
449461
// [pairs.spec], specialized algorithms
450462

451463
template <class _T1, class _T2, class _U1, class _U2>

0 commit comments

Comments
 (0)