Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:56

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H
0011 #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H
0012 
0013 #include <__config>
0014 #include <__cstddef/size_t.h>
0015 #include <__type_traits/datasizeof.h>
0016 #include <__type_traits/is_empty.h>
0017 #include <__type_traits/is_final.h>
0018 #include <__type_traits/is_reference.h>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025 
0026 // ================================================================================================================== //
0027 // The utilites here are for staying ABI compatible with the legacy `__compressed_pair`. They should not be used      //
0028 // for new data structures. Use `_LIBCPP_NO_UNIQUE_ADDRESS` for new data structures instead (but make sure you        //
0029 // understand how it works).                                                                                          //
0030 // ================================================================================================================== //
0031 
0032 // The first member is aligned to the alignment of the second member to force padding in front of the compressed pair
0033 // in case there are members before it.
0034 //
0035 // For example:
0036 // (assuming x86-64 linux)
0037 // class SomeClass {
0038 //   uint32_t member1;
0039 //   _LIBCPP_COMPRESSED_PAIR(uint32_t, member2, uint64_t, member3);
0040 // }
0041 //
0042 // The layout with __compressed_pair is:
0043 // member1 - offset: 0,  size: 4
0044 // padding - offset: 4,  size: 4
0045 // member2 - offset: 8,  size: 4
0046 // padding - offset: 12, size: 4
0047 // member3 - offset: 16, size: 8
0048 //
0049 // If the [[gnu::aligned]] wasn't there, the layout would instead be:
0050 // member1 - offset: 0, size: 4
0051 // member2 - offset: 4, size: 4
0052 // member3 - offset: 8, size: 8
0053 //
0054 // Furthermore, that alignment must be the same as what was used in the old __compressed_pair layout, so we must
0055 // handle reference types specially since alignof(T&) == alignof(T).
0056 // See https://github.com/llvm/llvm-project/issues/118559.
0057 
0058 #ifndef _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
0059 
0060 template <class _Tp>
0061 inline const size_t __compressed_pair_alignment = _LIBCPP_ALIGNOF(_Tp);
0062 
0063 template <class _Tp>
0064 inline const size_t __compressed_pair_alignment<_Tp&> = _LIBCPP_ALIGNOF(void*);
0065 
0066 template <class _ToPad,
0067           bool _Empty = ((is_empty<_ToPad>::value && !__libcpp_is_final<_ToPad>::value) ||
0068                          is_reference<_ToPad>::value || sizeof(_ToPad) == __datasizeof_v<_ToPad>)>
0069 class __compressed_pair_padding {
0070   char __padding_[sizeof(_ToPad) - __datasizeof_v<_ToPad>] = {};
0071 };
0072 
0073 template <class _ToPad>
0074 class __compressed_pair_padding<_ToPad, true> {};
0075 
0076 #  define _LIBCPP_COMPRESSED_PAIR(T1, Initializer1, T2, Initializer2)                                                  \
0077     _LIBCPP_NO_UNIQUE_ADDRESS __attribute__((__aligned__(::std::__compressed_pair_alignment<T2>))) T1 Initializer1;    \
0078     _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);          \
0079     _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2;                                                                         \
0080     _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _)
0081 
0082 #  define _LIBCPP_COMPRESSED_TRIPLE(T1, Initializer1, T2, Initializer2, T3, Initializer3)                              \
0083     _LIBCPP_NO_UNIQUE_ADDRESS                                                                                          \
0084     __attribute__((__aligned__(::std::__compressed_pair_alignment<T2>),                                                \
0085                    __aligned__(::std::__compressed_pair_alignment<T3>))) T1 Initializer1;                              \
0086     _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);          \
0087     _LIBCPP_NO_UNIQUE_ADDRESS T2 Initializer2;                                                                         \
0088     _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T2> _LIBCPP_CONCAT3(__padding2_, __LINE__, _);          \
0089     _LIBCPP_NO_UNIQUE_ADDRESS T3 Initializer3;                                                                         \
0090     _LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T3> _LIBCPP_CONCAT3(__padding3_, __LINE__, _)
0091 
0092 #else
0093 #  define _LIBCPP_COMPRESSED_PAIR(T1, Name1, T2, Name2)                                                                \
0094     _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1;                                                                                \
0095     _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2
0096 
0097 #  define _LIBCPP_COMPRESSED_TRIPLE(T1, Name1, T2, Name2, T3, Name3)                                                   \
0098     _LIBCPP_NO_UNIQUE_ADDRESS T1 Name1;                                                                                \
0099     _LIBCPP_NO_UNIQUE_ADDRESS T2 Name2;                                                                                \
0100     _LIBCPP_NO_UNIQUE_ADDRESS T3 Name3
0101 #endif // _LIBCPP_ABI_NO_COMPRESSED_PAIR_PADDING
0102 
0103 _LIBCPP_END_NAMESPACE_STD
0104 
0105 #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H