-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[libc++] Add __pointer_int_pair #94324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
philnik777
wants to merge
1
commit into
main
Choose a base branch
from
users/philnik777/spr/add_pointer_int_pair
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___UTILITY_POINTER_INT_PAIR_H | ||
#define _LIBCPP___UTILITY_POINTER_INT_PAIR_H | ||
|
||
#include <__assert> | ||
#include <__config> | ||
#include <__cstddef/size_t.h> | ||
#include <__fwd/tuple.h> | ||
#include <__type_traits/enable_if.h> | ||
#include <__type_traits/is_integral.h> | ||
#include <__type_traits/is_unsigned.h> | ||
#include <__type_traits/is_void.h> | ||
#include <cstdint> | ||
#include <limits> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
// A __pointer_int_pair is a pair of a pointer and an integral type. The lower bits of the pointer that are free | ||
// due to the alignment requirement of the pointee are used to store the integral type. | ||
// | ||
// This imposes a constraint on the number of bits available for the integral type -- the integral type can use | ||
// at most log2(alignof(T)) bits. This technique allows storing the integral type without additional storage | ||
// beyond that of the pointer itself, at the cost of some bit twiddling. | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
template <class, class = void> | ||
struct _PointerLikeTraits; | ||
|
||
template <class _Tp> | ||
struct _PointerLikeTraits<_Tp*, __enable_if_t<!is_void<_Tp>::value> > { | ||
// This is really `__bit_log2`, but we need this to be a constant expression even in C++03, so we can't use that | ||
static const size_t __low_bits_available = numeric_limits<size_t>::digits - 1 - __builtin_clzg(_LIBCPP_ALIGNOF(_Tp)); | ||
|
||
static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_Tp* __ptr) { return reinterpret_cast<uintptr_t>(__ptr); } | ||
static _LIBCPP_HIDE_FROM_ABI _Tp* __to_pointer(uintptr_t __ptr) { return reinterpret_cast<_Tp*>(__ptr); } | ||
}; | ||
|
||
template <class _Tp> | ||
struct _PointerLikeTraits<_Tp*, __enable_if_t<is_void<_Tp>::value> > { | ||
static const size_t __low_bits_available = 0; | ||
|
||
static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_Tp* __ptr) { return reinterpret_cast<uintptr_t>(__ptr); } | ||
static _LIBCPP_HIDE_FROM_ABI _Tp* __to_pointer(uintptr_t __ptr) { return reinterpret_cast<_Tp*>(__ptr); } | ||
}; | ||
|
||
enum __integer_width : size_t {}; | ||
|
||
template <class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
class __pointer_int_pair { | ||
using _PointerTraits = _PointerLikeTraits<_Pointer>; | ||
|
||
static const auto __int_width = static_cast<size_t>(__int_bit_count); | ||
|
||
static_assert(__int_width <= _PointerTraits::__low_bits_available, | ||
"Not enough bits available for requested bit count"); | ||
static_assert(is_integral<_IntType>::value, "_IntType has to be an integral type"); | ||
static_assert(is_unsigned<_IntType>::value, | ||
"__pointer_int_pair doesn't work for signed types since that would require handling the sign bit"); | ||
|
||
static const size_t __extra_bits = _PointerTraits::__low_bits_available - __int_width; | ||
static const uintptr_t __int_mask = static_cast<uintptr_t>(1 << _PointerTraits::__low_bits_available) - 1; | ||
static const uintptr_t __ptr_mask = ~__int_mask; | ||
|
||
uintptr_t __value_ = 0; | ||
|
||
public: | ||
__pointer_int_pair() = default; | ||
|
||
_LIBCPP_HIDE_FROM_ABI __pointer_int_pair(_Pointer __ptr_value, _IntType __int_value) | ||
: __value_(_PointerTraits::__to_uintptr(__ptr_value) | (__int_value << __extra_bits)) { | ||
_LIBCPP_ASSERT_INTERNAL((__int_value & (__int_mask >> __extra_bits)) == __int_value, "integer is too large!"); | ||
philnik777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_LIBCPP_ASSERT_INTERNAL( | ||
(_PointerTraits::__to_uintptr(__ptr_value) & __ptr_mask) == _PointerTraits::__to_uintptr(__ptr_value), | ||
"Pointer alignment is too low!"); | ||
} | ||
|
||
_LIBCPP_HIDE_FROM_ABI _IntType __get_value() const { return (__value_ & __int_mask) >> __extra_bits; } | ||
_LIBCPP_HIDE_FROM_ABI _Pointer __get_ptr() const { return _PointerTraits::__to_pointer(__value_ & __ptr_mask); } | ||
|
||
template <class, class> | ||
friend struct _PointerLikeTraits; | ||
}; | ||
|
||
template <class _Pointer, __integer_width __int_bit_count, class _IntType> | ||
struct _PointerLikeTraits<__pointer_int_pair<_Pointer, _IntType, __int_bit_count> > { | ||
private: | ||
using _PointerIntPair = __pointer_int_pair<_Pointer, _IntType, __int_bit_count>; | ||
|
||
public: | ||
static inline const size_t __low_bits_available = _PointerIntPair::__extra_bits; | ||
|
||
static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_PointerIntPair __ptr) { return __ptr.__value_; } | ||
|
||
static _LIBCPP_HIDE_FROM_ABI _PointerIntPair __to_pointer(uintptr_t __ptr) { | ||
_PointerIntPair __tmp; | ||
__tmp.__value_ = __ptr; | ||
return __tmp; | ||
} | ||
}; | ||
|
||
#ifndef _LIBCPP_CXX03_LANG | ||
|
||
// Make __pointer_int_pair tuple-like | ||
|
||
template <class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this to be It's a bunch of instantiations we should really try to avoid. (I'm considering how much the bloat of |
||
struct tuple_size<__pointer_int_pair<_Pointer, _IntType, __int_bit_count> > : integral_constant<size_t, 2> {}; | ||
|
||
template <class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
struct tuple_element<0, __pointer_int_pair<_Pointer, _IntType, __int_bit_count> > { | ||
using type = _Pointer; | ||
}; | ||
|
||
template <class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
struct tuple_element<1, __pointer_int_pair<_Pointer, _IntType, __int_bit_count> > { | ||
using type = _IntType; | ||
}; | ||
|
||
template <size_t __i> | ||
struct __pointer_int_pair_getter; | ||
|
||
template <> | ||
struct __pointer_int_pair_getter<0> { | ||
template <class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Pointer | ||
__get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) { | ||
return __pair.__get_ptr(); | ||
} | ||
}; | ||
|
||
template <> | ||
struct __pointer_int_pair_getter<1> { | ||
template <class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _IntType | ||
__get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) { | ||
return __pair.__get_value(); | ||
} | ||
}; | ||
|
||
template <size_t __i, class _Pointer, class _IntType, __integer_width __int_bit_count> | ||
_LIBCPP_HIDE_FROM_ABI typename tuple_element<__i, __pointer_int_pair<_Pointer, _IntType, __int_bit_count> >::type | ||
get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) { | ||
return __pointer_int_pair_getter<__i>::__get(__pair); | ||
} | ||
|
||
#endif // _LIBCPP_CXX03_LANG | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___UTILITY_POINTER_INT_PAIR_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
libcxx/test/libcxx/utilities/pointer_int_pair/assert.constructor.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// REQUIRES: has-unix-headers | ||
// REQUIRES: libcpp-hardening-mode=debug | ||
// XFAIL: availability-verbose_abort-missing | ||
|
||
#include "test_macros.h" | ||
|
||
TEST_DIAGNOSTIC_PUSH | ||
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wprivate-header") | ||
#include <__utility/pointer_int_pair.h> | ||
TEST_DIAGNOSTIC_POP | ||
|
||
#include <cassert> | ||
|
||
#include "check_assertion.h" | ||
|
||
struct [[gnu::packed]] Packed { | ||
char c; | ||
int i; | ||
}; | ||
|
||
int main(int, char**) { | ||
TEST_LIBCPP_ASSERT_FAILURE( | ||
(std::__pointer_int_pair<int*, size_t, std::__integer_width{1}>{nullptr, 2}), "integer is too large!"); | ||
|
||
TEST_DIAGNOSTIC_PUSH | ||
TEST_CLANG_DIAGNOSTIC_IGNORED("-Waddress-of-packed-member") // That's what we're trying to test | ||
TEST_GCC_DIAGNOSTIC_IGNORED("-Waddress-of-packed-member") | ||
alignas(int) Packed p; | ||
TEST_LIBCPP_ASSERT_FAILURE( | ||
(std::__pointer_int_pair<int*, size_t, std::__integer_width{1}>{&p.i, 0}), "Pointer alignment is too low!"); | ||
TEST_DIAGNOSTIC_POP | ||
|
||
return 0; | ||
} |
24 changes: 24 additions & 0 deletions
24
libcxx/test/libcxx/utilities/pointer_int_pair/constinit.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Ensure that __pointer_int_pair cannot be constant initialized with a value. | ||
// This would mean that the constructor is `constexpr`, which should only be | ||
// possible with compiler magic. | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
|
||
// clang-format off | ||
|
||
#include <__utility/pointer_int_pair.h> | ||
#include <cstddef> | ||
|
||
template <class Ptr, class UnderlyingType> | ||
using single_bit_pair = std::__pointer_int_pair<Ptr, UnderlyingType, std::__integer_width{1}>; | ||
|
||
constinit int ptr = 0; | ||
constinit single_bit_pair<int*, size_t> continitiable_pointer_int_pair_values{&ptr, 0}; // expected-error {{variable does not have a constant initializer}} |
114 changes: 114 additions & 0 deletions
114
libcxx/test/libcxx/utilities/pointer_int_pair/pointer_int_pair.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// XFAIL: FROZEN-CXX03-HEADERS-FIXME | ||
|
||
#include <__utility/pointer_int_pair.h> | ||
#include <cassert> | ||
#include <cstddef> | ||
#include <type_traits> | ||
|
||
#include "test_macros.h" | ||
|
||
template <class Ptr, class UnderlyingType> | ||
using single_bit_pair = std::__pointer_int_pair<Ptr, UnderlyingType, std::__integer_width(1)>; | ||
|
||
template <class Ptr, class UnderlyingType> | ||
using two_bit_pair = std::__pointer_int_pair<Ptr, UnderlyingType, std::__integer_width(2)>; | ||
|
||
#if _LIBCPP_STD_VER >= 20 | ||
constinit single_bit_pair<int*, size_t> continitiable_pointer_int_pair; | ||
#endif | ||
|
||
int main(int, char**) { | ||
#if TEST_STD_VER >= 11 | ||
{ // __pointer_int_pair() constructor | ||
single_bit_pair<int*, size_t> pair = {}; | ||
assert(pair.__get_value() == 0); | ||
assert(pair.__get_ptr() == nullptr); | ||
} | ||
#endif | ||
|
||
{ // __pointer_int_pair(pointer, int) constructor | ||
single_bit_pair<int*, size_t> pair(nullptr, 1); | ||
assert(pair.__get_value() == 1); | ||
assert(pair.__get_ptr() == nullptr); | ||
} | ||
|
||
{ // pointer is correctly packed/unpacked (with different types and values) | ||
int i; | ||
single_bit_pair<int*, size_t> pair(&i, 0); | ||
assert(pair.__get_value() == 0); | ||
assert(pair.__get_ptr() == &i); | ||
} | ||
{ | ||
int i; | ||
two_bit_pair<int*, size_t> pair(&i, 2); | ||
assert(pair.__get_value() == 2); | ||
assert(pair.__get_ptr() == &i); | ||
} | ||
{ | ||
short i; | ||
single_bit_pair<short*, size_t> pair(&i, 1); | ||
assert(pair.__get_value() == 1); | ||
assert(pair.__get_ptr() == &i); | ||
} | ||
|
||
{ // check that a __pointer_int_pair<__pointer_int_pair> works | ||
int i; | ||
single_bit_pair<single_bit_pair<int*, size_t>, size_t> pair(single_bit_pair<int*, size_t>(&i, 1), 0); | ||
assert(pair.__get_value() == 0); | ||
assert(pair.__get_ptr().__get_ptr() == &i); | ||
assert(pair.__get_ptr().__get_value() == 1); | ||
} | ||
|
||
#if _LIBCPP_STD_VER >= 17 | ||
{ // check that the tuple protocol is correctly implemented | ||
int i; | ||
two_bit_pair<int*, size_t> pair{&i, 3}; | ||
auto [ptr, value] = pair; | ||
assert(ptr == &i); | ||
assert(value == 3); | ||
} | ||
#endif | ||
|
||
{ // check that the (pointer, int) constructor is implicit | ||
int i; | ||
two_bit_pair<int*, size_t> pair(&i, 3); | ||
assert(pair.__get_ptr() == &i); | ||
assert(pair.__get_value() == 3); | ||
} | ||
|
||
{ // check that overaligned types work as expected | ||
struct TEST_ALIGNAS(32) Overaligned { | ||
int i; | ||
}; | ||
|
||
Overaligned i; | ||
std::__pointer_int_pair<Overaligned*, size_t, std::__integer_width(4)> pair(&i, 13); | ||
assert(pair.__get_ptr() == &i); | ||
assert(pair.__get_value() == 13); | ||
} | ||
|
||
{ // check that types other than size_t work as well | ||
int i; | ||
single_bit_pair<int*, bool> pair(&i, true); | ||
assert(pair.__get_ptr() == &i); | ||
assert(pair.__get_value()); | ||
static_assert(std::is_same<decltype(pair.__get_value()), bool>::value, ""); | ||
} | ||
{ | ||
int i; | ||
single_bit_pair<int*, unsigned char> pair(&i, 1); | ||
assert(pair.__get_ptr() == &i); | ||
assert(pair.__get_value() == 1); | ||
static_assert(std::is_same<decltype(pair.__get_value()), unsigned char>::value, ""); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/foonathan/tiny might be worth taking a look at.
_PointerLikeTraits
is probably good enough for now, but if we want to generalize we might want to change the name of the trait.