blob: 21bc30ba1363e66c9a16d1b2b97d178a63368be3 [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
Marek Kurdej24b4c512021-01-07 12:29:04 +010052void* operator new(std::size_t size); // replaceable, nodiscard in C++20
53void* operator new(std::size_t size, std::align_val_t alignment); // replaceable, C++17, nodiscard in C++20
54void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
Eric Fiselier162922f2016-10-14 06:46:30 +000055void* operator new(std::size_t size, std::align_val_t alignment,
Marek Kurdej24b4c512021-01-07 12:29:04 +010056 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
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
Marek Kurdej24b4c512021-01-07 12:29:04 +010066void* operator new[](std::size_t size); // replaceable, nodiscard in C++20
Eric Fiselier162922f2016-10-14 06:46:30 +000067void* operator new[](std::size_t size,
Marek Kurdej24b4c512021-01-07 12:29:04 +010068 std::align_val_t alignment) noexcept; // replaceable, C++17, nodiscard in C++20
69void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++20
Eric Fiselier162922f2016-10-14 06:46:30 +000070void* operator new[](std::size_t size, std::align_val_t alignment,
Marek Kurdej24b4c512021-01-07 12:29:04 +010071 const std::nothrow_t&) noexcept; // replaceable, C++17, nodiscard in C++20
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
Marek Kurdej24b4c512021-01-07 12:29:04 +010082void* operator new (std::size_t size, void* ptr) noexcept; // nodiscard in C++20
83void* operator new[](std::size_t size, void* ptr) noexcept; // nodiscard in C++20
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>
Louis Dionnea3922172020-11-12 15:14:33 -050091#include <cstddef>
92#include <cstdlib>
Howard Hinnantc51e1022010-05-11 19:42:16 +000093#include <exception>
Eric Fiselier06548ab2018-03-22 04:42:56 +000094#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000095#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
Eric Fiselier85f66332019-03-05 01:57:01 +000097#if defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +000098#include <new.h>
99#endif
100
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000101#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000102#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000103#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104
Eric Fiselier2856ef82018-10-25 17:21:30 +0000105#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation < 201309L
106#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
107#endif
108
109#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
110 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000111# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
112#endif
113
114#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
Eric Fiselier2856ef82018-10-25 17:21:30 +0000115 defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
Eric Fiselier162922f2016-10-14 06:46:30 +0000116# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
117#endif
118
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119namespace std // purposefully not using versioning namespace
120{
121
Eric Fiselier85f66332019-03-05 01:57:01 +0000122#if !defined(_LIBCPP_ABI_VCRUNTIME)
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000123struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
Eric Fiselierec3a1672017-02-10 08:57:35 +0000124extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126class _LIBCPP_EXCEPTION_ABI bad_alloc
127 : public exception
128{
129public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000130 bad_alloc() _NOEXCEPT;
131 virtual ~bad_alloc() _NOEXCEPT;
132 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133};
134
135class _LIBCPP_EXCEPTION_ABI bad_array_new_length
136 : public bad_alloc
137{
138public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000139 bad_array_new_length() _NOEXCEPT;
140 virtual ~bad_array_new_length() _NOEXCEPT;
141 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142};
143
Eric Fiselierec3a1672017-02-10 08:57:35 +0000144typedef void (*new_handler)();
145_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
146_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
147
Eric Fiselier85f66332019-03-05 01:57:01 +0000148#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000149
Marshall Clow8fea1612016-08-25 15:09:01 +0000150_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
151
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000152#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
Eric Fiselier85f66332019-03-05 01:57:01 +0000153 !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselier162922f2016-10-14 06:46:30 +0000154#ifndef _LIBCPP_CXX03_LANG
155enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
156#else
157enum align_val_t { __zero = 0, __max = (size_t)-1 };
158#endif
159#endif
160
Eric Fiselier02a8a2c2019-05-23 23:46:44 +0000161#if _LIBCPP_STD_VER > 17
162// Enable the declaration even if the compiler doesn't support the language
163// feature.
164struct destroying_delete_t {
165 explicit destroying_delete_t() = default;
166};
167_LIBCPP_INLINE_VAR constexpr destroying_delete_t destroying_delete{};
168#endif // _LIBCPP_STD_VER > 17
169
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170} // std
171
Eric Fiselier499a0a52017-01-02 23:27:42 +0000172#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier162922f2016-10-14 06:46:30 +0000173#define _THROW_BAD_ALLOC throw(std::bad_alloc)
174#else
175#define _THROW_BAD_ALLOC
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000176#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000177
Eric Fiselier85f66332019-03-05 01:57:01 +0000178#if !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +0000179
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000180_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 +0000181_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 +0000182_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p) _NOEXCEPT;
183_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000184#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000185_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 +0000186#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000187
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000188_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 +0000189_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 +0000190_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
191_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000192#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000193_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 +0000194#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000195
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000196#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000197_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 +0000198_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 +0000199_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::align_val_t) _NOEXCEPT;
200_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 +0000201#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000202_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 +0000203#endif
204
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000205_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 +0000206_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 +0000207_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
208_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 +0000209#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000210_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 +0000211#endif
212#endif
213
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000214_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
215_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 +0000216inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
217inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218
Eric Fiselier85f66332019-03-05 01:57:01 +0000219#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000220
Richard Smith1f1c1472014-06-04 19:54:15 +0000221_LIBCPP_BEGIN_NAMESPACE_STD
222
Eric Fiselier06548ab2018-03-22 04:42:56 +0000223_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
224#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
225 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
226#else
227 return __align > alignment_of<max_align_t>::value;
228#endif
229}
230
Louis Dionne11a08d02020-09-25 09:24:14 -0400231template <class ..._Args>
232_LIBCPP_INLINE_VISIBILITY
233void* __libcpp_operator_new(_Args ...__args) {
234#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
235 return __builtin_operator_new(__args...);
236#else
237 return ::operator new(__args...);
238#endif
239}
240
241template <class ..._Args>
242_LIBCPP_INLINE_VISIBILITY
243void __libcpp_operator_delete(_Args ...__args) {
244#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
245 __builtin_operator_delete(__args...);
246#else
247 ::operator delete(__args...);
248#endif
249}
250
Louis Dionne3c989bc2020-09-28 17:29:52 -0400251inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne99f59432020-09-17 12:06:13 -0400252void *__libcpp_allocate(size_t __size, size_t __align) {
Eric Fiselier06548ab2018-03-22 04:42:56 +0000253#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
254 if (__is_overaligned_for_new(__align)) {
255 const align_val_t __align_val = static_cast<align_val_t>(__align);
Louis Dionne11a08d02020-09-25 09:24:14 -0400256 return __libcpp_operator_new(__size, __align_val);
Eric Fiselier06548ab2018-03-22 04:42:56 +0000257 }
Eric Fiselier06548ab2018-03-22 04:42:56 +0000258#endif
Louis Dionne11a08d02020-09-25 09:24:14 -0400259
260 (void)__align;
261 return __libcpp_operator_new(__size);
262}
263
264template <class ..._Args>
265_LIBCPP_INLINE_VISIBILITY
266void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
267#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
268 (void)__size;
269 return __libcpp_operator_delete(__ptr, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000270#else
Louis Dionne11a08d02020-09-25 09:24:14 -0400271 return __libcpp_operator_delete(__ptr, __size, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000272#endif
273}
274
Louis Dionne11a08d02020-09-25 09:24:14 -0400275inline _LIBCPP_INLINE_VISIBILITY
276void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
Eric Fiselier2856ef82018-10-25 17:21:30 +0000277#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Louis Dionne11a08d02020-09-25 09:24:14 -0400278 (void)__align;
Eric Fiselier2856ef82018-10-25 17:21:30 +0000279 return __do_deallocate_handle_size(__ptr, __size);
280#else
281 if (__is_overaligned_for_new(__align)) {
282 const align_val_t __align_val = static_cast<align_val_t>(__align);
283 return __do_deallocate_handle_size(__ptr, __size, __align_val);
284 } else {
285 return __do_deallocate_handle_size(__ptr, __size);
286 }
287#endif
Eric Fiselier2856ef82018-10-25 17:21:30 +0000288}
289
290inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
Louis Dionne11a08d02020-09-25 09:24:14 -0400291#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
292 (void)__align;
293 return __libcpp_operator_delete(__ptr);
294#else
295 if (__is_overaligned_for_new(__align)) {
296 const align_val_t __align_val = static_cast<align_val_t>(__align);
297 return __libcpp_operator_delete(__ptr, __align_val);
298 } else {
299 return __libcpp_operator_delete(__ptr);
300 }
301#endif
Richard Smith1f1c1472014-06-04 19:54:15 +0000302}
303
Louis Dionnea3922172020-11-12 15:14:33 -0500304#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
305// Low-level helpers to call the aligned allocation and deallocation functions
306// on the target platform. This is used to implement libc++'s own memory
307// allocation routines -- if you need to allocate memory inside the library,
308// chances are that you want to use `__libcpp_allocate` instead.
309//
310// Returns the allocated memory, or `nullptr` on failure.
311inline _LIBCPP_INLINE_VISIBILITY
312void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
313#if defined(_LIBCPP_MSVCRT_LIKE)
314 return ::_aligned_malloc(__size, __alignment);
315#else
316 void* __result = nullptr;
Vitaly Buka9dda87b2021-02-26 14:29:06 -0800317 (void)::posix_memalign(&__result, __alignment, __size);
Louis Dionnea3922172020-11-12 15:14:33 -0500318 // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
319 return __result;
320#endif
321}
322
323inline _LIBCPP_INLINE_VISIBILITY
324void __libcpp_aligned_free(void* __ptr) {
325#if defined(_LIBCPP_MSVCRT_LIKE)
326 ::_aligned_free(__ptr);
327#else
328 ::free(__ptr);
329#endif
330}
331#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
332
333
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000334template <class _Tp>
Louis Dionne44bcff92018-08-03 22:36:53 +0000335_LIBCPP_NODISCARD_AFTER_CXX17 inline
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000336_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
337{
338 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
339 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
340#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
341 return __builtin_launder(__p);
342#else
343 return __p;
344#endif
345}
346
347
348#if _LIBCPP_STD_VER > 14
349template <class _Tp>
350_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
351constexpr _Tp* launder(_Tp* __p) noexcept
352{
Marshall Clow6ac349c2017-11-23 01:25:03 +0000353 return _VSTD::__launder(__p);
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000354}
355#endif
356
Richard Smith1f1c1472014-06-04 19:54:15 +0000357_LIBCPP_END_NAMESPACE_STD
358
Howard Hinnantc51e1022010-05-11 19:42:16 +0000359#endif // _LIBCPP_NEW