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 <__type_traits/is_function.h>
93#include <__type_traits/is_same.h>
94#include <__type_traits/remove_cv.h>
95#include <cstddef>
96#include <cstdlib>
97#include <exception>
98#include <version>
99
100#if defined(_LIBCPP_ABI_VCRUNTIME)
101#include <new.h>
102#endif
103
104#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
105#  pragma GCC system_header
106#endif
107
108#if !defined(__cpp_sized_deallocation) || __cpp_sized_deallocation  < 201309L
109#define _LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION
110#endif
111
112#if !defined(_LIBCPP_BUILDING_LIBRARY) && _LIBCPP_STD_VER < 14 && \
113    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
114# define _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
115#endif
116
117#if defined(_LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION) || \
118    defined(_LIBCPP_HAS_NO_LANGUAGE_SIZED_DEALLOCATION)
119# define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
120#endif
121
122namespace std  // purposefully not using versioning namespace
123{
124
125#if !defined(_LIBCPP_ABI_VCRUNTIME)
126struct _LIBCPP_TYPE_VIS nothrow_t { explicit nothrow_t() = default; };
127extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
128
129class _LIBCPP_EXCEPTION_ABI bad_alloc
130    : public exception
131{
132public:
133    bad_alloc() _NOEXCEPT;
134    ~bad_alloc() _NOEXCEPT override;
135    const char* what() const _NOEXCEPT override;
136};
137
138class _LIBCPP_EXCEPTION_ABI bad_array_new_length
139    : public bad_alloc
140{
141public:
142    bad_array_new_length() _NOEXCEPT;
143    ~bad_array_new_length() _NOEXCEPT override;
144    const char* what() const _NOEXCEPT override;
145};
146
147typedef void (*new_handler)();
148_LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
149_LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
150
151#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
152
153// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
154// since they would normally be provided in vcruntime_exception.h
155class bad_alloc : public exception {
156public:
157  bad_alloc() noexcept : exception("bad allocation") {}
158
159private:
160  friend class bad_array_new_length;
161
162  bad_alloc(char const* const __message) noexcept : exception(__message) {}
163};
164
165class bad_array_new_length : public bad_alloc {
166public:
167  bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
168};
169#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
170
171_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
172
173_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
174void __throw_bad_array_new_length()
175{
176#ifndef _LIBCPP_NO_EXCEPTIONS
177    throw bad_array_new_length();
178#else
179    _VSTD::abort();
180#endif
181}
182
183#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) && \
184    !defined(_LIBCPP_ABI_VCRUNTIME)
185#ifndef _LIBCPP_CXX03_LANG
186enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
187#else
188enum align_val_t { __zero = 0, __max = (size_t)-1 };
189#endif
190#endif
191
192#if _LIBCPP_STD_VER > 17
193// Enable the declaration even if the compiler doesn't support the language
194// feature.
195struct destroying_delete_t {
196  explicit destroying_delete_t() = default;
197};
198inline constexpr destroying_delete_t destroying_delete{};
199#endif // _LIBCPP_STD_VER > 17
200
201} // namespace std
202
203#if defined(_LIBCPP_CXX03_LANG)
204#define _THROW_BAD_ALLOC throw(std::bad_alloc)
205#else
206#define _THROW_BAD_ALLOC
207#endif
208
209#if !defined(_LIBCPP_ABI_VCRUNTIME)
210
211_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
212_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
213_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
214_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
215#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
216_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
217#endif
218
219_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
220_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _LIBCPP_NOALIAS;
221_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
222_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
223#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
224_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
225#endif
226
227#ifndef _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
228_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
229_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;
230_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
231_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
232#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
233_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
234#endif
235
236_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
237_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;
238_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
239_LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
240#ifndef _LIBCPP_HAS_NO_LIBRARY_SIZED_DEALLOCATION
241_LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
242#endif
243#endif
244
245_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
246_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
247inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
248inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
249
250#endif // !_LIBCPP_ABI_VCRUNTIME
251
252_LIBCPP_BEGIN_NAMESPACE_STD
253
254_LIBCPP_CONSTEXPR inline _LIBCPP_INLINE_VISIBILITY bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
255#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
256  return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
257#else
258  return __align > alignment_of<max_align_t>::value;
259#endif
260}
261
262template <class ..._Args>
263_LIBCPP_INLINE_VISIBILITY
264void* __libcpp_operator_new(_Args ...__args) {
265#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
266  return __builtin_operator_new(__args...);
267#else
268  return ::operator new(__args...);
269#endif
270}
271
272template <class ..._Args>
273_LIBCPP_INLINE_VISIBILITY
274void __libcpp_operator_delete(_Args ...__args) {
275#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
276  __builtin_operator_delete(__args...);
277#else
278  ::operator delete(__args...);
279#endif
280}
281
282inline _LIBCPP_INLINE_VISIBILITY
283void *__libcpp_allocate(size_t __size, size_t __align) {
284#ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
285  if (__is_overaligned_for_new(__align)) {
286    const align_val_t __align_val = static_cast<align_val_t>(__align);
287    return __libcpp_operator_new(__size, __align_val);
288  }
289#endif
290
291  (void)__align;
292  return __libcpp_operator_new(__size);
293}
294
295template <class ..._Args>
296_LIBCPP_INLINE_VISIBILITY
297void __do_deallocate_handle_size(void *__ptr, size_t __size, _Args ...__args) {
298#ifdef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
299  (void)__size;
300  return std::__libcpp_operator_delete(__ptr, __args...);
301#else
302  return std::__libcpp_operator_delete(__ptr, __size, __args...);
303#endif
304}
305
306inline _LIBCPP_INLINE_VISIBILITY
307void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) {
308#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
309    (void)__align;
310    return __do_deallocate_handle_size(__ptr, __size);
311#else
312    if (__is_overaligned_for_new(__align)) {
313      const align_val_t __align_val = static_cast<align_val_t>(__align);
314      return __do_deallocate_handle_size(__ptr, __size, __align_val);
315    } else {
316      return __do_deallocate_handle_size(__ptr, __size);
317    }
318#endif
319}
320
321inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate_unsized(void* __ptr, size_t __align) {
322#if defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
323    (void)__align;
324    return __libcpp_operator_delete(__ptr);
325#else
326    if (__is_overaligned_for_new(__align)) {
327      const align_val_t __align_val = static_cast<align_val_t>(__align);
328      return __libcpp_operator_delete(__ptr, __align_val);
329    } else {
330      return __libcpp_operator_delete(__ptr);
331    }
332#endif
333}
334
335#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
336// Low-level helpers to call the aligned allocation and deallocation functions
337// on the target platform. This is used to implement libc++'s own memory
338// allocation routines -- if you need to allocate memory inside the library,
339// chances are that you want to use `__libcpp_allocate` instead.
340//
341// Returns the allocated memory, or `nullptr` on failure.
342inline _LIBCPP_INLINE_VISIBILITY void* __libcpp_aligned_alloc(std::size_t __alignment, std::size_t __size) {
343#  if defined(_LIBCPP_MSVCRT_LIKE)
344    return ::_aligned_malloc(__size, __alignment);
345#  elif _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_C11_ALIGNED_ALLOC)
346    // aligned_alloc() requires that __size is a multiple of __alignment,
347    // but for C++ [new.delete.general], only states "if the value of an
348    // alignment argument passed to any of these functions is not a valid
349    // alignment value, the behavior is undefined".
350    // To handle calls such as ::operator new(1, std::align_val_t(128)), we
351    // round __size up to the next multiple of __alignment.
352    size_t __rounded_size = (__size + __alignment - 1) & ~(__alignment - 1);
353    // Rounding up could have wrapped around to zero, so we have to add another
354    // max() ternary to the actual call site to avoid succeeded in that case.
355    return ::aligned_alloc(__alignment, __size > __rounded_size ? __size : __rounded_size);
356#  else
357    void* __result = nullptr;
358    (void)::posix_memalign(&__result, __alignment, __size);
359    // If posix_memalign fails, __result is unmodified so we still return `nullptr`.
360    return __result;
361#  endif
362}
363
364inline _LIBCPP_INLINE_VISIBILITY
365void __libcpp_aligned_free(void* __ptr) {
366#if defined(_LIBCPP_MSVCRT_LIKE)
367  ::_aligned_free(__ptr);
368#else
369  ::free(__ptr);
370#endif
371}
372#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
373
374
375template <class _Tp>
376_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
377_LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
378{
379    static_assert (!(is_function<_Tp>::value), "can't launder functions" );
380    static_assert (!(is_same<void, __remove_cv_t<_Tp> >::value), "can't launder cv-void" );
381    return __builtin_launder(__p);
382}
383
384#if _LIBCPP_STD_VER > 14
385template <class _Tp>
386_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
387constexpr _Tp* launder(_Tp* __p) noexcept
388{
389    return _VSTD::__launder(__p);
390}
391#endif
392
393#if _LIBCPP_STD_VER > 14
394
395#if defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
396
397inline constexpr size_t hardware_destructive_interference_size = __GCC_DESTRUCTIVE_SIZE;
398inline constexpr size_t hardware_constructive_interference_size = __GCC_CONSTRUCTIVE_SIZE;
399
400#endif // defined(__GCC_DESTRUCTIVE_SIZE) && defined(__GCC_CONSTRUCTIVE_SIZE)
401
402#endif // _LIBCPP_STD_VER > 14
403
404_LIBCPP_END_NAMESPACE_STD
405
406#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
407#  include <type_traits>
408#endif
409
410#endif // _LIBCPP_NEW
411