Fix PR22634 - std::allocator doesn't respect over-aligned types.

This patch fixes std::allocator, and more specifically, all users
of __libcpp_allocate and __libcpp_deallocate, to support over-aligned
types.

__libcpp_allocate/deallocate now take an alignment parameter, and when
the specified alignment is greater than that supported by malloc/new,
the aligned version of operator new is called (assuming it's available).

When aligned new isn't available, the old behavior has been kept, and the
alignment parameter is ignored.

This patch depends on recent changes to __builtin_operator_new/delete which
allow them to be used to call any regular new/delete operator. By using
__builtin_operator_new/delete when possible, the new/delete erasure optimization
is maintained.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@328180 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/new b/include/new
index 27c248c..e42ffad 100644
--- a/include/new
+++ b/include/new
@@ -89,6 +89,7 @@
 
 #include <__config>
 #include <exception>
+#include <type_traits>
 #include <cstddef>
 #ifdef _LIBCPP_NO_EXCEPTIONS
 #include <cstdlib>
@@ -113,6 +114,14 @@
 # define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
 #endif
 
+
+#if !__has_builtin(__builtin_operator_new) || \
+   __has_builtin(__builtin_operator_new) < 201802L || \
+   defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || \
+   !defined(__cpp_aligned_new) || __cpp_aligned_new < 201606
+#define _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
+#endif
+
 namespace std  // purposefully not using versioning namespace
 {
 
@@ -223,7 +232,27 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
+_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
+#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
+  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
+#else
+  return __align > alignment_of<max_align_t>::value;
+#endif
+}
+
+inline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {
+#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
+  if (__is_overaligned_for_new(__align)) {
+    const align_val_t __align_val = static_cast<align_val_t>(__align);
+# ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
+    return ::operator new(__size, __align_val);
+# else
+    return __builtin_operator_new(__size, __align_val);
+# endif
+  }
+#else
+  ((void)__align);
+#endif
 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
   return ::operator new(__size);
 #else
@@ -231,11 +260,23 @@
 #endif
 }
 
-inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void *__ptr) {
-#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
-  ::operator delete(__ptr);
+inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __align) {
+#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
+  if (__is_overaligned_for_new(__align)) {
+    const align_val_t __align_val = static_cast<align_val_t>(__align);
+# ifdef _LIBCPP_HAS_NO_BUILTIN_ALIGNED_OPERATOR_NEW_DELETE
+    return ::operator delete(__ptr, __align_val);
+# else
+    return __builtin_operator_delete(__ptr, __align_val);
+# endif
+  }
 #else
-  __builtin_operator_delete(__ptr);
+  ((void)__align);
+#endif
+#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
+  return ::operator delete(__ptr);
+#else
+  return __builtin_operator_delete(__ptr);
 #endif
 }