blob: e21c22bc2ed1f290bfcdc316039e3ee4ca759ee2 [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>
90#include <exception>
Eric Fiselier06548ab2018-03-22 04:42:56 +000091#include <type_traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +000092#include <cstddef>
Marshall Clow0a1e7502018-09-12 19:41:40 +000093#include <version>
Eric Fiselier279c3192016-09-06 21:25:27 +000094#ifdef _LIBCPP_NO_EXCEPTIONS
95#include <cstdlib>
96#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000097
Eric Fiselier85f66332019-03-05 01:57:01 +000098#if defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +000099#include <new.h>
100#endif
101
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000102#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000104#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105
Eric Fiselier2856ef82018-10-25 17:21:30 +0000106#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
107#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
108#endif
109
110#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
111 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000112# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
113#endif
114
115#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
Eric Fiselier2856ef82018-10-25 17:21:30 +0000116 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
Eric Fiselier162922f2016-10-14 06:46:30 +0000117# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
118#endif
119
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120namespace std // purposefully not using versioning namespace
121{
122
Eric Fiselier85f66332019-03-05 01:57:01 +0000123#if !defined(_LIBCPP_ABI_VCRUNTIME)
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000124struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
Eric Fiselierec3a1672017-02-10 08:57:35 +0000125extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
126
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127class _LIBCPP_EXCEPTION_ABI bad_alloc
128 : public exception
129{
130public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000131 bad_alloc() _NOEXCEPT;
132 virtual ~bad_alloc() _NOEXCEPT;
133 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134};
135
136class _LIBCPP_EXCEPTION_ABI bad_array_new_length
137 : public bad_alloc
138{
139public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000140 bad_array_new_length() _NOEXCEPT;
141 virtual ~bad_array_new_length() _NOEXCEPT;
142 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143};
144
Eric Fiselierec3a1672017-02-10 08:57:35 +0000145typedef void (*new_handler)();
146_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
147_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
148
Eric Fiselier85f66332019-03-05 01:57:01 +0000149#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000150
Marshall Clow8fea1612016-08-25 15:09:01 +0000151_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
152
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000153#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
Eric Fiselier85f66332019-03-05 01:57:01 +0000154 !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselier162922f2016-10-14 06:46:30 +0000155#ifndef _LIBCPP_CXX03_LANG
156enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
157#else
158enum align_val_t { __zero = 0, __max = (size_t)-1 };
159#endif
160#endif
161
Eric Fiselier02a8a2c2019-05-23 23:46:44 +0000162#if _LIBCPP_STD_VER > 17
163// Enable the declaration even if the compiler doesn't support the language
164// feature.
165struct destroying_delete_t {
166 explicit destroying_delete_t() = default;
167};
168_LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{};
169#endif // _LIBCPP_STD_VER > 17
170
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171} // std
172
Eric Fiselier499a0a52017-01-02 23:27:42 +0000173#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier162922f2016-10-14 06:46:30 +0000174#define _THROW_BAD_ALLOC throw(std::bad_alloc)
175#else
176#define _THROW_BAD_ALLOC
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000177#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000178
Eric Fiselier85f66332019-03-05 01:57:01 +0000179#if !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +0000180
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000181_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 +0000182_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 +0000183_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
184_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000185#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000186_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 +0000187#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000188
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000189_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 +0000190_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 +0000191_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
192_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000193#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000194_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 +0000195#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000196
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000197#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000198_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 +0000199_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 +0000200_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
201_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 +0000202#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000203_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 +0000204#endif
205
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000206_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 +0000207_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 +0000208_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
209_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 +0000210#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000211_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 +0000212#endif
213#endif
214
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000215_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
216_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 +0000217inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
218inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219
Eric Fiselier85f66332019-03-05 01:57:01 +0000220#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000221
Richard Smith1f1c1472014-06-04 19:54:15 +0000222_LIBCPP_BEGIN_NAMESPACE_STD
223
Eric Fiselier06548ab2018-03-22 04:42:56 +0000224_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
225#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
226 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
227#else
228 return __align > alignment_of<max_align_t>::value;
229#endif
230}
231
Louis Dionne11a08d02020-09-25 09:24:14 -0400232template <class ..._Args>
233_LIBCPP_INLINE_VISIBILITY
234void* __libcpp_operator_new(_Args ...__args) {
235#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
236 return __builtin_operator_new(__args...);
237#else
238 return ::operator new(__args...);
239#endif
240}
241
242template <class ..._Args>
243_LIBCPP_INLINE_VISIBILITY
244void __libcpp_operator_delete(_Args ...__args) {
245#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
246 __builtin_operator_delete(__args...);
247#else
248 ::operator delete(__args...);
249#endif
250}
251
Louis Dionne3c989bc2020-09-28 17:29:52 -0400252inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne99f59432020-09-17 12:06:13 -0400253void *__libcpp_allocate(size_t __size, size_t __align) {
Eric Fiselier06548ab2018-03-22 04:42:56 +0000254#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
255 if (__is_overaligned_for_new(__align)) {
256 const align_val_t __align_val = static_cast<align_val_t>(__align);
Louis Dionne11a08d02020-09-25 09:24:14 -0400257 return __libcpp_operator_new(__size, __align_val);
Eric Fiselier06548ab2018-03-22 04:42:56 +0000258 }
Eric Fiselier06548ab2018-03-22 04:42:56 +0000259#endif
Louis Dionne11a08d02020-09-25 09:24:14 -0400260
261 (void)__align;
262 return __libcpp_operator_new(__size);
263}
264
265template <class ..._Args>
266_LIBCPP_INLINE_VISIBILITY
267void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
268#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
269 (void)__size;
270 return __libcpp_operator_delete(__ptr, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000271#else
Louis Dionne11a08d02020-09-25 09:24:14 -0400272 return __libcpp_operator_delete(__ptr, __size, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000273#endif
274}
275
Louis Dionne11a08d02020-09-25 09:24:14 -0400276inline _LIBCPP_INLINE_VISIBILITY
277void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
Eric Fiselier2856ef82018-10-25 17:21:30 +0000278#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Louis Dionne11a08d02020-09-25 09:24:14 -0400279 (void)__align;
Eric Fiselier2856ef82018-10-25 17:21:30 +0000280 return __do_deallocate_handle_size(__ptr, __size);
281#else
282 if (__is_overaligned_for_new(__align)) {
283 const align_val_t __align_val = static_cast<align_val_t>(__align);
284 return __do_deallocate_handle_size(__ptr, __size, __align_val);
285 } else {
286 return __do_deallocate_handle_size(__ptr, __size);
287 }
288#endif
Eric Fiselier2856ef82018-10-25 17:21:30 +0000289}
290
291inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
Louis Dionne11a08d02020-09-25 09:24:14 -0400292#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
293 (void)__align;
294 return __libcpp_operator_delete(__ptr);
295#else
296 if (__is_overaligned_for_new(__align)) {
297 const align_val_t __align_val = static_cast<align_val_t>(__align);
298 return __libcpp_operator_delete(__ptr, __align_val);
299 } else {
300 return __libcpp_operator_delete(__ptr);
301 }
302#endif
Richard Smith1f1c1472014-06-04 19:54:15 +0000303}
304
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000305template <class _Tp>
Louis Dionne44bcff92018-08-03 22:36:53 +0000306_LIBCPP_NODISCARD_AFTER_CXX17 inline
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000307_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
308{
309 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
310 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
311#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
312 return __builtin_launder(__p);
313#else
314 return __p;
315#endif
316}
317
318
319#if _LIBCPP_STD_VER > 14
320template <class _Tp>
321_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
322constexpr _Tp* launder(_Tp* __p) noexcept
323{
Marshall Clow6ac349c2017-11-23 01:25:03 +0000324 return _VSTD::__launder(__p);
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000325}
326#endif
327
Richard Smith1f1c1472014-06-04 19:54:15 +0000328_LIBCPP_END_NAMESPACE_STD
329
Howard Hinnantc51e1022010-05-11 19:42:16 +0000330#endif // _LIBCPP_NEW