Commit 8b3a565d authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Fix folly::max_align_v for Clang on ARMv7

Summary:
[Folly] Fix `folly::max_align_v` for Clang on ARMv7.

There is some problem with computing the correct result of `alignof` for a compound `union` POD type, because why wouldn't there be. The result *should* just be the max of the `alignof` of the type of each field if each field is default aligned - but not on some platforms! So we must compute the max directly.

Reviewed By: mzlee, Orvid

Differential Revision: D6573548

fbshipit-source-id: 512a255fda64795104d71fde14372befa3bf41e4
parent 6ae2206d
...@@ -22,22 +22,34 @@ namespace folly { ...@@ -22,22 +22,34 @@ namespace folly {
namespace detail { namespace detail {
union max_align_ { // Implemented this way because of a bug in Clang for ARMv7, which gives the
std::max_align_t mat; // wrong result for `alignof` a `union` with a field of each scalar type.
long double ld; constexpr size_t max_align_(std::size_t a) {
double d; return a;
float f; }
long long int lli; template <typename... Es>
long int li; constexpr std::size_t max_align_(std::size_t a, std::size_t e, Es... es) {
int i; return !(a < e) ? a : max_align_(e, es...);
short int si; }
bool b; template <typename... Ts>
char c; struct max_align_t_ {
char16_t c16; static constexpr std::size_t value = max_align_(0u, alignof(Ts)...);
char32_t c32;
wchar_t wc;
std::nullptr_t n;
}; };
using max_align_v_ = max_align_t_<
long double,
double,
float,
long long int,
long int,
int,
short int,
bool,
char,
char16_t,
char32_t,
wchar_t,
void*,
std::max_align_t>;
} // namespace detail } // namespace detail
...@@ -73,7 +85,7 @@ union max_align_ { ...@@ -73,7 +85,7 @@ union max_align_ {
// crashes on 32-bit iOS apps that use `double`. // crashes on 32-bit iOS apps that use `double`.
// //
// Apple's allocation reference: http://bit.ly/malloc-small // Apple's allocation reference: http://bit.ly/malloc-small
constexpr std::size_t max_align_v = alignof(detail::max_align_); constexpr std::size_t max_align_v = detail::max_align_v_::value;
struct alignas(max_align_v) max_align_t {}; struct alignas(max_align_v) max_align_t {};
} // namespace folly } // namespace folly
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment