blob: be0d972f42da5d88a8b454a544848e19882881a3 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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
Louis Dionne73912b22020-11-04 15:01:25 -050089#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040090#include <__config>
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
Mikhail Maltsevd93a8772021-10-18 19:12:42 +0100152_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
153void __throw_bad_array_new_length()
154{
155#ifndef _LIBCPP_NO_EXCEPTIONS
156 throw bad_array_new_length();
157#else
158 _VSTD::abort();
159#endif
160}
161
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000162#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
Eric Fiselier85f66332019-03-05 01:57:01 +0000163 !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselier162922f2016-10-14 06:46:30 +0000164#ifndef _LIBCPP_CXX03_LANG
165enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
166#else
167enum align_val_t { __zero = 0, __max = (size_t)-1 };
168#endif
169#endif
170
Eric Fiselier02a8a2c2019-05-23 23:46:44 +0000171#if _LIBCPP_STD_VER > 17
172// Enable the declaration even if the compiler doesn't support the language
173// feature.
174struct destroying_delete_t {
175 explicit destroying_delete_t() = default;
176};
Louis Dionne559be102021-09-22 09:35:32 -0400177inline constexpr destroying_delete_t destroying_delete{};
Eric Fiselier02a8a2c2019-05-23 23:46:44 +0000178#endif // _LIBCPP_STD_VER > 17
179
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100180} // namespace std
Howard Hinnantc51e1022010-05-11 19:42:16 +0000181
Eric Fiselier499a0a52017-01-02 23:27:42 +0000182#if defined(_LIBCPP_CXX03_LANG)
Eric Fiselier162922f2016-10-14 06:46:30 +0000183#define _THROW_BAD_ALLOC throw(std::bad_alloc)
184#else
185#define _THROW_BAD_ALLOC
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000186#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000187
Eric Fiselier85f66332019-03-05 01:57:01 +0000188#if !defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +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
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000198_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 +0000199_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 +0000200_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p) _NOEXCEPT;
201_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000202#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Mehdi Amini228053d2017-05-04 17:08:54 +0000203_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 +0000204#endif
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000205
Eric Fiselierff4c8a22018-10-11 00:17:24 +0000206#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
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
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000215_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 +0000216_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 +0000217_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
218_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 +0000219#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
Akira Hatanaka8c77d152017-06-30 18:50:23 +0000220_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 +0000221#endif
222#endif
223
Marshall Clowbe8b84d2017-12-04 23:03:42 +0000224_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
225_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 +0000226inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
227inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000228
Eric Fiselier85f66332019-03-05 01:57:01 +0000229#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselierec3a1672017-02-10 08:57:35 +0000230
Richard Smith1f1c1472014-06-04 19:54:15 +0000231_LIBCPP_BEGIN_NAMESPACE_STD
232
Eric Fiselier06548ab2018-03-22 04:42:56 +0000233_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
234#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
235 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
236#else
237 return __align > alignment_of<max_align_t>::value;
238#endif
239}
240
Louis Dionne11a08d02020-09-25 09:24:14 -0400241template <class ..._Args>
242_LIBCPP_INLINE_VISIBILITY
243void* __libcpp_operator_new(_Args ...__args) {
244#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
245 return __builtin_operator_new(__args...);
246#else
247 return ::operator new(__args...);
248#endif
249}
250
251template <class ..._Args>
252_LIBCPP_INLINE_VISIBILITY
253void __libcpp_operator_delete(_Args ...__args) {
254#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
255 __builtin_operator_delete(__args...);
256#else
257 ::operator delete(__args...);
258#endif
259}
260
Louis Dionne3c989bc2020-09-28 17:29:52 -0400261inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne99f59432020-09-17 12:06:13 -0400262void *__libcpp_allocate(size_t __size, size_t __align) {
Eric Fiselier06548ab2018-03-22 04:42:56 +0000263#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
264 if (__is_overaligned_for_new(__align)) {
265 const align_val_t __align_val = static_cast<align_val_t>(__align);
Louis Dionne11a08d02020-09-25 09:24:14 -0400266 return __libcpp_operator_new(__size, __align_val);
Eric Fiselier06548ab2018-03-22 04:42:56 +0000267 }
Eric Fiselier06548ab2018-03-22 04:42:56 +0000268#endif
Louis Dionne11a08d02020-09-25 09:24:14 -0400269
270 (void)__align;
271 return __libcpp_operator_new(__size);
272}
273
274template <class ..._Args>
275_LIBCPP_INLINE_VISIBILITY
276void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
277#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
278 (void)__size;
279 return __libcpp_operator_delete(__ptr, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000280#else
Louis Dionne11a08d02020-09-25 09:24:14 -0400281 return __libcpp_operator_delete(__ptr, __size, __args...);
Richard Smith1f1c1472014-06-04 19:54:15 +0000282#endif
283}
284
Louis Dionne11a08d02020-09-25 09:24:14 -0400285inline _LIBCPP_INLINE_VISIBILITY
286void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
Eric Fiselier2856ef82018-10-25 17:21:30 +0000287#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Louis Dionne11a08d02020-09-25 09:24:14 -0400288 (void)__align;
Eric Fiselier2856ef82018-10-25 17:21:30 +0000289 return __do_deallocate_handle_size(__ptr, __size);
290#else
291 if (__is_overaligned_for_new(__align)) {
292 const align_val_t __align_val = static_cast<align_val_t>(__align);
293 return __do_deallocate_handle_size(__ptr, __size, __align_val);
294 } else {
295 return __do_deallocate_handle_size(__ptr, __size);
296 }
297#endif
Eric Fiselier2856ef82018-10-25 17:21:30 +0000298}
299
300inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
Louis Dionne11a08d02020-09-25 09:24:14 -0400301#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
302 (void)__align;
303 return __libcpp_operator_delete(__ptr);
304#else
305 if (__is_overaligned_for_new(__align)) {
306 const align_val_t __align_val = static_cast<align_val_t>(__align);
307 return __libcpp_operator_delete(__ptr, __align_val);
308 } else {
309 return __libcpp_operator_delete(__ptr);
310 }
311#endif
Richard Smith1f1c1472014-06-04 19:54:15 +0000312}
313
Louis Dionnea3922172020-11-12 15:14:33 -0500314#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
315// Low-level helpers to call the aligned allocation and deallocation functions
316// on the target platform. This is used to implement libc++'s own memory
317// allocation routines -- if you need to allocate memory inside the library,
318// chances are that you want to use `__libcpp_allocate` instead.
319//
320// Returns the allocated memory, or `nullptr` on failure.
321inline _LIBCPP_INLINE_VISIBILITY
322void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
323#if defined(_LIBCPP_MSVCRT_LIKE)
324 return ::_aligned_malloc(__size, __alignment);
325#else
326 void* __result = nullptr;
Vitaly Buka9dda87b2021-02-26 14:29:06 -0800327 (void)::posix_memalign(&__result, __alignment, __size);
Louis Dionnea3922172020-11-12 15:14:33 -0500328 // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
329 return __result;
330#endif
331}
332
333inline _LIBCPP_INLINE_VISIBILITY
334void __libcpp_aligned_free(void* __ptr) {
335#if defined(_LIBCPP_MSVCRT_LIKE)
336 ::_aligned_free(__ptr);
337#else
338 ::free(__ptr);
339#endif
340}
341#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
342
343
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000344template <class _Tp>
Louis Dionneade3f4a2021-08-31 10:29:22 -0400345_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000346_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
347{
348 static_assert (!(is_function<_Tp>::value), "can't launder functions" );
349 static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000350 return __builtin_launder(__p);
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000351}
352
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000353#if _LIBCPP_STD_VER > 14
354template <class _Tp>
Louis Dionneade3f4a2021-08-31 10:29:22 -0400355_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000356constexpr _Tp* launder(_Tp* __p) noexcept
357{
Marshall Clow6ac349c2017-11-23 01:25:03 +0000358 return _VSTD::__launder(__p);
Marshall Clowdf5a11f2017-11-22 19:49:03 +0000359}
360#endif
361
Richard Smith1f1c1472014-06-04 19:54:15 +0000362_LIBCPP_END_NAMESPACE_STD
363
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400364#endif // _LIBCPP_NEW