Skip to content

Commit 5780820

Browse files
committed
[libc++] Add __pointer_int_pair
1 parent ee29eb1 commit 5780820

File tree

7 files changed

+349
-0
lines changed

7 files changed

+349
-0
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ set(files
877877
__utility/no_destroy.h
878878
__utility/pair.h
879879
__utility/piecewise_construct.h
880+
__utility/pointer_int_pair.h
880881
__utility/priority_tag.h
881882
__utility/private_constructor_tag.h
882883
__utility/rel_ops.h
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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___UTILITY_POINTER_INT_PAIR_H
10+
#define _LIBCPP___UTILITY_POINTER_INT_PAIR_H
11+
12+
#include <__assert>
13+
#include <__bit/bit_log2.h>
14+
#include <__concepts/derived_from.h>
15+
#include <__config>
16+
#include <__cstddef/size_t.h>
17+
#include <__tuple/tuple_element.h>
18+
#include <__tuple/tuple_size.h>
19+
#include <__type_traits/is_integral.h>
20+
#include <__type_traits/is_pointer.h>
21+
#include <__type_traits/is_unsigned.h>
22+
#include <__type_traits/is_void.h>
23+
#include <__type_traits/remove_pointer.h>
24+
#include <__utility/swap.h>
25+
#include <cstdint>
26+
27+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28+
# pragma GCC system_header
29+
#endif
30+
31+
#if _LIBCPP_STD_VER >= 17
32+
33+
// A __pointer_int_pair is a pair of a pointer and an integral type. The lower bits of the pointer that are free
34+
// due to the alignment requirement of the pointee are used to store the integral type.
35+
//
36+
// This imposes a constraint on the number of bits available for the integral type -- the integral type can use
37+
// at most log2(alignof(T)) bits. This technique allows storing the integral type without additional storage
38+
// beyond that of the pointer itself, at the cost of some bit twiddling.
39+
40+
_LIBCPP_BEGIN_NAMESPACE_STD
41+
42+
template <class>
43+
struct _PointerLikeTraits;
44+
45+
template <class _Tp>
46+
requires(!is_void_v<_Tp>)
47+
struct _PointerLikeTraits<_Tp*> {
48+
static constexpr size_t __low_bits_available = std::__bit_log2(alignof(_Tp));
49+
50+
static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_Tp* __ptr) { return reinterpret_cast<uintptr_t>(__ptr); }
51+
static _LIBCPP_HIDE_FROM_ABI _Tp* __to_pointer(uintptr_t __ptr) { return reinterpret_cast<_Tp*>(__ptr); }
52+
};
53+
54+
template <class _Tp>
55+
requires is_void_v<_Tp>
56+
struct _PointerLikeTraits<_Tp*> {
57+
static constexpr size_t __low_bits_available = 0;
58+
59+
static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_Tp* __ptr) { return reinterpret_cast<uintptr_t>(__ptr); }
60+
static _LIBCPP_HIDE_FROM_ABI _Tp* __to_pointer(uintptr_t __ptr) { return reinterpret_cast<_Tp*>(__ptr); }
61+
};
62+
63+
enum class __integer_width : size_t {};
64+
65+
template <class _Pointer, class _IntType, __integer_width __int_bit_count>
66+
class __pointer_int_pair {
67+
using _PointerTraits = _PointerLikeTraits<_Pointer>;
68+
69+
static constexpr auto __int_width = static_cast<size_t>(__int_bit_count);
70+
71+
static_assert(__int_width <= _PointerTraits::__low_bits_available,
72+
"Not enough bits available for requested bit count");
73+
static_assert(is_integral_v<_IntType>, "_IntType has to be an integral type");
74+
static_assert(is_unsigned_v<_IntType>,
75+
"__pointer_int_pair doesn't work for signed types since that would require handling the sign bit");
76+
77+
static constexpr size_t __extra_bits = _PointerTraits::__low_bits_available - __int_width;
78+
static constexpr uintptr_t __int_mask = static_cast<uintptr_t>(1 << _PointerTraits::__low_bits_available) - 1;
79+
static constexpr uintptr_t __ptr_mask = ~__int_mask;
80+
81+
uintptr_t __value_ = 0;
82+
83+
public:
84+
__pointer_int_pair() = default;
85+
__pointer_int_pair(const __pointer_int_pair&) = default;
86+
__pointer_int_pair(__pointer_int_pair&&) = default;
87+
__pointer_int_pair& operator=(const __pointer_int_pair&) = default;
88+
__pointer_int_pair& operator=(__pointer_int_pair&&) = default;
89+
~__pointer_int_pair() = default;
90+
91+
_LIBCPP_HIDE_FROM_ABI __pointer_int_pair(_Pointer __ptr_value, _IntType __int_value)
92+
: __value_(_PointerTraits::__to_uintptr(__ptr_value) | (__int_value << __extra_bits)) {
93+
_LIBCPP_ASSERT_INTERNAL((__int_value & (__int_mask >> __extra_bits)) == __int_value, "integer is too large!");
94+
_LIBCPP_ASSERT_INTERNAL(
95+
(_PointerTraits::__to_uintptr(__ptr_value) & __ptr_mask) == _PointerTraits::__to_uintptr(__ptr_value),
96+
"Pointer alignment is too low!");
97+
}
98+
99+
_LIBCPP_HIDE_FROM_ABI _IntType __get_value() const { return (__value_ & __int_mask) >> __extra_bits; }
100+
_LIBCPP_HIDE_FROM_ABI _Pointer __get_ptr() const { return _PointerTraits::__to_pointer(__value_ & __ptr_mask); }
101+
102+
template <class>
103+
friend struct _PointerLikeTraits;
104+
};
105+
106+
template <class _Pointer, __integer_width __int_bit_count, class _IntType>
107+
struct _PointerLikeTraits<__pointer_int_pair<_Pointer, _IntType, __int_bit_count>> {
108+
private:
109+
using _PointerIntPair = __pointer_int_pair<_Pointer, _IntType, __int_bit_count>;
110+
111+
public:
112+
static constexpr size_t __low_bits_available = _PointerIntPair::__extra_bits;
113+
114+
static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_PointerIntPair __ptr) { return __ptr.__value_; }
115+
116+
static _LIBCPP_HIDE_FROM_ABI _PointerIntPair __to_pointer(uintptr_t __ptr) {
117+
_PointerIntPair __tmp{};
118+
__tmp.__value_ = __ptr;
119+
return __tmp;
120+
}
121+
};
122+
123+
// Make __pointer_int_pair tuple-like
124+
125+
template <class _Pointer, class _IntType, __integer_width __int_bit_count>
126+
struct tuple_size<__pointer_int_pair<_Pointer, _IntType, __int_bit_count>> : integral_constant<size_t, 2> {};
127+
128+
template <class _Pointer, class _IntType, __integer_width __int_bit_count>
129+
struct tuple_element<0, __pointer_int_pair<_Pointer, _IntType, __int_bit_count>> {
130+
using type = _Pointer;
131+
};
132+
133+
template <class _Pointer, class _IntType, __integer_width __int_bit_count>
134+
struct tuple_element<1, __pointer_int_pair<_Pointer, _IntType, __int_bit_count>> {
135+
using type = _IntType;
136+
};
137+
138+
template <size_t __i, class _Pointer, class _IntType, __integer_width __int_bit_count>
139+
_LIBCPP_HIDE_FROM_ABI auto get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) {
140+
if constexpr (__i == 0) {
141+
return __pair.__get_ptr();
142+
} else if constexpr (__i == 1) {
143+
return __pair.__get_value();
144+
} else {
145+
static_assert(__i == 0, "Index out of bounds");
146+
}
147+
}
148+
149+
_LIBCPP_END_NAMESPACE_STD
150+
151+
#endif // _LIBCPP_STD_VER >= 17
152+
153+
#endif // _LIBCPP___UTILITY_POINTER_INT_PAIR_H

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,7 @@ module std [system] {
20012001
module no_destroy { header "__utility/no_destroy.h" }
20022002
module pair { header "__utility/pair.h" }
20032003
module piecewise_construct { header "__utility/piecewise_construct.h" }
2004+
module pointer_int_pair { header "__utility/pointer_int_pair.h" }
20042005
module priority_tag { header "__utility/priority_tag.h" }
20052006
module private_constructor_tag { header "__utility/private_constructor_tag.h" }
20062007
module rel_ops { header "__utility/rel_ops.h" }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// REQUIRES: has-unix-headers
12+
// REQUIRES: libcpp-hardening-mode=debug
13+
// XFAIL: availability-verbose_abort-missing
14+
15+
#include "test_macros.h"
16+
17+
TEST_DIAGNOSTIC_PUSH
18+
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wprivate-header")
19+
#include <__utility/pointer_int_pair.h>
20+
TEST_DIAGNOSTIC_POP
21+
22+
#include <cassert>
23+
24+
#include "check_assertion.h"
25+
26+
struct [[gnu::packed]] Packed {
27+
char c;
28+
int i;
29+
};
30+
31+
int main(int, char**) {
32+
TEST_LIBCPP_ASSERT_FAILURE(
33+
(std::__pointer_int_pair<int*, size_t, std::__integer_width{1}>{nullptr, 2}), "integer is too large!");
34+
35+
TEST_DIAGNOSTIC_PUSH
36+
TEST_CLANG_DIAGNOSTIC_IGNORED("-Waddress-of-packed-member") // That's what we're trying to test
37+
TEST_GCC_DIAGNOSTIC_IGNORED("-Waddress-of-packed-member")
38+
alignas(int) Packed p;
39+
TEST_LIBCPP_ASSERT_FAILURE(
40+
(std::__pointer_int_pair<int*, size_t, std::__integer_width{1}>{&p.i, 0}), "Pointer alignment is too low!");
41+
TEST_DIAGNOSTIC_POP
42+
43+
return 0;
44+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// Ensure that __pointer_int_pair cannot be constant initialized with a value.
10+
// This would mean that the constructor is `constexpr`, which should only be
11+
// possible with compiler magic.
12+
13+
// UNSUPPORTED: c++03, c++11, c++14
14+
15+
// clang-format off
16+
17+
#include <__utility/pointer_int_pair.h>
18+
#include <cstddef>
19+
20+
template <class Ptr, class UnderlyingType>
21+
using single_bit_pair = std::__pointer_int_pair<Ptr, UnderlyingType, std::__integer_width{1}>;
22+
23+
constinit int ptr = 0;
24+
constinit single_bit_pair<int*, size_t> continitiable_pointer_int_pair_values{&ptr, 0}; // expected-error {{variable does not have a constant initializer}}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
#include <__utility/pointer_int_pair.h>
12+
#include <cassert>
13+
#include <cstddef>
14+
15+
template <class Ptr, class UnderlyingType>
16+
using single_bit_pair = std::__pointer_int_pair<Ptr, UnderlyingType, std::__integer_width{1}>;
17+
18+
template <class Ptr, class UnderlyingType>
19+
using two_bit_pair = std::__pointer_int_pair<Ptr, UnderlyingType, std::__integer_width{2}>;
20+
21+
constinit single_bit_pair<int*, size_t> continitiable_pointer_int_pair;
22+
23+
int main(int, char**) {
24+
{ // __pointer_int_pair() constructor
25+
single_bit_pair<int*, size_t> pair = {};
26+
assert(pair.__get_value() == 0);
27+
assert(pair.__get_ptr() == nullptr);
28+
}
29+
30+
{ // __pointer_int_pair(pointer, int) constructor
31+
single_bit_pair<int*, size_t> pair(nullptr, 1);
32+
assert(pair.__get_value() == 1);
33+
assert(pair.__get_ptr() == nullptr);
34+
}
35+
36+
{ // pointer is correctly packed/unpacked (with different types and values)
37+
int i;
38+
single_bit_pair<int*, size_t> pair(&i, 0);
39+
assert(pair.__get_value() == 0);
40+
assert(pair.__get_ptr() == &i);
41+
}
42+
{
43+
int i;
44+
two_bit_pair<int*, size_t> pair(&i, 2);
45+
assert(pair.__get_value() == 2);
46+
assert(pair.__get_ptr() == &i);
47+
}
48+
{
49+
short i;
50+
single_bit_pair<short*, size_t> pair(&i, 1);
51+
assert(pair.__get_value() == 1);
52+
assert(pair.__get_ptr() == &i);
53+
}
54+
55+
{ // check that a __pointer_int_pair<__pointer_int_pair> works
56+
int i;
57+
single_bit_pair<single_bit_pair<int*, size_t>, size_t> pair({&i, 1}, 0);
58+
assert(pair.__get_value() == 0);
59+
assert(pair.__get_ptr().__get_ptr() == &i);
60+
assert(pair.__get_ptr().__get_value() == 1);
61+
}
62+
63+
{ // check that the tuple protocol is correctly implemented
64+
int i;
65+
two_bit_pair<int*, size_t> pair{&i, 3};
66+
auto [ptr, value] = pair;
67+
assert(ptr == &i);
68+
assert(value == 3);
69+
}
70+
71+
{ // check that the (pointer, int) constructor is implicit
72+
int i;
73+
two_bit_pair<int*, size_t> pair = {&i, 3};
74+
assert(pair.__get_ptr() == &i);
75+
assert(pair.__get_value() == 3);
76+
}
77+
78+
{ // check that overaligned types work as expected
79+
struct alignas(32) Overaligned {
80+
int i;
81+
};
82+
83+
Overaligned i;
84+
std::__pointer_int_pair<Overaligned*, size_t, std::__integer_width{4}> pair = {&i, 13};
85+
assert(pair.__get_ptr() == &i);
86+
assert(pair.__get_value() == 13);
87+
}
88+
89+
{ // check that types other than size_t work as well
90+
int i;
91+
single_bit_pair<int*, bool> pair = {&i, true};
92+
assert(pair.__get_ptr() == &i);
93+
assert(pair.__get_value());
94+
static_assert(std::is_same_v<decltype(pair.__get_value()), bool>);
95+
}
96+
{
97+
int i;
98+
single_bit_pair<int*, unsigned char> pair = {&i, 1};
99+
assert(pair.__get_ptr() == &i);
100+
assert(pair.__get_value() == 1);
101+
static_assert(std::is_same_v<decltype(pair.__get_value()), unsigned char>);
102+
}
103+
104+
return 0;
105+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
#include <__utility/pointer_int_pair.h>
12+
#include <cstddef>
13+
14+
#include "test_macros.h"
15+
16+
// expected-error@*:* {{Not enough bits available for requested bit count}}
17+
std::__pointer_int_pair<char*, size_t, std::__integer_width{2}> ptr1; // expected-note {{here}}
18+
// expected-error@*:* {{_IntType has to be an integral type}}
19+
std::__pointer_int_pair<int*, int*, std::__integer_width{2}> ptr2; // expected-note {{here}}
20+
// expected-error@*:* {{__pointer_int_pair doesn't work for signed types}}
21+
std::__pointer_int_pair<int*, signed, std::__integer_width{2}> ptr3; // expected-note {{here}}

0 commit comments

Comments
 (0)