[PATCH] D41746: Make std::get_temporary_buffer respect overaligned types when possible
Chris Kennelly via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 4 16:33:38 PST 2018
ckennelly created this revision.
ckennelly added reviewers: EricWF, mclow.lists.
Repository:
rCXX libc++
https://reviews.llvm.org/D41746
Files:
include/memory
test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
Index: test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
===================================================================
--- /dev/null
+++ test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// template <class T>
+// pair<T*, ptrdiff_t>
+// get_temporary_buffer(ptrdiff_t n);
+//
+// template <class T>
+// void
+// return_temporary_buffer(T* p);
+
+#include <memory>
+#include <cassert>
+
+struct alignas(32) A {
+ int field;
+};
+
+int main()
+{
+ std::pair<A*, std::ptrdiff_t> ip = std::get_temporary_buffer<A>(5);
+ assert(!(ip.first == nullptr) ^ (ip.second == 0));
+ assert(reinterpret_cast<uintptr_t>(ip.first) % alignof(A) == 0);
+ std::return_temporary_buffer(ip.first);
+}
Index: include/memory
===================================================================
--- include/memory
+++ include/memory
@@ -2003,7 +2003,31 @@
__n = __m;
while (__n > 0)
{
+#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
+ if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ std::align_val_t __al = std::align_val_t(alignof(_Tp));
+ __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 (__alignof__(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+#else
+ if (__alignof__(_Tp) > __alignof__(std::max_align_t))
+#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;
@@ -2016,7 +2040,18 @@
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 (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
+ {
+ std::align_val_t __al = std::align_val_t(alignof(_Tp));
+ ::operator delete(__p, __al);
+ return;
+ }
+#endif
+ ::operator delete(__p);
+}
#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
template <class _Tp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41746.128674.patch
Type: text/x-patch
Size: 2910 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180105/8dbef796/attachment.bin>
More information about the cfe-commits
mailing list