[LoongArch] Fix register type in FCMP_*_D instructions definition
[llvm-project.git] / libcxx / src / new.cpp
blob48d6f997fdda4f59d556a95e4524803e1603befe
1 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include <new>
10 #include <stdlib.h>
12 #include "include/atomic_support.h"
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
29 namespace std
32 #ifndef __GLIBCXX__
33 const nothrow_t nothrow{};
34 #endif
36 #ifndef LIBSTDCXX
38 void
39 __throw_bad_alloc()
41 #ifndef _LIBCPP_NO_EXCEPTIONS
42 throw bad_alloc();
43 #else
44 _VSTD::abort();
45 #endif
48 #endif // !LIBSTDCXX
50 } // std
52 #if !defined(__GLIBCXX__) && \
53 !defined(_LIBCPP_ABI_VCRUNTIME) && \
54 !defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
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.
60 _LIBCPP_WEAK
61 void *
62 operator new(std::size_t size) _THROW_BAD_ALLOC
64 if (size == 0)
65 size = 1;
66 void* p;
67 while ((p = ::malloc(size)) == nullptr)
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
81 return p;
84 _LIBCPP_WEAK
85 void*
86 operator new(size_t size, const std::nothrow_t&) noexcept
88 void* p = nullptr;
89 #ifndef _LIBCPP_NO_EXCEPTIONS
90 try
92 #endif // _LIBCPP_NO_EXCEPTIONS
93 p = ::operator new(size);
94 #ifndef _LIBCPP_NO_EXCEPTIONS
96 catch (...)
99 #endif // _LIBCPP_NO_EXCEPTIONS
100 return p;
103 _LIBCPP_WEAK
104 void*
105 operator new[](size_t size) _THROW_BAD_ALLOC
107 return ::operator new(size);
110 _LIBCPP_WEAK
111 void*
112 operator new[](size_t size, const std::nothrow_t&) noexcept
114 void* p = nullptr;
115 #ifndef _LIBCPP_NO_EXCEPTIONS
118 #endif // _LIBCPP_NO_EXCEPTIONS
119 p = ::operator new[](size);
120 #ifndef _LIBCPP_NO_EXCEPTIONS
122 catch (...)
125 #endif // _LIBCPP_NO_EXCEPTIONS
126 return p;
129 _LIBCPP_WEAK
130 void
131 operator delete(void* ptr) noexcept
133 ::free(ptr);
136 _LIBCPP_WEAK
137 void
138 operator delete(void* ptr, const std::nothrow_t&) noexcept
140 ::operator delete(ptr);
143 _LIBCPP_WEAK
144 void
145 operator delete(void* ptr, size_t) noexcept
147 ::operator delete(ptr);
150 _LIBCPP_WEAK
151 void
152 operator delete[] (void* ptr) noexcept
154 ::operator delete(ptr);
157 _LIBCPP_WEAK
158 void
159 operator delete[] (void* ptr, const std::nothrow_t&) noexcept
161 ::operator delete[](ptr);
164 _LIBCPP_WEAK
165 void
166 operator delete[] (void* ptr, size_t) noexcept
168 ::operator delete[](ptr);
171 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
173 _LIBCPP_WEAK
174 void *
175 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
177 if (size == 0)
178 size = 1;
179 if (static_cast<size_t>(alignment) < sizeof(void*))
180 alignment = std::align_val_t(sizeof(void*));
182 // Try allocating memory. If allocation fails and there is a new_handler,
183 // call it to try free up memory, and try again until it succeeds, or until
184 // the new_handler decides to terminate.
186 // If allocation fails and there is no new_handler, we throw bad_alloc
187 // (or return nullptr if exceptions are disabled).
188 void* p;
189 while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
191 std::new_handler nh = std::get_new_handler();
192 if (nh)
193 nh();
194 else {
195 #ifndef _LIBCPP_NO_EXCEPTIONS
196 throw std::bad_alloc();
197 #else
198 break;
199 #endif
202 return p;
205 _LIBCPP_WEAK
206 void*
207 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
209 void* p = nullptr;
210 #ifndef _LIBCPP_NO_EXCEPTIONS
213 #endif // _LIBCPP_NO_EXCEPTIONS
214 p = ::operator new(size, alignment);
215 #ifndef _LIBCPP_NO_EXCEPTIONS
217 catch (...)
220 #endif // _LIBCPP_NO_EXCEPTIONS
221 return p;
224 _LIBCPP_WEAK
225 void*
226 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
228 return ::operator new(size, alignment);
231 _LIBCPP_WEAK
232 void*
233 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
235 void* p = nullptr;
236 #ifndef _LIBCPP_NO_EXCEPTIONS
239 #endif // _LIBCPP_NO_EXCEPTIONS
240 p = ::operator new[](size, alignment);
241 #ifndef _LIBCPP_NO_EXCEPTIONS
243 catch (...)
246 #endif // _LIBCPP_NO_EXCEPTIONS
247 return p;
250 _LIBCPP_WEAK
251 void
252 operator delete(void* ptr, std::align_val_t) noexcept
254 std::__libcpp_aligned_free(ptr);
257 _LIBCPP_WEAK
258 void
259 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
261 ::operator delete(ptr, alignment);
264 _LIBCPP_WEAK
265 void
266 operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept
268 ::operator delete(ptr, alignment);
271 _LIBCPP_WEAK
272 void
273 operator delete[] (void* ptr, std::align_val_t alignment) noexcept
275 ::operator delete(ptr, alignment);
278 _LIBCPP_WEAK
279 void
280 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
282 ::operator delete[](ptr, alignment);
285 _LIBCPP_WEAK
286 void
287 operator delete[] (void* ptr, size_t, std::align_val_t alignment) noexcept
289 ::operator delete[](ptr, alignment);
292 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
293 #endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS