1//===--------------------------- new.cpp ----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <stdlib.h>
10
11#include "new"
12#include "include/atomic_support.h"
13
14#if defined(_LIBCPP_ABI_MICROSOFT)
15#   if !defined(_LIBCPP_ABI_VCRUNTIME)
16#       include "support/runtime/new_handler_fallback.ipp"
17#   endif
18#elif defined(LIBCXX_BUILDING_LIBCXXABI)
19#   include <cxxabi.h>
20#elif defined(LIBCXXRT)
21#   include <cxxabi.h>
22#   include "support/runtime/new_handler_fallback.ipp"
23#elif defined(__GLIBCXX__)
24    // nothing to do
25#else
26#   include "support/runtime/new_handler_fallback.ipp"
27#endif
28
29namespace std
30{
31
32#ifndef __GLIBCXX__
33const nothrow_t nothrow{};
34#endif
35
36#ifndef LIBSTDCXX
37
38void
39__throw_bad_alloc()
40{
41#ifndef _LIBCPP_NO_EXCEPTIONS
42    throw bad_alloc();
43#else
44    _VSTD::abort();
45#endif
46}
47
48#endif // !LIBSTDCXX
49
50}  // std
51
52#if !defined(__GLIBCXX__) &&                                                   \
53    !defined(_LIBCPP_ABI_VCRUNTIME) &&      \
54    !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
55
56// Implement all new and delete operators as weak definitions
57// in this shared library, so that they can be overridden by programs
58// that define non-weak copies of the functions.
59
60_LIBCPP_WEAK
61void *
62operator new(std::size_t size) _THROW_BAD_ALLOC
63{
64    if (size == 0)
65        size = 1;
66    void* p;
67    while ((p = ::malloc(size)) == 0)
68    {
69        // If malloc fails and there is a new_handler,
70        // call it to try free up memory.
71        std::new_handler nh = std::get_new_handler();
72        if (nh)
73            nh();
74        else
75#ifndef _LIBCPP_NO_EXCEPTIONS
76            throw std::bad_alloc();
77#else
78            break;
79#endif
80    }
81    return p;
82}
83
84_LIBCPP_WEAK
85void*
86operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
87{
88    void* p = 0;
89#ifndef _LIBCPP_NO_EXCEPTIONS
90    try
91    {
92#endif  // _LIBCPP_NO_EXCEPTIONS
93        p = ::operator new(size);
94#ifndef _LIBCPP_NO_EXCEPTIONS
95    }
96    catch (...)
97    {
98    }
99#endif  // _LIBCPP_NO_EXCEPTIONS
100    return p;
101}
102
103_LIBCPP_WEAK
104void*
105operator new[](size_t size) _THROW_BAD_ALLOC
106{
107    return ::operator new(size);
108}
109
110_LIBCPP_WEAK
111void*
112operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
113{
114    void* p = 0;
115#ifndef _LIBCPP_NO_EXCEPTIONS
116    try
117    {
118#endif  // _LIBCPP_NO_EXCEPTIONS
119        p = ::operator new[](size);
120#ifndef _LIBCPP_NO_EXCEPTIONS
121    }
122    catch (...)
123    {
124    }
125#endif  // _LIBCPP_NO_EXCEPTIONS
126    return p;
127}
128
129_LIBCPP_WEAK
130void
131operator delete(void* ptr) _NOEXCEPT
132{
133    ::free(ptr);
134}
135
136_LIBCPP_WEAK
137void
138operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
139{
140    ::operator delete(ptr);
141}
142
143_LIBCPP_WEAK
144void
145operator delete(void* ptr, size_t) _NOEXCEPT
146{
147    ::operator delete(ptr);
148}
149
150_LIBCPP_WEAK
151void
152operator delete[] (void* ptr) _NOEXCEPT
153{
154    ::operator delete(ptr);
155}
156
157_LIBCPP_WEAK
158void
159operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
160{
161    ::operator delete[](ptr);
162}
163
164_LIBCPP_WEAK
165void
166operator delete[] (void* ptr, size_t) _NOEXCEPT
167{
168    ::operator delete[](ptr);
169}
170
171#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
172
173_LIBCPP_WEAK
174void *
175operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
176{
177    if (size == 0)
178        size = 1;
179    if (static_cast<size_t>(alignment) < sizeof(void*))
180      alignment = std::align_val_t(sizeof(void*));
181    void* p;
182#if defined(_LIBCPP_MSVCRT_LIKE)
183    while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
184#else
185    while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
186#endif
187    {
188        // If posix_memalign fails and there is a new_handler,
189        // call it to try free up memory.
190        std::new_handler nh = std::get_new_handler();
191        if (nh)
192            nh();
193        else {
194#ifndef _LIBCPP_NO_EXCEPTIONS
195            throw std::bad_alloc();
196#else
197            p = nullptr; // posix_memalign doesn't initialize 'p' on failure
198            break;
199#endif
200        }
201    }
202    return p;
203}
204
205_LIBCPP_WEAK
206void*
207operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
208{
209    void* p = 0;
210#ifndef _LIBCPP_NO_EXCEPTIONS
211    try
212    {
213#endif  // _LIBCPP_NO_EXCEPTIONS
214        p = ::operator new(size, alignment);
215#ifndef _LIBCPP_NO_EXCEPTIONS
216    }
217    catch (...)
218    {
219    }
220#endif  // _LIBCPP_NO_EXCEPTIONS
221    return p;
222}
223
224_LIBCPP_WEAK
225void*
226operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
227{
228    return ::operator new(size, alignment);
229}
230
231_LIBCPP_WEAK
232void*
233operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
234{
235    void* p = 0;
236#ifndef _LIBCPP_NO_EXCEPTIONS
237    try
238    {
239#endif  // _LIBCPP_NO_EXCEPTIONS
240        p = ::operator new[](size, alignment);
241#ifndef _LIBCPP_NO_EXCEPTIONS
242    }
243    catch (...)
244    {
245    }
246#endif  // _LIBCPP_NO_EXCEPTIONS
247    return p;
248}
249
250_LIBCPP_WEAK
251void
252operator delete(void* ptr, std::align_val_t) _NOEXCEPT
253{
254#if defined(_LIBCPP_MSVCRT_LIKE)
255    ::_aligned_free(ptr);
256#else
257    ::free(ptr);
258#endif
259}
260
261_LIBCPP_WEAK
262void
263operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
264{
265    ::operator delete(ptr, alignment);
266}
267
268_LIBCPP_WEAK
269void
270operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
271{
272    ::operator delete(ptr, alignment);
273}
274
275_LIBCPP_WEAK
276void
277operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
278{
279    ::operator delete(ptr, alignment);
280}
281
282_LIBCPP_WEAK
283void
284operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
285{
286    ::operator delete[](ptr, alignment);
287}
288
289_LIBCPP_WEAK
290void
291operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
292{
293    ::operator delete[](ptr, alignment);
294}
295
296#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
297#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
298