Make std::get_temporary_buffer respect overaligned types when possible

Patch by Chris Kennelly!

Differential Revision: https://reviews.llvm.org/D41746


git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@324020 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Richard Smith
2018-02-01 22:24:45 +00:00
parent fd34566e5a
commit dfb1351077
2 changed files with 81 additions and 1 deletions

View File

@@ -2004,7 +2004,38 @@ get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
__n = __m;
while (__n > 0)
{
#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
if (std::alignment_of<_Tp>::value > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
#else
if (std::alignment_of<_Tp>::value >
std::alignment_of<std::max_align_t>::value)
#endif
{
std::align_val_t __al =
std::align_val_t(std::alignment_of<_Tp>::value);
__r.first = static_cast<_Tp*>(::operator new(
__n * sizeof(_Tp), __al, nothrow));
} else {
__r.first = static_cast<_Tp*>(::operator new(
__n * sizeof(_Tp), nothrow));
}
#else
#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
if (std::alignment_of<_Tp>::value > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
#else
if (std::alignment_of<_Tp>::value >
std::alignment_of<std::max_align_t>::value)
#endif
{
// Since aligned operator new is unavailable, return an empty
// buffer rather than one with invalid alignment.
return __r;
}
__r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
#endif
if (__r.first)
{
__r.second = __n;
@@ -2017,7 +2048,23 @@ get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void return_temporary_buffer(_Tp* __p) _NOEXCEPT {::operator delete(__p);}
void return_temporary_buffer(_Tp* __p) _NOEXCEPT
{
#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
if (std::alignment_of<_Tp>::value > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
#else
if (std::alignment_of<_Tp>::value >
std::alignment_of<std::max_align_t>::value)
#endif
{
std::align_val_t __al = std::align_val_t(std::alignment_of<_Tp>::value);
::operator delete(__p, __al);
return;
}
#endif
::operator delete(__p);
}
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
template <class _Tp>