|
| 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 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 36 | + |
| 37 | +template <class, class = void> |
| 38 | +struct _PointerLikeTraits; |
| 39 | + |
| 40 | +template <class _Tp> |
| 41 | +struct _PointerLikeTraits<_Tp*, __enable_if_t<!is_void<_Tp>::value> > { |
| 42 | + // This is really `__bit_log2`, but we need it to be a constant expression even in C++03, so we can't use that |
| 43 | + static const size_t __low_bits_available = numeric_limits<size_t>::digits - 1 - __builtin_clzg(_LIBCPP_ALIGNOF(_Tp)); |
| 44 | + |
| 45 | + static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_Tp* __ptr) { return reinterpret_cast<uintptr_t>(__ptr); } |
| 46 | + static _LIBCPP_HIDE_FROM_ABI _Tp* __to_pointer(uintptr_t __ptr) { return reinterpret_cast<_Tp*>(__ptr); } |
| 47 | +}; |
| 48 | + |
| 49 | +template <class _Tp> |
| 50 | +struct _PointerLikeTraits<_Tp*, __enable_if_t<is_void<_Tp>::value> > { |
| 51 | + static const size_t __low_bits_available = 0; |
| 52 | + |
| 53 | + static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_Tp* __ptr) { return reinterpret_cast<uintptr_t>(__ptr); } |
| 54 | + static _LIBCPP_HIDE_FROM_ABI _Tp* __to_pointer(uintptr_t __ptr) { return reinterpret_cast<_Tp*>(__ptr); } |
| 55 | +}; |
| 56 | + |
| 57 | +enum __integer_width : size_t {}; |
| 58 | + |
| 59 | +template <class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 60 | +class __pointer_int_pair { |
| 61 | + using _PointerTraits = _PointerLikeTraits<_Pointer>; |
| 62 | + |
| 63 | + static const auto __int_width = static_cast<size_t>(__int_bit_count); |
| 64 | + |
| 65 | + static_assert(__int_width <= _PointerTraits::__low_bits_available, |
| 66 | + "Not enough bits available for requested bit count"); |
| 67 | + static_assert(is_integral<_IntType>::value, "_IntType has to be an integral type"); |
| 68 | + static_assert(is_unsigned<_IntType>::value, |
| 69 | + "__pointer_int_pair doesn't work for signed types since that would require handling the sign bit"); |
| 70 | + |
| 71 | + static const size_t __extra_bits = _PointerTraits::__low_bits_available - __int_width; |
| 72 | + static const uintptr_t __int_mask = static_cast<uintptr_t>(1 << _PointerTraits::__low_bits_available) - 1; |
| 73 | + static const uintptr_t __ptr_mask = ~__int_mask; |
| 74 | + |
| 75 | + uintptr_t __value_ = 0; |
| 76 | + |
| 77 | +public: |
| 78 | + __pointer_int_pair() = default; |
| 79 | + |
| 80 | + _LIBCPP_HIDE_FROM_ABI __pointer_int_pair(_Pointer __ptr_value, _IntType __int_value) |
| 81 | + : __value_(_PointerTraits::__to_uintptr(__ptr_value) | (__int_value << __extra_bits)) { |
| 82 | + _LIBCPP_ASSERT_INTERNAL((__int_value & (__int_mask >> __extra_bits)) == __int_value, "integer is too large!"); |
| 83 | + _LIBCPP_ASSERT_INTERNAL( |
| 84 | + (_PointerTraits::__to_uintptr(__ptr_value) & __ptr_mask) == _PointerTraits::__to_uintptr(__ptr_value), |
| 85 | + "Pointer alignment is too low!"); |
| 86 | + } |
| 87 | + |
| 88 | + _LIBCPP_HIDE_FROM_ABI _IntType __get_value() const { return (__value_ & __int_mask) >> __extra_bits; } |
| 89 | + _LIBCPP_HIDE_FROM_ABI _Pointer __get_ptr() const { return _PointerTraits::__to_pointer(__value_ & __ptr_mask); } |
| 90 | + |
| 91 | + template <class, class> |
| 92 | + friend struct _PointerLikeTraits; |
| 93 | +}; |
| 94 | + |
| 95 | +template <class _Pointer, __integer_width __int_bit_count, class _IntType> |
| 96 | +struct _PointerLikeTraits<__pointer_int_pair<_Pointer, _IntType, __int_bit_count> > { |
| 97 | +private: |
| 98 | + using _PointerIntPair = __pointer_int_pair<_Pointer, _IntType, __int_bit_count>; |
| 99 | + |
| 100 | +public: |
| 101 | + static inline const size_t __low_bits_available = _PointerIntPair::__extra_bits; |
| 102 | + |
| 103 | + static _LIBCPP_HIDE_FROM_ABI uintptr_t __to_uintptr(_PointerIntPair __ptr) { return __ptr.__value_; } |
| 104 | + |
| 105 | + static _LIBCPP_HIDE_FROM_ABI _PointerIntPair __to_pointer(uintptr_t __ptr) { |
| 106 | + _PointerIntPair __tmp; |
| 107 | + __tmp.__value_ = __ptr; |
| 108 | + return __tmp; |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +// Make __pointer_int_pair tuple-like |
| 113 | + |
| 114 | +template <class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 115 | +struct tuple_size<__pointer_int_pair<_Pointer, _IntType, __int_bit_count> > : integral_constant<size_t, 2> {}; |
| 116 | + |
| 117 | +template <class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 118 | +struct tuple_element<0, __pointer_int_pair<_Pointer, _IntType, __int_bit_count> > { |
| 119 | + using type = _Pointer; |
| 120 | +}; |
| 121 | + |
| 122 | +template <class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 123 | +struct tuple_element<1, __pointer_int_pair<_Pointer, _IntType, __int_bit_count> > { |
| 124 | + using type = _IntType; |
| 125 | +}; |
| 126 | + |
| 127 | +template <size_t __i> |
| 128 | +struct __pointer_int_pair_getter; |
| 129 | + |
| 130 | +template <> |
| 131 | +struct __pointer_int_pair_getter<0> { |
| 132 | + template <class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 133 | + static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Pointer |
| 134 | + __get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) { |
| 135 | + return __pair.__get_ptr(); |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +template <> |
| 140 | +struct __pointer_int_pair_getter<1> { |
| 141 | + template <class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 142 | + static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _IntType |
| 143 | + __get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) { |
| 144 | + return __pair.__get_value(); |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +template <size_t __i, class _Pointer, class _IntType, __integer_width __int_bit_count> |
| 149 | +_LIBCPP_HIDE_FROM_ABI typename tuple_element<__i, __pointer_int_pair<_Pointer, _IntType, __int_bit_count> >::type |
| 150 | +get(__pointer_int_pair<_Pointer, _IntType, __int_bit_count> __pair) { |
| 151 | + return __pointer_int_pair_getter<__i>::__get(__pair); |
| 152 | +} |
| 153 | + |
| 154 | +_LIBCPP_END_NAMESPACE_STD |
| 155 | + |
| 156 | +#endif // _LIBCPP___UTILITY_POINTER_INT_PAIR_H |
0 commit comments