xref: /llvm-project-15.0.7/libcxx/include/new (revision a852cf66)
1// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
4// 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
7//
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:
23    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;
27};
28
29class bad_array_new_length : public bad_alloc // C++14
30{
31public:
32    bad_array_new_length() noexcept;
33};
34
35enum class align_val_t : size_t {}; // C++17
36
37struct destroying_delete_t { // C++20
38  explicit destroying_delete_t() = default;
39};
40inline constexpr destroying_delete_t destroying_delete{}; // C++20
41
42struct nothrow_t { explicit nothrow_t() = default; };
43extern const nothrow_t nothrow;
44typedef void (*new_handler)();
45new_handler set_new_handler(new_handler new_p) noexcept;
46new_handler get_new_handler() noexcept;
47
48// 21.6.4, pointer optimization barrier
49template <class T> constexpr T* launder(T* p) noexcept; // C++17
50}  // std
51
52void* 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
55void* operator new(std::size_t size, std::align_val_t alignment,
56                   const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++2a
57void  operator delete(void* ptr) noexcept;                              // replaceable
58void  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
59void  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
62void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
63void  operator delete(void* ptr, std:align_val_t alignment,
64                      const std::nothrow_t&) noexcept;                  // replaceable, C++17
65
66void* operator new[](std::size_t size);                                 // replaceable, nodiscard in C++2a
67void* operator new[](std::size_t size,
68                     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
70void* operator new[](std::size_t size, std::align_val_t alignment,
71                     const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++2a
72void  operator delete[](void* ptr) noexcept;                            // replaceable
73void  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
74void  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
78void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
79void  operator delete[](void* ptr, std::align_val_t alignment,
80                        const std::nothrow_t&) noexcept;                // replaceable, C++17
81
82void* 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
84void  operator delete  (void* ptr, void*) noexcept;
85void  operator delete[](void* ptr, void*) noexcept;
86
87*/
88
89#include <__config>
90#include <__availability>
91#include <exception>
92#include <type_traits>
93#include <cstddef>
94#include <version>
95#ifdef _LIBCPP_NO_EXCEPTIONS
96#include <cstdlib>
97#endif
98
99#if defined(_LIBCPP_ABI_VCRUNTIME)
100#include <new.h>
101#endif
102
103#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
104#pragma GCC system_header
105#endif
106
107#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)
113# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
114#endif
115
116#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
117    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
118# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
119#endif
120
121namespace std  // purposefully not using versioning namespace
122{
123
124#if !defined(_LIBCPP_ABI_VCRUNTIME)
125struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
126extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
127
128class _LIBCPP_EXCEPTION_ABI bad_alloc
129    : public exception
130{
131public:
132    bad_alloc() _NOEXCEPT;
133    virtual ~bad_alloc() _NOEXCEPT;
134    virtual const char* what() const _NOEXCEPT;
135};
136
137class _LIBCPP_EXCEPTION_ABI bad_array_new_length
138    : public bad_alloc
139{
140public:
141    bad_array_new_length() _NOEXCEPT;
142    virtual ~bad_array_new_length() _NOEXCEPT;
143    virtual const char* what() const _NOEXCEPT;
144};
145
146typedef 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
150#endif // !_LIBCPP_ABI_VCRUNTIME
151
152_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
153
154#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
155    !defined(_LIBCPP_ABI_VCRUNTIME)
156#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
163#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
172}  // std
173
174#if defined(_LIBCPP_CXX03_LANG)
175#define _THROW_BAD_ALLOC throw(std::bad_alloc)
176#else
177#define _THROW_BAD_ALLOC
178#endif
179
180#if !defined(_LIBCPP_ABI_VCRUNTIME)
181
182_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
183_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
184_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;
186#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
187_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
188#endif
189
190_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
191_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
192_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;
194#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
195_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
196#endif
197
198#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
199_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
200_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;
201_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;
203#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
204_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
205#endif
206
207_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
208_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;
209_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;
211#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
212_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
213#endif
214#endif
215
216_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;}
218inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
219inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
220
221#endif // !_LIBCPP_ABI_VCRUNTIME
222
223_LIBCPP_BEGIN_NAMESPACE_STD
224
225_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
233template <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
253inline _LIBCPP_INLINE_VISIBILITY
254void *__libcpp_allocate(size_t __size, size_t __align) {
255#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);
258    return __libcpp_operator_new(__size, __align_val);
259  }
260#endif
261
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...);
272#else
273  return __libcpp_operator_delete(__ptr, __size, __args...);
274#endif
275}
276
277inline _LIBCPP_INLINE_VISIBILITY
278void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
279#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
280    (void)__align;
281    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
290}
291
292inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
293#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
304}
305
306template <class _Tp>
307_LIBCPP_NODISCARD_AFTER_CXX17 inline
308_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{
325    return _VSTD::__launder(__p);
326}
327#endif
328
329_LIBCPP_END_NAMESPACE_STD
330
331#endif  // _LIBCPP_NEW
332