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 #include <__type_traits/is_reference.h> 19 20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 21 # pragma GCC system_header 22 #endif 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 // ================================================================================================================== // 27 // The utilites here are for staying ABI compatible with the legacy `__compressed_pair`. They should not be used // 28 // for new data structures. Use `_LIBCPP_NO_UNIQUE_ADDRESS` for new data structures instead (but make sure you // 29 // understand how it works). // 30 // ================================================================================================================== // 31 32 // The first member is aligned to the alignment of the second member to force padding in front of the compressed pair 33 // in case there are members before it. 34 // 35 // For example: 36 // (assuming x86-64 linux) 37 // class SomeClass { 38 // uint32_t member1; 39 // _LIBCPP_COMPRESSED_PAIR(uint32_t, member2, uint64_t, member3); 40 // } 41 // 42 // The layout with __compressed_pair is: 43 // member1 - offset: 0, size: 4 44 // padding - offset: 4, size: 4 45 // member2 - offset: 8, size: 4 46 // padding - offset: 12, size: 4 47 // member3 - offset: 16, size: 8 48 // 49 // If the [[gnu::aligned]] wasn't there, the layout would instead be: 50 // member1 - offset: 0, size: 4 51 // member2 - offset: 4, size: 4 52 // member3 - offset: 8, size: 8 53 // 54 // Furthermore, that alignment must be the same as what was used in the old __compressed_pair layout, so we must 55 // handle reference types specially since alignof(T&) == alignof(T). 56 // See https://github.com/llvm/llvm-project/issues/118559. 57 58 #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING 59 60 template <class _Tp> 61 inline const size_t __compressed_pair_alignment = _LIBCPP_ALIGNOF(_Tp); 62 63 template <class _Tp> 64 inline const size_t __compressed_pair_alignment<_Tp&> = _LIBCPP_ALIGNOF(void*); 65 66 template <class _ToPad, 67 bool _Empty = ((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) || 68 is_reference<_ToPad>::value || sizeof(_ToPad) == __datasizeof_v<_ToPad>)> 69 class __compressed_pair_padding { 70 char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>] = {}; 71 }; 72 73 template <class _ToPad> 74 class __compressed_pair_padding<_ToPad, true> {}; 75 76 # define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2) \ 77 _LIBCPP_NO_UNIQUE_ADDRESS __attribute__((__aligned__(::std::__compressed_pair_alignment<T2>))) T1 Initializer1; \ 78 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \ 79 _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \ 80 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _) 81 82 # define _LIBCPP_COMPRESSED_TRIPLE(T1, Initializer1, T2, Initializer2, T3, Initializer3) \ 83 _LIBCPP_NO_UNIQUE_ADDRESS \ 84 __attribute__((__aligned__(::std::__compressed_pair_alignment<T2>), \ 85 __aligned__(::std::__compressed_pair_alignment<T3>))) T1 Initializer1; \ 86 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \ 87 _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \ 88 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _); \ 89 _LIBCPP_NO_UNIQUE_ADDRESS T3 Initializer3; \ 90 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T3> _LIBCPP_CONCAT3(__padding3_, __LINE__, _) 91 92 #else 93 # define _LIBCPP_COMPRESSED_PAIR(T1, Name1, T2, Name2) \ 94 _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1; \ 95 _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2 96 97 # define _LIBCPP_COMPRESSED_TRIPLE(T1, Name1, T2, Name2, T3, Name3) \ 98 _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1; \ 99 _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2; \ 100 _LIBCPP_NO_UNIQUE_ADDRESS T3 Name3 101 #endif // _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING 102 103 _LIBCPP_END_NAMESPACE_STD 104 105 #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H 106