Skip to content

Commit 43ca08d

Browse files
authored
[libc++] Simplify the implementation of aligned_storage (#162459)
1 parent e7bccc7 commit 43ca08d

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

libcxx/include/__config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152

153153
# ifndef _LIBCPP_CXX03_LANG
154154

155-
# define _LIBCPP_ALIGNOF(_Tp) alignof(_Tp)
155+
# define _LIBCPP_ALIGNOF(...) alignof(__VA_ARGS__)
156156
# define _ALIGNAS_TYPE(x) alignas(x)
157157
# define _ALIGNAS(x) alignas(x)
158158
# define _NOEXCEPT noexcept
@@ -161,7 +161,7 @@
161161

162162
# else
163163

164-
# define _LIBCPP_ALIGNOF(_Tp) _Alignof(_Tp)
164+
# define _LIBCPP_ALIGNOF(...) _Alignof(__VA_ARGS__)
165165
# define _ALIGNAS_TYPE(x) __attribute__((__aligned__(_LIBCPP_ALIGNOF(x))))
166166
# define _ALIGNAS(x) __attribute__((__aligned__(x)))
167167
# define nullptr __nullptr

libcxx/include/__type_traits/aligned_storage.h

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
#include <__config>
1313
#include <__cstddef/size_t.h>
14-
#include <__type_traits/integral_constant.h>
15-
#include <__type_traits/type_list.h>
1614

1715
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1816
# pragma GCC system_header
@@ -21,10 +19,10 @@
2119
_LIBCPP_BEGIN_NAMESPACE_STD
2220

2321
template <class _Tp>
24-
struct __align_type {
25-
static const size_t value = _LIBCPP_PREFERRED_ALIGNOF(_Tp);
26-
typedef _Tp type;
27-
};
22+
struct _ALIGNAS(_LIBCPP_PREFERRED_ALIGNOF(_Tp)) _AlignedAsT {};
23+
24+
template <class... _Args>
25+
struct __max_align_impl : _AlignedAsT<_Args>... {};
2826

2927
struct __struct_double {
3028
long double __lx;
@@ -33,41 +31,16 @@ struct __struct_double4 {
3331
double __lx[4];
3432
};
3533

36-
using __all_types _LIBCPP_NODEBUG =
37-
__type_list<__align_type<unsigned char>,
38-
__align_type<unsigned short>,
39-
__align_type<unsigned int>,
40-
__align_type<unsigned long>,
41-
__align_type<unsigned long long>,
42-
__align_type<double>,
43-
__align_type<long double>,
44-
__align_type<__struct_double>,
45-
__align_type<__struct_double4>,
46-
__align_type<int*> >;
47-
48-
template <class _TL, size_t _Len>
49-
struct __find_max_align;
50-
51-
template <class _Head, size_t _Len>
52-
struct __find_max_align<__type_list<_Head>, _Len> : public integral_constant<size_t, _Head::value> {};
53-
54-
template <size_t _Len, size_t _A1, size_t _A2>
55-
struct __select_align {
56-
private:
57-
static const size_t __min = _A2 < _A1 ? _A2 : _A1;
58-
static const size_t __max = _A1 < _A2 ? _A2 : _A1;
59-
60-
public:
61-
static const size_t value = _Len < __max ? __min : __max;
62-
};
34+
inline const size_t __aligned_storage_max_align =
35+
_LIBCPP_ALIGNOF(__max_align_impl<unsigned long long, double, long double, __struct_double, __struct_double4, int*>);
6336

64-
template <class _Head, class... _Tail, size_t _Len>
65-
struct __find_max_align<__type_list<_Head, _Tail...>, _Len>
66-
: public integral_constant<
67-
size_t,
68-
__select_align<_Len, _Head::value, __find_max_align<__type_list<_Tail...>, _Len>::value>::value> {};
37+
template <size_t _Len>
38+
inline const size_t __aligned_storage_alignment =
39+
_Len > __aligned_storage_max_align
40+
? __aligned_storage_max_align
41+
: size_t(1) << ((sizeof(size_t) * __CHAR_BIT__) - __builtin_clzg(_Len) - 1);
6942

70-
template <size_t _Len, size_t _Align = __find_max_align<__all_types, _Len>::value>
43+
template <size_t _Len, size_t _Align = __aligned_storage_alignment<_Len> >
7144
struct _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_NO_SPECIALIZATIONS aligned_storage {
7245
union _ALIGNAS(_Align) type {
7346
unsigned char __data[(_Len + _Align - 1) / _Align * _Align];
@@ -77,7 +50,7 @@ struct _LIBCPP_DEPRECATED_IN_CXX23 _LIBCPP_NO_SPECIALIZATIONS aligned_storage {
7750
#if _LIBCPP_STD_VER >= 14
7851

7952
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
80-
template <size_t _Len, size_t _Align = __find_max_align<__all_types, _Len>::value>
53+
template <size_t _Len, size_t _Align = __aligned_storage_alignment<_Len> >
8154
using aligned_storage_t _LIBCPP_DEPRECATED_IN_CXX23 = typename aligned_storage<_Len, _Align>::type;
8255
_LIBCPP_SUPPRESS_DEPRECATED_POP
8356

0 commit comments

Comments
 (0)