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