xref: /freebsd/contrib/llvm-project/libcxx/include/new (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
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> [[nodiscard]] constexpr T* launder(T* p) noexcept;   // C++17, nodiscard since C++20
50}  // std
51
52void* 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
55void* operator new(std::size_t size, std::align_val_t alignment,
56                   const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++20
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++20
67void* operator new[](std::size_t size,
68                     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
70void* operator new[](std::size_t size, std::align_val_t alignment,
71                     const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++20
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++20
83void* operator new[](std::size_t size, void* ptr) noexcept;             // nodiscard in C++20
84void  operator delete  (void* ptr, void*) noexcept;
85void  operator delete[](void* ptr, void*) noexcept;
86
87*/
88
89#include <__assert> // all public C++ headers provide the assertion handler
90#include <__availability>
91#include <__config>
92#include <__exception/exception.h>
93#include <__type_traits/is_function.h>
94#include <__type_traits/is_same.h>
95#include <__type_traits/remove_cv.h>
96#include <cstddef>
97#include <version>
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_EXPORTED_FROM_ABI nothrow_t { explicit nothrow_t() = default; };
126extern _LIBCPP_EXPORTED_FROM_ABI const nothrow_t nothrow;
127
128class _LIBCPP_EXPORTED_FROM_ABI bad_alloc
129    : public exception
130{
131public:
132    bad_alloc() _NOEXCEPT;
133    _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
134    _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
135    ~bad_alloc() _NOEXCEPT override;
136    const char* what() const _NOEXCEPT override;
137};
138
139class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length
140    : public bad_alloc
141{
142public:
143    bad_array_new_length() _NOEXCEPT;
144    _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
145    _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
146    ~bad_array_new_length() _NOEXCEPT override;
147    const char* what() const _NOEXCEPT override;
148};
149
150typedef void (*new_handler)();
151_LIBCPP_EXPORTED_FROM_ABI new_handler set_new_handler(new_handler) _NOEXCEPT;
152_LIBCPP_EXPORTED_FROM_ABI new_handler get_new_handler() _NOEXCEPT;
153
154#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
155
156// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
157// since they would normally be provided in vcruntime_exception.h
158class bad_alloc : public exception {
159public:
160  bad_alloc() noexcept : exception("bad allocation") {}
161
162private:
163  friend class bad_array_new_length;
164
165  bad_alloc(char const* const __message) noexcept : exception(__message) {}
166};
167
168class bad_array_new_length : public bad_alloc {
169public:
170  bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
171};
172#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
173
174_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc();  // not in C++ spec
175
176_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
177void __throw_bad_array_new_length()
178{
179#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
180    throw bad_array_new_length();
181#else
182    _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
183#endif
184}
185
186#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
187    !defined(_LIBCPP_ABI_VCRUNTIME)
188#ifndef _LIBCPP_CXX03_LANG
189enum class align_val_t : size_t { };
190#else
191enum align_val_t { __zero = 0, __max = (size_t)-1 };
192#endif
193#endif
194
195#if _LIBCPP_STD_VER >= 20
196// Enable the declaration even if the compiler doesn't support the language
197// feature.
198struct destroying_delete_t {
199  explicit destroying_delete_t() = default;
200};
201inline constexpr destroying_delete_t destroying_delete{};
202#endif // _LIBCPP_STD_VER >= 20
203
204} // namespace std
205
206#if defined(_LIBCPP_CXX03_LANG)
207#define _THROW_BAD_ALLOC throw(std::bad_alloc)
208#else
209#define _THROW_BAD_ALLOC
210#endif
211
212#if !defined(_LIBCPP_ABI_VCRUNTIME)
213
214_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
215_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
216_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
217_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
218#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
219_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
220#endif
221
222_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
223_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
224_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
225_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
226#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
227_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
228#endif
229
230#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
231_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
232_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;
233_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
234_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
235#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
236_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
237#endif
238
239_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
240_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;
241_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
242_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
243#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
244_LIBCPP_OVERRIDABLE_FUNC_VIS void operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
245#endif
246#endif
247
248_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
249_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
250inline _LIBCPP_HIDE_FROM_ABI void  operator delete  (void*, void*) _NOEXCEPT {}
251inline _LIBCPP_HIDE_FROM_ABI void  operator delete[](void*, void*) _NOEXCEPT {}
252
253#endif // !_LIBCPP_ABI_VCRUNTIME
254
255_LIBCPP_BEGIN_NAMESPACE_STD
256
257_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
258#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
259  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
260#else
261  return __align > _LIBCPP_ALIGNOF(max_align_t);
262#endif
263}
264
265template <class ..._Args>
266_LIBCPP_HIDE_FROM_ABI
267void* __libcpp_operator_new(_Args ...__args) {
268#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
269  return __builtin_operator_new(__args...);
270#else
271  return ::operator new(__args...);
272#endif
273}
274
275template <class ..._Args>
276_LIBCPP_HIDE_FROM_ABI
277void __libcpp_operator_delete(_Args ...__args) {
278#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
279  __builtin_operator_delete(__args...);
280#else
281  ::operator delete(__args...);
282#endif
283}
284
285inline _LIBCPP_HIDE_FROM_ABI
286void *__libcpp_allocate(size_t __size, size_t __align) {
287#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
288  if (__is_overaligned_for_new(__align)) {
289    const align_val_t __align_val = static_cast<align_val_t>(__align);
290    return __libcpp_operator_new(__size, __align_val);
291  }
292#endif
293
294  (void)__align;
295  return __libcpp_operator_new(__size);
296}
297
298template <class ..._Args>
299_LIBCPP_HIDE_FROM_ABI
300void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
301#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
302  (void)__size;
303  return std::__libcpp_operator_delete(__ptr, __args...);
304#else
305  return std::__libcpp_operator_delete(__ptr, __size, __args...);
306#endif
307}
308
309inline _LIBCPP_HIDE_FROM_ABI
310void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
311#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
312    (void)__align;
313    return __do_deallocate_handle_size(__ptr, __size);
314#else
315    if (__is_overaligned_for_new(__align)) {
316      const align_val_t __align_val = static_cast<align_val_t>(__align);
317      return __do_deallocate_handle_size(__ptr, __size, __align_val);
318    } else {
319      return __do_deallocate_handle_size(__ptr, __size);
320    }
321#endif
322}
323
324inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
325#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
326    (void)__align;
327    return __libcpp_operator_delete(__ptr);
328#else
329    if (__is_overaligned_for_new(__align)) {
330      const align_val_t __align_val = static_cast<align_val_t>(__align);
331      return __libcpp_operator_delete(__ptr, __align_val);
332    } else {
333      return __libcpp_operator_delete(__ptr);
334    }
335#endif
336}
337
338template <class _Tp>
339_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
340_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
341{
342    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
343    static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
344    return __builtin_launder(__p);
345}
346
347#if _LIBCPP_STD_VER >= 17
348template <class _Tp>
349_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
350constexpr _Tp* launder(_Tp* __p) noexcept
351{
352    return std::__launder(__p);
353}
354#endif
355
356#if _LIBCPP_STD_VER >= 17
357
358#if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
359
360inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
361inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
362
363#endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
364
365#endif // _LIBCPP_STD_VER >= 17
366
367_LIBCPP_END_NAMESPACE_STD
368
369#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
370#  include <cstdlib>
371#  include <exception>
372#  include <type_traits>
373#endif
374
375#endif // _LIBCPP_NEW
376