Skip to content

Commit c9cbd37

Browse files
committed
[libc++] Add __pointer_int_pair
1 parent 8a5c8eb commit c9cbd37

File tree

9 files changed

+350
-2
lines changed

9 files changed

+350
-2
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ set(files
917917
__utility/no_destroy.h
918918
__utility/pair.h
919919
__utility/piecewise_construct.h
920+
__utility/pointer_int_pair.h
920921
__utility/priority_tag.h
921922
__utility/private_constructor_tag.h
922923
__utility/rel_ops.h

libcxx/include/__bit/bit_log2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
_LIBCPP_BEGIN_NAMESPACE_STD
2222

2323
template <class _Tp>
24-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __bit_log2(_Tp __t) _NOEXCEPT {
24+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp __bit_log2(_Tp __t) _NOEXCEPT {
2525
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
2626
return numeric_limits<_Tp>::digits - 1 - std::__countl_zero(__t);
2727
}

libcxx/include/__bit/countl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _LIBCPP_PUSH_MACROS
2424
_LIBCPP_BEGIN_NAMESPACE_STD
2525

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

libcxx/include/module.modulemap.in

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

0 commit comments

Comments
 (0)