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