xref: /llvm-project-15.0.7/libcxx/include/new (revision bacf751a)
1// -*- C++ -*-
2//===----------------------------- new ------------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_NEW
12#define _LIBCPP_NEW
13
14/*
15    new synopsis
16
17namespace std
18{
19
20class bad_alloc
21    : public exception
22{
23public:
24    bad_alloc() noexcept;
25    bad_alloc(const bad_alloc&) noexcept;
26    bad_alloc& operator=(const bad_alloc&) noexcept;
27    virtual const char* what() const noexcept;
28};
29
30class bad_array_length : public bad_alloc // FIXME: Not part of C++
31{
32public:
33    bad_array_length() noexcept;
34};
35
36class bad_array_new_length : public bad_alloc // C++14
37{
38public:
39    bad_array_new_length() noexcept;
40};
41
42enum class align_val_t : size_t {}; // C++17
43struct nothrow_t {};
44extern const nothrow_t nothrow;
45typedef void (*new_handler)();
46new_handler set_new_handler(new_handler new_p) noexcept;
47new_handler get_new_handler() noexcept;
48
49// 21.6.4, pointer optimization barrier
50template <class T> constexpr T* launder(T* p) noexcept; // C++17
51}  // std
52
53void* operator new(std::size_t size);                                   // replaceable, nodiscard in C++2a
54void* operator new(std::size_t size, std::align_val_t alignment);       // replaceable, C++17, nodiscard in C++2a
55void* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable, nodiscard in C++2a
56void* operator new(std::size_t size, std::align_val_t alignment,
57                   const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++2a
58void  operator delete(void* ptr) noexcept;                              // replaceable
59void  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
60void  operator delete(void* ptr, std::align_val_t alignment) noexcept;  // replaceable, C++17
61void  operator delete(void* ptr, std::size_t size,
62                      std::align_val_t alignment) noexcept;             // replaceable, C++17
63void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
64void  operator delete(void* ptr, std:align_val_t alignment,
65                      const std::nothrow_t&) noexcept;                  // replaceable, C++17
66
67void* operator new[](std::size_t size);                                 // replaceable, nodiscard in C++2a
68void* operator new[](std::size_t size,
69                     std::align_val_t alignment) noexcept;              // replaceable, C++17, nodiscard in C++2a
70void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
71void* operator new[](std::size_t size, std::align_val_t alignment,
72                     const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++2a
73void  operator delete[](void* ptr) noexcept;                            // replaceable
74void  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
75void  operator delete[](void* ptr,
76                        std::align_val_t alignment) noexcept;           // replaceable, C++17
77void  operator delete[](void* ptr, std::size_t size,
78                        std::align_val_t alignment) noexcept;           // replaceable, C++17
79void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
80void  operator delete[](void* ptr, std::align_val_t alignment,
81                        const std::nothrow_t&) noexcept;                // replaceable, C++17
82
83void* operator new  (std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
84void* operator new[](std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
85void  operator delete  (void* ptr, void*) noexcept;
86void  operator delete[](void* ptr, void*) noexcept;
87
88*/
89
90#include <__config>
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_MICROSOFT) && !defined(_LIBCPP_NO_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
121#if !__has_builtin(__builtin_operator_new) || \
122   __has_builtin(__builtin_operator_new) < 201802L
123#define _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
124#endif
125
126namespace std  // purposefully not using versioning namespace
127{
128
129#if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
130struct _LIBCPP_TYPE_VIS nothrow_t {};
131extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
132
133class _LIBCPP_EXCEPTION_ABI bad_alloc
134    : public exception
135{
136public:
137    bad_alloc() _NOEXCEPT;
138    virtual ~bad_alloc() _NOEXCEPT;
139    virtual const char* what() const _NOEXCEPT;
140};
141
142class _LIBCPP_EXCEPTION_ABI bad_array_new_length
143    : public bad_alloc
144{
145public:
146    bad_array_new_length() _NOEXCEPT;
147    virtual ~bad_array_new_length() _NOEXCEPT;
148    virtual const char* what() const _NOEXCEPT;
149};
150
151typedef void (*new_handler)();
152_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
153_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
154
155#endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
156
157_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
158
159#if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
160
161class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
162    bad_array_length : public bad_alloc {
163public:
164    bad_array_length() _NOEXCEPT;
165    virtual ~bad_array_length() _NOEXCEPT;
166    virtual const char* what() const _NOEXCEPT;
167};
168
169#define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
170
171#endif  // defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
172
173#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
174    !defined(_LIBCPP_DEFER_NEW_TO_VCRUNTIME)
175#ifndef _LIBCPP_CXX03_LANG
176enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
177#else
178enum align_val_t { __zero = 0, __max = (size_t)-1 };
179#endif
180#endif
181
182}  // std
183
184#if defined(_LIBCPP_CXX03_LANG)
185#define _THROW_BAD_ALLOC throw(std::bad_alloc)
186#else
187#define _THROW_BAD_ALLOC
188#endif
189
190#if !defined(_LIBCPP_DEFER_NEW_TO_VCRUNTIME)
191
192_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
193_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
194_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
195_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
196#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
197_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
198#endif
199
200_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
201_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
202_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
203_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
204#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
205_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
206#endif
207
208#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
209_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
210_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
211_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
212_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
213#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
214_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
215#endif
216
217_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
218_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
219_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
220_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
221#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
222_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
223#endif
224#endif
225
226_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
227_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
228inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
229inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
230
231#endif // !_LIBCPP_DEFER_NEW_TO_VCRUNTIME
232
233_LIBCPP_BEGIN_NAMESPACE_STD
234
235_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
236#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
237  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
238#else
239  return __align > alignment_of<max_align_t>::value;
240#endif
241}
242
243inline _LIBCPP_INLINE_VISIBILITY void *__libcpp_allocate(size_t __size, size_t __align) {
244#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
245  if (__is_overaligned_for_new(__align)) {
246    const align_val_t __align_val = static_cast<align_val_t>(__align);
247# ifdef _LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE
248    return ::operator new(__size, __align_val);
249# else
250    return __builtin_operator_new(__size, __align_val);
251# endif
252  }
253#else
254  ((void)__align);
255#endif
256#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
257  return ::operator new(__size);
258#else
259  return __builtin_operator_new(__size);
260#endif
261}
262
263struct _DeallocateCaller {
264  static inline _LIBCPP_INLINE_VISIBILITY
265  void __do_deallocate_handle_size_align(void *__ptr, size_t __size, size_t __align) {
266#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
267    ((void)__align);
268    return __do_deallocate_handle_size(__ptr, __size);
269#else
270    if (__is_overaligned_for_new(__align)) {
271      const align_val_t __align_val = static_cast<align_val_t>(__align);
272      return __do_deallocate_handle_size(__ptr, __size, __align_val);
273    } else {
274      return __do_deallocate_handle_size(__ptr, __size);
275    }
276#endif
277  }
278
279  static inline _LIBCPP_INLINE_VISIBILITY
280  void __do_deallocate_handle_align(void *__ptr, size_t __align) {
281#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
282    ((void)__align);
283    return __do_call(__ptr);
284#else
285    if (__is_overaligned_for_new(__align)) {
286      const align_val_t __align_val = static_cast<align_val_t>(__align);
287      return __do_call(__ptr, __align_val);
288    } else {
289      return __do_call(__ptr);
290    }
291#endif
292  }
293
294 private:
295  static inline void __do_deallocate_handle_size(void *__ptr, size_t __size) {
296#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
297    ((void)__size);
298    return __do_call(__ptr);
299#else
300    return __do_call(__ptr, __size);
301#endif
302  }
303
304#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
305  static inline void __do_deallocate_handle_size(void *__ptr, size_t __size, align_val_t __align) {
306#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
307    ((void)__size);
308    return __do_call(__ptr, __align);
309#else
310    return __do_call(__ptr, __size, __align);
311#endif
312  }
313#endif
314
315private:
316  template <class _A1, class _A2>
317  static inline void __do_call(void *__ptr, _A1 __a1, _A2 __a2) {
318#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
319    defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
320    return ::operator delete(__ptr, __a1, __a2);
321#else
322    return __builtin_operator_delete(__ptr, __a1, __a2);
323#endif
324  }
325
326  template <class _A1>
327  static inline void __do_call(void *__ptr, _A1 __a1) {
328#if defined(_LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE) || \
329    defined(_LIBCPP_HAS_NO_BUILTIN_OVERLOADED_OPERATOR_NEW_DELETE)
330    return ::operator delete(__ptr, __a1);
331#else
332    return __builtin_operator_delete(__ptr, __a1);
333#endif
334  }
335
336  static inline void __do_call(void *__ptr) {
337#ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
338    return ::operator delete(__ptr);
339#else
340    return __builtin_operator_delete(__ptr);
341#endif
342  }
343};
344
345inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
346  _DeallocateCaller::__do_deallocate_handle_size_align(__ptr, __size, __align);
347}
348
349inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
350  _DeallocateCaller::__do_deallocate_handle_align(__ptr, __align);
351}
352
353#ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
354_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
355#ifndef _LIBCPP_NO_EXCEPTIONS
356_LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
357#endif
358void __throw_bad_array_length()
359{
360#ifndef _LIBCPP_NO_EXCEPTIONS
361    throw bad_array_length();
362#else
363    _VSTD::abort();
364#endif
365}
366#endif
367
368template <class _Tp>
369_LIBCPP_NODISCARD_AFTER_CXX17 inline
370_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
371{
372    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
373    static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
374#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
375    return __builtin_launder(__p);
376#else
377    return __p;
378#endif
379}
380
381
382#if _LIBCPP_STD_VER > 14
383template <class _Tp>
384_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
385constexpr _Tp* launder(_Tp* __p) noexcept
386{
387    return _VSTD::__launder(__p);
388}
389#endif
390
391_LIBCPP_END_NAMESPACE_STD
392
393#endif  // _LIBCPP_NEW
394