Skip to content

Commit 9d97c28

Browse files
committed
add aliased_pointer support to schemas
1 parent c7b2a6d commit 9d97c28

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

libcxx/include/__memory/tagged_ptr.h

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2929
template <typename T, typename Y> concept convertible_to_from = std::convertible_to<Y, T> && std::convertible_to<T, Y>;
3030

3131
template <typename T> concept pointer_tagging_schema = requires(T::dirty_pointer payload, T::clean_pointer clean, T::tag_type tag) {
32-
//requires convertible_to_from<typename T::tag_type, uintptr_t>;
32+
requires convertible_to_from<typename T::tag_type, uintptr_t>;
3333
requires std::is_pointer_v<typename T::clean_pointer>;
3434

3535
{ T::encode_pointer_with_tag(clean, tag) } noexcept -> std::same_as<typename T::dirty_pointer>;
@@ -41,6 +41,7 @@ template <typename T> concept pointer_tagging_schema_with_aliasing = pointer_tag
4141
{ T::recover_aliasing_pointer(payload) } noexcept -> std::same_as<typename T::clean_pointer>;
4242
};
4343

44+
// no-op schema so I can better explain how schemas work
4445
struct no_tag {
4546
template <typename T, typename Tag> struct schema {
4647
using clean_pointer = T *;
@@ -53,12 +54,17 @@ struct no_tag {
5354
[[clang::always_inline]] static constexpr clean_pointer recover_pointer(dirty_pointer _ptr) noexcept {
5455
return (clean_pointer)_ptr;
5556
}
57+
[[clang::always_inline]] static constexpr clean_pointer recover_aliasing_pointer(dirty_pointer _ptr) noexcept {
58+
return (clean_pointer)_ptr;
59+
}
5660
[[clang::always_inline]] static constexpr tag_type recover_value(dirty_pointer) noexcept {
5761
return {};
5862
}
5963
};
6064
};
6165

66+
// most basic schema for tagging
67+
// it lets user to provide their own mask
6268
template <uintptr_t Mask> struct bitmask_tag {
6369
static constexpr uintptr_t _mask = Mask;
6470

@@ -91,16 +97,40 @@ template <uintptr_t Mask> struct bitmask_tag {
9197
};
9298
};
9399

100+
// schema which allows only pointer of custom provided minimal alignment
101+
// otherwise it behaves as custom mask schema
94102
template <unsigned Alignment> struct custom_alignment_tag {
95103
static constexpr uintptr_t mask = (static_cast<uintptr_t>(1u) << static_cast<uintptr_t>(Alignment)) - 1ull;
96-
template <typename T, typename Tag> using schema = typename bitmask_tag<mask>::template schema<T, Tag>;
104+
template <typename T, typename Tag> struct schema: bitmask_tag<mask>::template schema<T, Tag> {
105+
using _underlying_schema =bitmask_tag<mask>::template schema<T, Tag>;
106+
107+
using clean_pointer = _underlying_schema::clean_pointer;
108+
using dirty_pointer = _underlying_schema::dirty_pointer;
109+
using tag_type = _underlying_schema::tag_type;
110+
111+
[[clang::always_inline]] static constexpr dirty_pointer encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept {
112+
#if __has_builtin(__builtin_is_aligned)
113+
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__builtin_is_aligned(_ptr, Alignment), "Pointer must be aligned by provided alignemt for tagging");
114+
#else
115+
if !consteval {
116+
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(reinterpret_cast<uintptr_t>(std::addressof(_ptr)) % Alignment == 0, "Pointer must be aligned by provided alignemt for tagging");
117+
}
118+
#endif
119+
return _underlying_schema::encode_pointer_with_tag(_ptr, _value);
120+
}
121+
122+
using _underlying_schema::recover_pointer;
123+
using _underlying_schema::recover_value;
124+
};
97125
};
98126

127+
// default scheme which gives only bits from alignment
99128
struct alignment_low_bits_tag {
100129
template <typename T> static constexpr unsigned alignment = alignof(T);
101130
template <typename T, typename Tag> using schema = typename custom_alignment_tag<alignment<T>>::template schema<T, Tag>;
102131
};
103132

133+
// scheme which shifts bits to left by Bits bits and gives the space for tagging
104134
template <unsigned Bits> struct shift_tag {
105135
static constexpr unsigned _shift = Bits;
106136
static constexpr uintptr_t _mask = (uintptr_t{1u} << _shift) - 1u;
@@ -134,40 +164,56 @@ template <unsigned Bits> struct shift_tag {
134164
};
135165
};
136166

167+
// scheme which shifts pointer to left by 8 bits and give this space as guaranteed space for tagging
137168
struct low_byte_tag {
138169
template <typename T, typename Tag> using schema = typename shift_tag<8>::template schema<T, Tag>;
139170
};
140171

172+
// this will give user access to upper byte of pointer on aarch64
173+
// also it supports recovering aliasing pointer as no-op (fast-path)
141174
struct upper_byte_tag {
142175
template <typename T> static constexpr unsigned _shift = sizeof(T *) * 8ull - 8ull;
143176
template <typename T> static constexpr uintptr_t _mask = 0b1111'1111ull << _shift<T>;
144177

145-
template <typename T, typename Tag> using schema = typename bitmask_tag<_mask<T>>::template schema<T, Tag>;
178+
template <typename T, typename Tag> struct schema: bitmask_tag<_mask<T>>::template schema<T, Tag> {
179+
using _underlying_schema = bitmask_tag<_mask<T>>::template schema<T, Tag>;
180+
181+
using clean_pointer = _underlying_schema::clean_pointer;
182+
using dirty_pointer = _underlying_schema::dirty_pointer;
183+
using tag_type = _underlying_schema::tag_type;
184+
185+
[[clang::always_inline]] static constexpr clean_pointer recover_aliasing_pointer(dirty_pointer _ptr) noexcept {
186+
return (clean_pointer)_ptr;
187+
}
188+
189+
using _underlying_schema::encode_pointer_with_tag;
190+
using _underlying_schema::recover_pointer;
191+
using _underlying_schema::recover_value;
192+
};
146193
};
147194

148-
struct upper_byte_shifted_tag: upper_byte_tag {
149-
template <typename T, typename Tag> struct schema {
150-
using _underlying_schema = typename upper_byte_tag::template schema<T, uintptr_t>;
151-
static constexpr unsigned _shift = upper_byte_tag::template _shift<T>;
195+
// improved version of previous aarch64 upper byte scheme
196+
// with added shifting tag value into position, so the tag doesn't need to know about exact position
197+
struct upper_byte_shifted_tag: upper_byte_tag {
198+
template <typename T, typename Tag> struct schema: upper_byte_tag::template schema<T, uintptr_t> {
199+
using _underlying_schema = upper_byte_tag::template schema<T, uintptr_t>;
152200

153-
using clean_pointer = T *;
154-
using dirty_pointer = void *;
201+
using clean_pointer = _underlying_schema::clean_pointer;
202+
using dirty_pointer = _underlying_schema::dirty_pointer;
155203
using tag_type = Tag;
156204

157205
[[clang::always_inline]] static constexpr dirty_pointer encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept {
158-
return _underlying_schema::encode_pointer_with_tag(_ptr, static_cast<uintptr_t>(_value) << _shift);
159-
}
160-
[[clang::always_inline]] static constexpr clean_pointer recover_pointer(dirty_pointer _ptr) noexcept {
161-
return _underlying_schema::recover_pointer(_ptr);
206+
return _underlying_schema::encode_pointer_with_tag(_ptr, static_cast<uintptr_t>(_value) << upper_byte_tag::_shift<T>);
162207
}
163208
[[clang::always_inline]] static constexpr tag_type recover_value(dirty_pointer _ptr) noexcept {
164-
return static_cast<tag_type>(_underlying_schema::recover_value(_ptr) >> _shift);
209+
return static_cast<tag_type>(_underlying_schema::recover_value(_ptr) >> upper_byte_tag::_shift<T>);
165210
}
211+
212+
using _underlying_schema::recover_pointer;
213+
using _underlying_schema::recover_aliasing_pointer;
166214
};
167215
};
168216

169-
170-
171217
// forward declaration
172218
template <typename _T, typename _Tag = uintptr_t, typename _Schema = alignment_low_bits_tag> class tagged_ptr;
173219

0 commit comments

Comments
 (0)