blob: 66747b04d4d74d73bab21538a4d9e11b159cfb24 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_NEW
11#define _LIBCPP_NEW
12
13/*
14 new synopsis
15
16namespace std
17{
18
19class bad_alloc
20 : public exception
21{
22public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000023 bad_alloc() noexcept;
24 bad_alloc(const bad_alloc&) noexcept;
25 bad_alloc& operator=(const bad_alloc&) noexcept;
26 virtual const char* what() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000027};
28
Eric Fiselier162922f2016-10-14 06:46:30 +000029class bad_array_new_length : public bad_alloc // C++14
Marshall Clow3bf77132013-09-11 01:38:42 +000030{
31public:
32 bad_array_new_length() noexcept;
33};
34
Eric Fiselier162922f2016-10-14 06:46:30 +000035enum class align_val_t : size_t {}; // C++17
Eric Fiselier02a8a2c2019-05-23 23:46:44 +000036
37struct destroying_delete_t { // C++20
38 explicit destroying_delete_t() = default;
39};
40inline constexpr destroying_delete_t destroying_delete{}; // C++20
41
Louis Dionnea7a2beb2019-09-26 14:51:10 +000042struct nothrow_t { explicit nothrow_t() = default; };
Howard Hinnantc51e1022010-05-11 19:42:16 +000043extern const nothrow_t nothrow;
44typedef void (*new_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000045new_handler set_new_handler(new_handler new_p) noexcept;
46new_handler get_new_handler() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047
Marshall Clowdf5a11f2017-11-22 19:49:03 +000048// 21.6.4, pointer optimization barrier
49template <class T> constexpr T* launder(T* p) noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000050} // std
51
Marshall Clowbe8b84d2017-12-04 23:03:42 +000052void* operator new(std::size_t size); // replaceable, nodiscard in C++2a
53void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++2a
54void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
Eric Fiselier162922f2016-10-14 06:46:30 +000055void* operator new(std::size_t size, std::align_val_t alignment,
Marshall Clowbe8b84d2017-12-04 23:03:42 +000056 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000057void operator delete(void* ptr) noexcept; // replaceable
Larisse Voufo574190d2015-02-15 05:18:55 +000058void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier162922f2016-10-14 06:46:30 +000059void operator delete(void* ptr, std::align_val_t alignment) noexcept; // replaceable, C++17
60void operator delete(void* ptr, std::size_t size,
61 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000062void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier162922f2016-10-14 06:46:30 +000063void operator delete(void* ptr, std:align_val_t alignment,
64 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000065
Marshall Clowbe8b84d2017-12-04 23:03:42 +000066void* operator new[](std::size_t size); // replaceable, nodiscard in C++2a
Eric Fiselier162922f2016-10-14 06:46:30 +000067void* operator new[](std::size_t size,
Marshall Clowbe8b84d2017-12-04 23:03:42 +000068 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++2a
69void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
Eric Fiselier162922f2016-10-14 06:46:30 +000070void* operator new[](std::size_t size, std::align_val_t alignment,
Marshall Clowbe8b84d2017-12-04 23:03:42 +000071 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++2a
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000072void operator delete[](void* ptr) noexcept; // replaceable
Larisse Voufo574190d2015-02-15 05:18:55 +000073void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
Eric Fiselier162922f2016-10-14 06:46:30 +000074void operator delete[](void* ptr,
75 std::align_val_t alignment) noexcept; // replaceable, C++17
76void operator delete[](void* ptr, std::size_t size,
77 std::align_val_t alignment) noexcept; // replaceable, C++17
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000078void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
Eric Fiselier162922f2016-10-14 06:46:30 +000079void operator delete[](void* ptr, std::align_val_t alignment,
80 const std::nothrow_t&) noexcept; // replaceable, C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000081
Marshall Clowbe8b84d2017-12-04 23:03:42 +000082void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++2a
83void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++2a
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000084void operator delete (void* ptr, void*) noexcept;
85void operator delete[](void* ptr, void*) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000086
87*/
88
89#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -050090#include <__availability>
Howard Hinnantc51e1022010-05-11 19:42:16 +000091#include <exception>
Eric Fiselier06548ab2018-03-22 04:42:56 +000092#include <type_traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +000093#include <cstddef>
Marshall Clow0a1e7502018-09-12 19:41:40 +000094#include <version>
Eric Fiselier279c3192016-09-06 21:25:27 +000095#ifdef _LIBCPP_NO_EXCEPTIONS
96#include <cstdlib>
97#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000098
Eric Fiselier85f66332019-03-05 01:57:01 +000099#if defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +0000100#include <new.h>
101#endif
102
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000105#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106
Eric Fiselier2856ef82018-10-25 17:21:30 +0000107#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
108#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
109#endif
110
111#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
112 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000113# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
114#endif
115
116#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
Eric Fiselier2856ef82018-10-25 17:21:30 +0000117 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
Eric Fiselier162922f2016-10-14 06:46:30 +0000118# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
119#endif
120
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121namespace std // purposefully not using versioning namespace
122{
123
Eric Fiselier85f66332019-03-05 01:57:01 +0000124#if !defined(_LIBCPP_ABI_VCRUNTIME)
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000125struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
Eric Fiselierec3a1672017-02-10 08:57:35 +0000126extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
127
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128class _LIBCPP_EXCEPTION_ABI bad_alloc
129 : public exception
130{
131public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000132 bad_alloc() _NOEXCEPT;
133 virtual ~bad_alloc() _NOEXCEPT;
134 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135};
136
137class _LIBCPP_EXCEPTION_ABI bad_array_new_length
138 : public bad_alloc
139{
140public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000141 bad_array_new_length() _NOEXCEPT;
142 virtual ~bad_array_new_length() _NOEXCEPT;
143 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144};
145
Eric Fiselierec3a1672017-02-10 08:57:35 +0000146typedef void (*new_handler)();
147_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
148_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
149
Eric Fiselier85f66332019-03-05 01:57:01 +0000150#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000151
Marshall Clow8fea1612016-08-25 15:09:01 +0000152_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
153
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000154#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
Eric Fiselier85f66332019-03-05 01:57:01 +0000155 !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselier162922f2016-10-14 06:46:30 +0000156#ifndef _LIBCPP_CXX03_LANG
157enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
158#else
159enum align_val_t { __zero = 0, __max = (size_t)-1 };
160#endif
161#endif
162
Eric Fiselier02a8a2c2019-05-23 23:46:44 +0000163#if _LIBCPP_STD_VER > 17
164// Enable the declaration even if the compiler doesn't support the language
165// feature.
166struct destroying_delete_t {
167 explicit destroying_delete_t() = default;
168};
169_LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{};
170#endif // _LIBCPP_STD_VER > 17
171
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172} // std
173
Eric Fiselier499a0a52017-01-02 23:27:42 +0000174#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier162922f2016-10-14 06:46:30 +0000175#define _THROW_BAD_ALLOC throw(std::bad_alloc)
176#else
177#define _THROW_BAD_ALLOC
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000178#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000179
Eric Fiselier85f66332019-03-05 01:57:01 +0000180#if !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +0000181
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000182_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
Louis Dionnee147b352019-02-26 06:34:42 +0000183_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000184_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
185_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000186#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000187_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000188#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000189
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000190_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
Louis Dionnee147b352019-02-26 06:34:42 +0000191_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000192_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
193_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000194#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000195_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
Larisse Voufoa744a7e2015-02-20 06:13:05 +0000196#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000197
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000198#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000199_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
Louis Dionnee147b352019-02-26 06:34:42 +0000200_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000201_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
202_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000203#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000204_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
Eric Fiselier162922f2016-10-14 06:46:30 +0000205#endif
206
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000207_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
Louis Dionnee147b352019-02-26 06:34:42 +0000208_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000209_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
210_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000211#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000212_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
Eric Fiselier162922f2016-10-14 06:46:30 +0000213#endif
214#endif
215
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000216_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
217_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000218inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
219inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220
Eric Fiselier85f66332019-03-05 01:57:01 +0000221#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000222
Richard Smith1f1c1472014-06-04 19:54:15 +0000223_LIBCPP_BEGIN_NAMESPACE_STD
224
Eric Fiselier06548ab2018-03-22 04:42:56 +0000225_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
226#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
227 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
228#else
229 return __align > alignment_of<max_align_t>::value;
230#endif
231}
232
Louis Dionne11a08d02020-09-25 09:24:14 -0400233template <class ..._Args>
234_LIBCPP_INLINE_VISIBILITY
235void* __libcpp_operator_new(_Args ...__args) {
236#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
237 return __builtin_operator_new(__args...);
238#else
239 return ::operator new(__args...);
240#endif
241}
242
243template <class ..._Args>
244_LIBCPP_INLINE_VISIBILITY
245void __libcpp_operator_delete(_Args ...__args) {
246#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
247 __builtin_operator_delete(__args...);
248#else
249 ::operator delete(__args...);
250#endif
251}
252
Louis Dionne3c989bc2020-09-28 17:29:52 -0400253inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne99f59432020-09-17 12:06:13 -0400254void *__libcpp_allocate(size_t __size, size_t __align) {
Eric Fiselier06548ab2018-03-22 04:42:56 +0000255#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
256 if (__is_overaligned_for_new(__align)) {
257 const align_val_t __align_val = static_cast<align_val_t>(__align);
Louis Dionne11a08d02020-09-25 09:24:14 -0400258 return __libcpp_operator_new(__size, __align_val);
Eric Fiselier06548ab2018-03-22 04:42:56 +0000259 }
Eric Fiselier06548ab2018-03-22 04:42:56 +0000260#endif
Louis Dionne11a08d02020-09-25 09:24:14 -0400261
262 (void)__align;
263 return __libcpp_operator_new(__size);
264}
265
266template <class ..._Args>
267_LIBCPP_INLINE_VISIBILITY
268void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
269#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
270 (void)__size;
271 return __libcpp_operator_delete(__ptr, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000272#else
Louis Dionne11a08d02020-09-25 09:24:14 -0400273 return __libcpp_operator_delete(__ptr, __size, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000274#endif
275}
276
Louis Dionne11a08d02020-09-25 09:24:14 -0400277inline _LIBCPP_INLINE_VISIBILITY
278void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
Eric Fiselier2856ef82018-10-25 17:21:30 +0000279#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Louis Dionne11a08d02020-09-25 09:24:14 -0400280 (void)__align;
Eric Fiselier2856ef82018-10-25 17:21:30 +0000281 return __do_deallocate_handle_size(__ptr, __size);
282#else
283 if (__is_overaligned_for_new(__align)) {
284 const align_val_t __align_val = static_cast<align_val_t>(__align);
285 return __do_deallocate_handle_size(__ptr, __size, __align_val);
286 } else {
287 return __do_deallocate_handle_size(__ptr, __size);
288 }
289#endif
Eric Fiselier2856ef82018-10-25 17:21:30 +0000290}
291
292inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
Louis Dionne11a08d02020-09-25 09:24:14 -0400293#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
294 (void)__align;
295 return __libcpp_operator_delete(__ptr);
296#else
297 if (__is_overaligned_for_new(__align)) {
298 const align_val_t __align_val = static_cast<align_val_t>(__align);
299 return __libcpp_operator_delete(__ptr, __align_val);
300 } else {
301 return __libcpp_operator_delete(__ptr);
302 }
303#endif
Richard Smith1f1c1472014-06-04 19:54:15 +0000304}
305
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000306template <class _Tp>
Louis Dionne44bcff92018-08-03 22:36:53 +0000307_LIBCPP_NODISCARD_AFTER_CXX17 inline
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000308_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
309{
310 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
311 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
312#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
313 return __builtin_launder(__p);
314#else
315 return __p;
316#endif
317}
318
319
320#if _LIBCPP_STD_VER > 14
321template <class _Tp>
322_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
323constexpr _Tp* launder(_Tp* __p) noexcept
324{
Marshall Clow6ac349c2017-11-23 01:25:03 +0000325 return _VSTD::__launder(__p);
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000326}
327#endif
328
Richard Smith1f1c1472014-06-04 19:54:15 +0000329_LIBCPP_END_NAMESPACE_STD
330
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331#endif // _LIBCPP_NEW