[libcxx-commits] [libcxx] [libc++] Granularize the <new> header (PR #119270)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 10 07:38:08 PST 2024
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/119270
>From c90284d11d9466fd4f56b534081807b4bcbb31de Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Mon, 9 Dec 2024 14:33:21 -0500
Subject: [PATCH] [libc++] Granularize the <new> header
---
libcxx/include/CMakeLists.txt | 9 +
libcxx/include/__memory/allocator.h | 1 +
.../include/__memory/builtin_new_allocator.h | 1 +
libcxx/include/__memory/shared_ptr.h | 1 +
.../__memory/uninitialized_algorithms.h | 1 +
libcxx/include/__new/align_val_t.h | 30 ++
libcxx/include/__new/allocate.h | 101 +++++++
libcxx/include/__new/destroying_delete_t.h | 30 ++
libcxx/include/__new/exceptions.h | 74 +++++
libcxx/include/__new/interference_size.h | 34 +++
libcxx/include/__new/launder.h | 36 +++
libcxx/include/__new/new_handler.h | 29 ++
libcxx/include/__new/nothrow_t.h | 30 ++
libcxx/include/__new/operators.h | 100 ++++++
libcxx/include/__utility/small_buffer.h | 1 +
libcxx/include/future | 1 +
libcxx/include/module.modulemap | 10 +
libcxx/include/new | 284 +-----------------
18 files changed, 503 insertions(+), 270 deletions(-)
create mode 100644 libcxx/include/__new/align_val_t.h
create mode 100644 libcxx/include/__new/allocate.h
create mode 100644 libcxx/include/__new/destroying_delete_t.h
create mode 100644 libcxx/include/__new/exceptions.h
create mode 100644 libcxx/include/__new/interference_size.h
create mode 100644 libcxx/include/__new/launder.h
create mode 100644 libcxx/include/__new/new_handler.h
create mode 100644 libcxx/include/__new/nothrow_t.h
create mode 100644 libcxx/include/__new/operators.h
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 561ce4b42fb250..c9ba28ad59b673 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -581,6 +581,15 @@ set(files
__mutex/once_flag.h
__mutex/tag_types.h
__mutex/unique_lock.h
+ __new/align_val_t.h
+ __new/allocate.h
+ __new/destroying_delete_t.h
+ __new/exceptions.h
+ __new/interference_size.h
+ __new/launder.h
+ __new/new_handler.h
+ __new/nothrow_t.h
+ __new/operators.h
__node_handle
__numeric/accumulate.h
__numeric/adjacent_difference.h
diff --git a/libcxx/include/__memory/allocator.h b/libcxx/include/__memory/allocator.h
index ddb4179940b8b1..1fcaf6402667e1 100644
--- a/libcxx/include/__memory/allocator.h
+++ b/libcxx/include/__memory/allocator.h
@@ -12,6 +12,7 @@
#include <__config>
#include <__cstddef/ptrdiff_t.h>
+#include <__cstddef/size_t.h>
#include <__memory/addressof.h>
#include <__memory/allocate_at_least.h>
#include <__memory/allocator_traits.h>
diff --git a/libcxx/include/__memory/builtin_new_allocator.h b/libcxx/include/__memory/builtin_new_allocator.h
index 128288efb05bc1..b1611b12bff03e 100644
--- a/libcxx/include/__memory/builtin_new_allocator.h
+++ b/libcxx/include/__memory/builtin_new_allocator.h
@@ -10,6 +10,7 @@
#define _LIBCPP___MEMORY_BUILTIN_NEW_ALLOCATOR_H
#include <__config>
+#include <__cstddef/size_t.h>
#include <__memory/unique_ptr.h>
#include <new>
diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h
index 5c34f2efc5730e..4fa75af4ccc86d 100644
--- a/libcxx/include/__memory/shared_ptr.h
+++ b/libcxx/include/__memory/shared_ptr.h
@@ -43,6 +43,7 @@
#include <__type_traits/is_bounded_array.h>
#include <__type_traits/is_constructible.h>
#include <__type_traits/is_convertible.h>
+#include <__type_traits/is_function.h>
#include <__type_traits/is_reference.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_unbounded_array.h>
diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h
index 71c7ed94fec13e..25d192c7718857 100644
--- a/libcxx/include/__memory/uninitialized_algorithms.h
+++ b/libcxx/include/__memory/uninitialized_algorithms.h
@@ -15,6 +15,7 @@
#include <__algorithm/unwrap_iter.h>
#include <__algorithm/unwrap_range.h>
#include <__config>
+#include <__cstddef/size_t.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/reverse_iterator.h>
#include <__memory/addressof.h>
diff --git a/libcxx/include/__new/align_val_t.h b/libcxx/include/__new/align_val_t.h
new file mode 100644
index 00000000000000..ffb4e36a8bcd88
--- /dev/null
+++ b/libcxx/include/__new/align_val_t.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_ALIGN_VAL_T_H
+#define _LIBCPP___NEW_ALIGN_VAL_T_H
+
+#include <__config>
+#include <__cstddef/size_t.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+// purposefully not using versioning namespace
+namespace std {
+#if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION && !defined(_LIBCPP_ABI_VCRUNTIME)
+# ifndef _LIBCPP_CXX03_LANG
+enum class align_val_t : size_t {};
+# else
+enum align_val_t { __zero = 0, __max = (size_t)-1 };
+# endif
+#endif
+} // namespace std
+
+#endif // _LIBCPP___NEW_ALIGN_VAL_T_H
diff --git a/libcxx/include/__new/allocate.h b/libcxx/include/__new/allocate.h
new file mode 100644
index 00000000000000..7ea7662c817ad4
--- /dev/null
+++ b/libcxx/include/__new/allocate.h
@@ -0,0 +1,101 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_ALLOCATE_H
+#define _LIBCPP___NEW_ALLOCATE_H
+
+#include <__config>
+#include <__cstddef/max_align_t.h>
+#include <__cstddef/size_t.h>
+#include <__new/align_val_t.h>
+#include <__new/operators.h> // for _LIBCPP_HAS_SIZED_DEALLOCATION
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
+#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
+ return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
+#else
+ return __align > _LIBCPP_ALIGNOF(max_align_t);
+#endif
+}
+
+template <class... _Args>
+_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
+#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
+ return __builtin_operator_new(__args...);
+#else
+ return ::operator new(__args...);
+#endif
+}
+
+template <class... _Args>
+_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT {
+#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
+ __builtin_operator_delete(__args...);
+#else
+ ::operator delete(__args...);
+#endif
+}
+
+inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) {
+#if _LIBCPP_HAS_ALIGNED_ALLOCATION
+ if (__is_overaligned_for_new(__align)) {
+ const align_val_t __align_val = static_cast<align_val_t>(__align);
+ return __libcpp_operator_new(__size, __align_val);
+ }
+#endif
+
+ (void)__align;
+ return __libcpp_operator_new(__size);
+}
+
+template <class... _Args>
+_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) _NOEXCEPT {
+#if !_LIBCPP_HAS_SIZED_DEALLOCATION
+ (void)__size;
+ return std::__libcpp_operator_delete(__ptr, __args...);
+#else
+ return std::__libcpp_operator_delete(__ptr, __size, __args...);
+#endif
+}
+
+inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) _NOEXCEPT {
+#if !_LIBCPP_HAS_ALIGNED_ALLOCATION
+ (void)__align;
+ return __do_deallocate_handle_size(__ptr, __size);
+#else
+ if (__is_overaligned_for_new(__align)) {
+ const align_val_t __align_val = static_cast<align_val_t>(__align);
+ return __do_deallocate_handle_size(__ptr, __size, __align_val);
+ } else {
+ return __do_deallocate_handle_size(__ptr, __size);
+ }
+#endif
+}
+
+inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) _NOEXCEPT {
+#if !_LIBCPP_HAS_ALIGNED_ALLOCATION
+ (void)__align;
+ return __libcpp_operator_delete(__ptr);
+#else
+ if (__is_overaligned_for_new(__align)) {
+ const align_val_t __align_val = static_cast<align_val_t>(__align);
+ return __libcpp_operator_delete(__ptr, __align_val);
+ } else {
+ return __libcpp_operator_delete(__ptr);
+ }
+#endif
+}
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___NEW_ALLOCATE_H
diff --git a/libcxx/include/__new/destroying_delete_t.h b/libcxx/include/__new/destroying_delete_t.h
new file mode 100644
index 00000000000000..7fca4f6c68b212
--- /dev/null
+++ b/libcxx/include/__new/destroying_delete_t.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_DESTROYING_DELETE_T_H
+#define _LIBCPP___NEW_DESTROYING_DELETE_T_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 20
+// purposefully not using versioning namespace
+namespace std {
+// Enable the declaration even if the compiler doesn't support the language
+// feature.
+struct destroying_delete_t {
+ explicit destroying_delete_t() = default;
+};
+inline constexpr destroying_delete_t destroying_delete{};
+} // namespace std
+#endif
+
+#endif // _LIBCPP___NEW_DESTROYING_DELETE_T_H
diff --git a/libcxx/include/__new/exceptions.h b/libcxx/include/__new/exceptions.h
new file mode 100644
index 00000000000000..053feecb036787
--- /dev/null
+++ b/libcxx/include/__new/exceptions.h
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_EXCEPTIONS_H
+#define _LIBCPP___NEW_EXCEPTIONS_H
+
+#include <__config>
+#include <__exception/exception.h>
+#include <__verbose_abort>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+// purposefully not using versioning namespace
+namespace std {
+#if !defined(_LIBCPP_ABI_VCRUNTIME)
+
+class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
+public:
+ bad_alloc() _NOEXCEPT;
+ _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
+ _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
+ ~bad_alloc() _NOEXCEPT override;
+ const char* what() const _NOEXCEPT override;
+};
+
+class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
+public:
+ bad_array_new_length() _NOEXCEPT;
+ _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
+ _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
+ ~bad_array_new_length() _NOEXCEPT override;
+ const char* what() const _NOEXCEPT override;
+};
+
+#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
+
+// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
+// since they would normally be provided in vcruntime_exception.h
+class bad_alloc : public exception {
+public:
+ bad_alloc() noexcept : exception("bad allocation") {}
+
+private:
+ friend class bad_array_new_length;
+
+ bad_alloc(char const* const __message) noexcept : exception(__message) {}
+};
+
+class bad_array_new_length : public bad_alloc {
+public:
+ bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
+};
+
+#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
+
+[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
+
+[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() {
+#if _LIBCPP_HAS_EXCEPTIONS
+ throw bad_array_new_length();
+#else
+ _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
+#endif
+}
+} // namespace std
+
+#endif // _LIBCPP___NEW_EXCEPTIONS_H
diff --git a/libcxx/include/__new/interference_size.h b/libcxx/include/__new/interference_size.h
new file mode 100644
index 00000000000000..d326c43a332034
--- /dev/null
+++ b/libcxx/include/__new/interference_size.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_INTERFERENCE_SIZE_H
+#define _LIBCPP___NEW_INTERFERENCE_SIZE_H
+
+#include <__config>
+#include <__cstddef/size_t.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 17
+
+# if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
+
+inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
+inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
+
+# endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
+
+#endif // _LIBCPP_STD_VER >= 17
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___NEW_INTERFERENCE_SIZE_H
diff --git a/libcxx/include/__new/launder.h b/libcxx/include/__new/launder.h
new file mode 100644
index 00000000000000..83d80015913d96
--- /dev/null
+++ b/libcxx/include/__new/launder.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_LAUNDER_H
+#define _LIBCPP___NEW_LAUNDER_H
+
+#include <__config>
+#include <__type_traits/is_function.h>
+#include <__type_traits/is_void.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+template <class _Tp>
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT {
+ static_assert(!(is_function<_Tp>::value), "can't launder functions");
+ static_assert(!is_void<_Tp>::value, "can't launder cv-void");
+ return __builtin_launder(__p);
+}
+
+#if _LIBCPP_STD_VER >= 17
+template <class _Tp>
+[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp* launder(_Tp* __p) noexcept {
+ return std::__launder(__p);
+}
+#endif
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___NEW_LAUNDER_H
diff --git a/libcxx/include/__new/new_handler.h b/libcxx/include/__new/new_handler.h
new file mode 100644
index 00000000000000..c9afdab45afc13
--- /dev/null
+++ b/libcxx/include/__new/new_handler.h
@@ -0,0 +1,29 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_NEW_HANDLER_H
+#define _LIBCPP___NEW_NEW_HANDLER_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if defined(_LIBCPP_ABI_VCRUNTIME)
+# include <new.h>
+#else
+// purposefully not using versioning namespace
+namespace std {
+typedef void (*new_handler)();
+_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
+_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
+} // namespace std
+#endif // _LIBCPP_ABI_VCRUNTIME
+
+#endif // _LIBCPP___NEW_NEW_HANDLER_H
diff --git a/libcxx/include/__new/nothrow_t.h b/libcxx/include/__new/nothrow_t.h
new file mode 100644
index 00000000000000..09c2d03f66ccc4
--- /dev/null
+++ b/libcxx/include/__new/nothrow_t.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_NOTHROW_T_H
+#define _LIBCPP___NEW_NOTHROW_T_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if defined(_LIBCPP_ABI_VCRUNTIME)
+# include <new.h>
+#else
+// purposefully not using versioning namespace
+namespace std {
+struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
+ explicit nothrow_t() = default;
+};
+extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
+} // namespace std
+#endif // _LIBCPP_ABI_VCRUNTIME
+
+#endif // _LIBCPP___NEW_NOTHROW_T_H
diff --git a/libcxx/include/__new/operators.h b/libcxx/include/__new/operators.h
new file mode 100644
index 00000000000000..411f956bdda156
--- /dev/null
+++ b/libcxx/include/__new/operators.h
@@ -0,0 +1,100 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___NEW_OPERATORS_H
+#define _LIBCPP___NEW_OPERATORS_H
+
+#include <__config>
+#include <__cstddef/size_t.h>
+#include <__new/align_val_t.h>
+#include <__new/exceptions.h>
+#include <__new/nothrow_t.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if defined(_LIBCPP_CXX03_LANG)
+# define _THROW_BAD_ALLOC throw(std::bad_alloc)
+#else
+# define _THROW_BAD_ALLOC
+#endif
+
+#if defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309L
+# define _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION 1
+#else
+# define _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION 0
+#endif
+
+#if _LIBCPP_STD_VER >= 14 || _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION
+# define _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION 1
+#else
+# define _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION 0
+#endif
+
+#if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION && _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION
+# define _LIBCPP_HAS_SIZED_DEALLOCATION 1
+#else
+# define _LIBCPP_HAS_SIZED_DEALLOCATION 0
+#endif
+
+#if defined(_LIBCPP_ABI_VCRUNTIME)
+# include <new.h>
+#else
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
+ _LIBCPP_NOALIAS;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
+# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
+# endif
+
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
+ _LIBCPP_NOALIAS;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
+# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
+# endif
+
+# if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
+operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
+# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
+# endif
+
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
+operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
+[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
+operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
+# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
+_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
+# endif
+# endif
+
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void*
+operator new(std::size_t, void* __p) _NOEXCEPT {
+ return __p;
+}
+[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void*
+operator new[](std::size_t, void* __p) _NOEXCEPT {
+ return __p;
+}
+inline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {}
+inline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {}
+#endif
+
+#endif // _LIBCPP___NEW_OPERATORS_H
diff --git a/libcxx/include/__utility/small_buffer.h b/libcxx/include/__utility/small_buffer.h
index b44b37e90e7653..405c0aed459632 100644
--- a/libcxx/include/__utility/small_buffer.h
+++ b/libcxx/include/__utility/small_buffer.h
@@ -11,6 +11,7 @@
#include <__config>
#include <__cstddef/byte.h>
+#include <__cstddef/size_t.h>
#include <__memory/construct_at.h>
#include <__type_traits/decay.h>
#include <__type_traits/is_trivially_constructible.h>
diff --git a/libcxx/include/future b/libcxx/include/future
index 62f59682d404d3..dacf2c10a1990b 100644
--- a/libcxx/include/future
+++ b/libcxx/include/future
@@ -373,6 +373,7 @@ template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
# include <__chrono/steady_clock.h>
# include <__chrono/time_point.h>
# include <__condition_variable/condition_variable.h>
+# include <__cstddef/nullptr_t.h>
# include <__exception/exception_ptr.h>
# include <__memory/addressof.h>
# include <__memory/allocator.h>
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index 81621f467aba71..db2d79604b02e2 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1603,6 +1603,16 @@ module std [system] {
module new {
header "new"
+ module align_val_t { header "__new/align_val_t.h" }
+ module allocate { header "__new/allocate.h" }
+ module destroying_delete_t { header "__new/destroying_delete_t.h" }
+ module exceptions { header "__new/exceptions.h" }
+ module interference_size { header "__new/interference_size.h" }
+ module launder { header "__new/launder.h" }
+ module new_handler { header "__new/new_handler.h" }
+ module nothrow_t { header "__new/nothrow_t.h" }
+ module operators { header "__new/operators.h" }
+
export *
}
diff --git a/libcxx/include/new b/libcxx/include/new
index 8bb7b300d08bce..3cadbf1571e7f5 100644
--- a/libcxx/include/new
+++ b/libcxx/include/new
@@ -89,285 +89,29 @@ void operator delete[](void* ptr, void*) noexcept;
#if 0
#else // 0
# include <__config>
-# include <__cstddef/max_align_t.h>
-# include <__cstddef/size_t.h>
-# include <__exception/exception.h>
-# include <__type_traits/is_function.h>
-# include <__type_traits/is_void.h>
-# include <__verbose_abort>
-# include <version>
-
-# if defined(_LIBCPP_ABI_VCRUNTIME)
-# include <new.h>
-# endif
-
-# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-# pragma GCC system_header
-# endif
-
-# if defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309L
-# define _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION 1
-# else
-# define _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION 0
-# endif
-
-# if _LIBCPP_STD_VER >= 14 || _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION
-# define _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION 1
-# else
-# define _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION 0
-# endif
-
-# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION && _LIBCPP_HAS_LANGUAGE_SIZED_DEALLOCATION
-# define _LIBCPP_HAS_SIZED_DEALLOCATION 1
-# else
-# define _LIBCPP_HAS_SIZED_DEALLOCATION 0
-# endif
-
-namespace std // purposefully not using versioning namespace
-{
-
-# if !defined(_LIBCPP_ABI_VCRUNTIME)
-struct _LIBCPP_EXPORTED_FROM_ABI nothrow_t {
- explicit nothrow_t() = default;
-};
-extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
-
-class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
-public:
- bad_alloc() _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
- _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
- ~bad_alloc() _NOEXCEPT override;
- const char* what() const _NOEXCEPT override;
-};
-
-class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
-public:
- bad_array_new_length() _NOEXCEPT;
- _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
- _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
- ~bad_array_new_length() _NOEXCEPT override;
- const char* what() const _NOEXCEPT override;
-};
-
-typedef void (*new_handler)();
-_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
-_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
-
-# elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
-
-// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
-// since they would normally be provided in vcruntime_exception.h
-class bad_alloc : public exception {
-public:
- bad_alloc() noexcept : exception("bad allocation") {}
-
-private:
- friend class bad_array_new_length;
+# include <__new/align_val_t.h>
+# include <__new/allocate.h>
+# include <__new/exceptions.h>
+# include <__new/new_handler.h>
+# include <__new/nothrow_t.h>
+# include <__new/operators.h>
- bad_alloc(char const* const __message) noexcept : exception(__message) {}
-};
-
-class bad_array_new_length : public bad_alloc {
-public:
- bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
-};
-# endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
-
-[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
-
-[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() {
-# if _LIBCPP_HAS_EXCEPTIONS
- throw bad_array_new_length();
-# else
- _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
-# endif
-}
-
-# if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION && !defined(_LIBCPP_ABI_VCRUNTIME)
-# ifndef _LIBCPP_CXX03_LANG
-enum class align_val_t : size_t {};
-# else
-enum align_val_t { __zero = 0, __max = (size_t)-1 };
-# endif
+# if _LIBCPP_STD_VER >= 17
+# include <__new/interference_size.h>
+# include <__new/launder.h>
# endif
# if _LIBCPP_STD_VER >= 20
-// Enable the declaration even if the compiler doesn't support the language
-// feature.
-struct destroying_delete_t {
- explicit destroying_delete_t() = default;
-};
-inline constexpr destroying_delete_t destroying_delete{};
-# endif // _LIBCPP_STD_VER >= 20
-
-} // namespace std
-
-# if defined(_LIBCPP_CXX03_LANG)
-# define _THROW_BAD_ALLOC throw(std::bad_alloc)
-# else
-# define _THROW_BAD_ALLOC
-# endif
-
-# if !defined(_LIBCPP_ABI_VCRUNTIME)
-
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
- _LIBCPP_NOALIAS;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
-# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
-# endif
-
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT
- _LIBCPP_NOALIAS;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
-# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
-# endif
-
-# if _LIBCPP_HAS_LIBRARY_ALIGNED_ALLOCATION
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
-operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
-# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
-# endif
-
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
-operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
-[[__nodiscard__]] _LIBCPP_OVERRIDABLE_FUNC_VIS void*
-operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
-# if _LIBCPP_HAS_LIBRARY_SIZED_DEALLOCATION
-_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
-# endif
-# endif
-
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void*
-operator new(std::size_t, void* __p) _NOEXCEPT {
- return __p;
-}
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void*
-operator new[](std::size_t, void* __p) _NOEXCEPT {
- return __p;
-}
-inline _LIBCPP_HIDE_FROM_ABI void operator delete(void*, void*) _NOEXCEPT {}
-inline _LIBCPP_HIDE_FROM_ABI void operator delete[](void*, void*) _NOEXCEPT {}
-
-# endif // !_LIBCPP_ABI_VCRUNTIME
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
-# ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
- return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
-# else
- return __align > _LIBCPP_ALIGNOF(max_align_t);
-# endif
-}
-
-template <class... _Args>
-_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
-# if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
- return __builtin_operator_new(__args...);
-# else
- return ::operator new(__args...);
-# endif
-}
-
-template <class... _Args>
-_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT {
-# if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
- __builtin_operator_delete(__args...);
-# else
- ::operator delete(__args...);
-# endif
-}
-
-inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) {
-# if _LIBCPP_HAS_ALIGNED_ALLOCATION
- if (__is_overaligned_for_new(__align)) {
- const align_val_t __align_val = static_cast<align_val_t>(__align);
- return __libcpp_operator_new(__size, __align_val);
- }
-# endif
-
- (void)__align;
- return __libcpp_operator_new(__size);
-}
-
-template <class... _Args>
-_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) _NOEXCEPT {
-# if !_LIBCPP_HAS_SIZED_DEALLOCATION
- (void)__size;
- return std::__libcpp_operator_delete(__ptr, __args...);
-# else
- return std::__libcpp_operator_delete(__ptr, __size, __args...);
-# endif
-}
-
-inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) _NOEXCEPT {
-# if !_LIBCPP_HAS_ALIGNED_ALLOCATION
- (void)__align;
- return __do_deallocate_handle_size(__ptr, __size);
-# else
- if (__is_overaligned_for_new(__align)) {
- const align_val_t __align_val = static_cast<align_val_t>(__align);
- return __do_deallocate_handle_size(__ptr, __size, __align_val);
- } else {
- return __do_deallocate_handle_size(__ptr, __size);
- }
-# endif
-}
-
-inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) _NOEXCEPT {
-# if !_LIBCPP_HAS_ALIGNED_ALLOCATION
- (void)__align;
- return __libcpp_operator_delete(__ptr);
-# else
- if (__is_overaligned_for_new(__align)) {
- const align_val_t __align_val = static_cast<align_val_t>(__align);
- return __libcpp_operator_delete(__ptr, __align_val);
- } else {
- return __libcpp_operator_delete(__ptr);
- }
+# include <__new/destroying_delete_t.h>
# endif
-}
-template <class _Tp>
-[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT {
- static_assert(!(is_function<_Tp>::value), "can't launder functions");
- static_assert(!is_void<_Tp>::value, "can't launder cv-void");
- return __builtin_launder(__p);
-}
+// feature-test macros
+# include <version>
-# if _LIBCPP_STD_VER >= 17
-template <class _Tp>
-[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp* launder(_Tp* __p) noexcept {
- return std::__launder(__p);
-}
+# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
# endif
-# if _LIBCPP_STD_VER >= 17
-
-# if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
-
-inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
-inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
-
-# endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
-
-# endif // _LIBCPP_STD_VER >= 17
-
-_LIBCPP_END_NAMESPACE_STD
-
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
# include <cstddef>
# include <cstdlib>
More information about the libcxx-commits
mailing list