| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H |
| 11 | #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H |
| 12 | |
| 13 | #include <__config> |
| 14 | #include <__cstddef/size_t.h> |
| 15 | #include <__type_traits/datasizeof.h> |
| 16 | #include <__type_traits/is_empty.h> |
| 17 | #include <__type_traits/is_final.h> |
| 18 | |
| 19 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 20 | # pragma GCC system_header |
| 21 | #endif |
| 22 | |
| 23 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 24 | |
| 25 | // ================================================================================================================== // |
| 26 | // The utilites here are for staying ABI compatible with the legacy `__compressed_pair`. They should not be used // |
| 27 | // for new data structures. Use `_LIBCPP_NO_UNIQUE_ADDRESS` for new data structures instead (but make sure you // |
| 28 | // understand how it works). // |
| 29 | // ================================================================================================================== // |
| 30 | |
| 31 | // On GCC, the first member is aligned to the alignment of the second member to force padding in front of the compressed |
| 32 | // pair in case there are members before it. |
| 33 | // |
| 34 | // For example: |
| 35 | // (assuming x86-64 linux) |
| 36 | // class SomeClass { |
| 37 | // uint32_t member1; |
| 38 | // _LIBCPP_COMPRESSED_PAIR(uint32_t, member2, uint64_t, member3); |
| 39 | // } |
| 40 | // |
| 41 | // The layout with __compressed_pair is: |
| 42 | // member1 - offset: 0, size: 4 |
| 43 | // padding - offset: 4, size: 4 |
| 44 | // member2 - offset: 8, size: 4 |
| 45 | // padding - offset: 12, size: 4 |
| 46 | // member3 - offset: 16, size: 8 |
| 47 | // |
| 48 | // If the [[gnu::aligned]] wasn't there, the layout would instead be: |
| 49 | // member1 - offset: 0, size: 4 |
| 50 | // member2 - offset: 4, size: 4 |
| 51 | // member3 - offset: 8, size: 8 |
| 52 | // |
| 53 | // Furthermore, that alignment must be the same as what was used in the old __compressed_pair layout, so we must |
| 54 | // handle reference types specially since alignof(T&) == alignof(T). |
| 55 | // See https://llvm.org/PR118559. |
| 56 | // |
| 57 | // On Clang, this is unnecessary, since we use anonymous structs instead, which automatically handle the alignment |
| 58 | // correctly. |
| 59 | |
| 60 | #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING |
| 61 | |
| 62 | template <class _Tp> |
| 63 | inline const size_t __compressed_pair_alignment = _LIBCPP_ALIGNOF(_Tp); |
| 64 | |
| 65 | template <class _Tp> |
| 66 | inline const size_t __compressed_pair_alignment<_Tp&> = _LIBCPP_ALIGNOF(void*); |
| 67 | |
| 68 | template <class _ToPad> |
| 69 | inline const bool __is_reference_or_unpadded_object = |
| 70 | (is_empty<_ToPad>::value && !__is_final_v<_ToPad>) || sizeof(_ToPad) == __datasizeof_v<_ToPad>; |
| 71 | |
| 72 | template <class _Tp> |
| 73 | inline const bool __is_reference_or_unpadded_object<_Tp&> = true; |
| 74 | |
| 75 | template <class _Tp> |
| 76 | inline const bool __is_reference_or_unpadded_object<_Tp&&> = true; |
| 77 | |
| 78 | template <class _ToPad, bool _Empty = __is_reference_or_unpadded_object<_ToPad> > |
| 79 | class __compressed_pair_padding { |
| 80 | char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>] = {}; |
| 81 | }; |
| 82 | |
| 83 | template <class _ToPad> |
| 84 | class __compressed_pair_padding<_ToPad, true> {}; |
| 85 | |
| 86 | # define _LIBCPP_COMPRESSED_ELEMENT(T1, Initializer1) \ |
| 87 | _LIBCPP_NO_UNIQUE_ADDRESS T1 Initializer1; \ |
| 88 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding_, __LINE__, _) |
| 89 | |
| 90 | // TODO: Fix the ABI for GCC as well once https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121637 is fixed |
| 91 | # ifdef _LIBCPP_COMPILER_GCC |
| 92 | # define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2) \ |
| 93 | _LIBCPP_NO_UNIQUE_ADDRESS __attribute__((__aligned__(::std::__compressed_pair_alignment<T2>))) T1 Initializer1; \ |
| 94 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \ |
| 95 | _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \ |
| 96 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _) |
| 97 | |
| 98 | # define _LIBCPP_COMPRESSED_TRIPLE(T1, Initializer1, T2, Initializer2, T3, Initializer3) \ |
| 99 | _LIBCPP_NO_UNIQUE_ADDRESS \ |
| 100 | __attribute__((__aligned__(::std::__compressed_pair_alignment<T2>), \ |
| 101 | __aligned__(::std::__compressed_pair_alignment<T3>))) T1 Initializer1; \ |
| 102 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \ |
| 103 | _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \ |
| 104 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _); \ |
| 105 | _LIBCPP_NO_UNIQUE_ADDRESS T3 Initializer3; \ |
| 106 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T3> _LIBCPP_CONCAT3(__padding3_, __LINE__, _) |
| 107 | # else |
| 108 | # define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2) \ |
| 109 | struct { \ |
| 110 | _LIBCPP_NO_UNIQUE_ADDRESS T1 Initializer1; \ |
| 111 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \ |
| 112 | _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \ |
| 113 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _); \ |
| 114 | } |
| 115 | |
| 116 | # define _LIBCPP_COMPRESSED_TRIPLE(T1, Initializer1, T2, Initializer2, T3, Initializer3) \ |
| 117 | struct { \ |
| 118 | _LIBCPP_NO_UNIQUE_ADDRESS T1 Initializer1; \ |
| 119 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \ |
| 120 | _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \ |
| 121 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _); \ |
| 122 | _LIBCPP_NO_UNIQUE_ADDRESS T3 Initializer3; \ |
| 123 | _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T3> _LIBCPP_CONCAT3(__padding3_, __LINE__, _); \ |
| 124 | } |
| 125 | # endif |
| 126 | |
| 127 | #else |
| 128 | # define _LIBCPP_COMPRESSED_ELEMENT(T1, Initializer1) _LIBCPP_NO_UNIQUE_ADDRESS T1 Initializer1 |
| 129 | |
| 130 | # define _LIBCPP_COMPRESSED_PAIR(T1, Name1, T2, Name2) \ |
| 131 | _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1; \ |
| 132 | _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2 |
| 133 | |
| 134 | # define _LIBCPP_COMPRESSED_TRIPLE(T1, Name1, T2, Name2, T3, Name3) \ |
| 135 | _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1; \ |
| 136 | _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2; \ |
| 137 | _LIBCPP_NO_UNIQUE_ADDRESS T3 Name3 |
| 138 | #endif // _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING |
| 139 | |
| 140 | _LIBCPP_END_NAMESPACE_STD |
| 141 | |
| 142 | #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H |
| 143 | |