forked from OSchip/llvm-project
ADT: Rely on std::aligned_union_t for math in AlignedCharArrayUnion, NFC
Instead of computing the alignment and size of the `char` buffer in `AlignedCharArrayUnion`, rely on the math in `std::aligned_union_t`. Because some users of this rely on the `buffer` field existing with a type convertible to `char *`, we can't change the field type, but we can still avoid duplicating the logic. A potential follow up would be to delete `AlignedCharArrayUnion` after updating its users to use `std::aligned_union_t` directly; or if we like our template parameters better, could update users to stop peeking inside and then replace the definition with: ``` template <class T, class... Ts> using AlignedCharArrayUnion = std::aligned_union_t<1, T, Ts...>; ``` Differential Revision: https://reviews.llvm.org/D92500
This commit is contained in:
parent
bab72dd5d5
commit
65c5c9f92e
|
@ -13,32 +13,10 @@
|
|||
#ifndef LLVM_SUPPORT_ALIGNOF_H
|
||||
#define LLVM_SUPPORT_ALIGNOF_H
|
||||
|
||||
#include "llvm/Support/Compiler.h"
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
|
||||
namespace llvm {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename... Ts> class AlignerImpl {
|
||||
T t;
|
||||
AlignerImpl<Ts...> rest;
|
||||
AlignerImpl() = delete;
|
||||
};
|
||||
|
||||
template <typename T> class AlignerImpl<T> {
|
||||
T t;
|
||||
AlignerImpl() = delete;
|
||||
};
|
||||
|
||||
template <typename T, typename... Ts> union SizerImpl {
|
||||
char arr[sizeof(T)];
|
||||
SizerImpl<Ts...> rest;
|
||||
};
|
||||
|
||||
template <typename T> union SizerImpl<T> { char arr[sizeof(T)]; };
|
||||
} // end namespace detail
|
||||
|
||||
/// A suitably aligned and sized character array member which can hold elements
|
||||
/// of any type.
|
||||
///
|
||||
|
@ -46,8 +24,8 @@ template <typename T> union SizerImpl<T> { char arr[sizeof(T)]; };
|
|||
/// `buffer` member which can be used as suitable storage for a placement new of
|
||||
/// any of these types.
|
||||
template <typename T, typename... Ts> struct AlignedCharArrayUnion {
|
||||
alignas(::llvm::detail::AlignerImpl<T, Ts...>) char buffer[sizeof(
|
||||
llvm::detail::SizerImpl<T, Ts...>)];
|
||||
using AlignedUnion = std::aligned_union_t<1, T, Ts...>;
|
||||
alignas(alignof(AlignedUnion)) char buffer[sizeof(AlignedUnion)];
|
||||
};
|
||||
|
||||
} // end namespace llvm
|
||||
|
|
Loading…
Reference in New Issue