2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H
11 #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H
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>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
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 // ================================================================================================================== //
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.
36 // (assuming x86-64 linux)
39 // _LIBCPP_COMPRESSED_PAIR(uint32_t, member2, uint64_t, member3);
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
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
54 #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
56 template <class _ToPad
,
57 bool _Empty
= ((is_empty
<_ToPad
>::value
&& !__libcpp_is_final
<_ToPad
>::value
) ||
58 is_reference
<_ToPad
>::value
|| sizeof(_ToPad
) == __datasizeof_v
<_ToPad
>)>
59 class __compressed_pair_padding
{
60 char __padding_
[sizeof(_ToPad
) - __datasizeof_v
<_ToPad
>] = {};
63 template <class _ToPad
>
64 class __compressed_pair_padding
<_ToPad
, true> {};
66 # define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2) \
67 _LIBCPP_NO_UNIQUE_ADDRESS __attribute__((__aligned__(_LIBCPP_ALIGNOF(T2)))) T1 Initializer1; \
68 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \
69 _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \
70 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _)
72 # define _LIBCPP_COMPRESSED_TRIPLE(T1, Initializer1, T2, Initializer2, T3, Initializer3) \
73 _LIBCPP_NO_UNIQUE_ADDRESS \
74 __attribute__((__aligned__(_LIBCPP_ALIGNOF(T2)), __aligned__(_LIBCPP_ALIGNOF(T3)))) T1 Initializer1; \
75 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _); \
76 _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2; \
77 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _); \
78 _LIBCPP_NO_UNIQUE_ADDRESS T3 Initializer3; \
79 _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T3> _LIBCPP_CONCAT3(__padding3_, __LINE__, _)
82 # define _LIBCPP_COMPRESSED_PAIR(T1, Name1, T2, Name2) \
83 _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1; \
84 _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2
86 # define _LIBCPP_COMPRESSED_TRIPLE(T1, Name1, T2, Name2, T3, Name3) \
87 _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1; \
88 _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2; \
89 _LIBCPP_NO_UNIQUE_ADDRESS T3 Name3
90 #endif // _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
92 _LIBCPP_END_NAMESPACE_STD
94 #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H