blob: 8e88d7f6c76c696e7914915f9b426398ee1278cb [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===--------------------------- new.cpp ----------------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
Howard Hinnantee11c312010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Marshall Clow3bf77132013-09-11 01:38:42 +000010#define _LIBCPP_BUILDING_NEW
11
Howard Hinnant155c2af2010-05-24 17:49:41 +000012#include <stdlib.h>
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000013
Howard Hinnantc51e1022010-05-11 19:42:16 +000014#include "new"
15
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000016#if defined(_LIBCPP_ABI_MICROSOFT)
17// nothing todo
18#elif defined(LIBCXX_BUILDING_LIBCXXABI)
19#include <cxxabi.h>
20#elif defined(LIBCXXRT)
21#include <cxxabi.h>
22#include "new_handler_fallback.ipp"
23#elif defined(__GLIBCXX__)
24// nothing todo
25#elif defined(_LIBCPP_BUILDING_NO_ABI_LIBRARY)
26// nothing todo
27#else
28#error UNSUPPORTED configuration
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000029#endif
30
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000031namespace std
32{
33
Peter Collingbournedc009952013-10-06 22:13:16 +000034#ifndef __GLIBCXX__
Eric Fiselier4e5fccc2017-02-10 04:25:33 +000035const nothrow_t nothrow = {};
36#endif
37
38#ifndef LIBSTDCXX
39
40void
41__throw_bad_alloc()
42{
43#ifndef _LIBCPP_NO_EXCEPTIONS
44 throw bad_alloc();
45#else
46 _VSTD::abort();
47#endif
48}
49
50#endif // !LIBSTDCXX
51
52} // std
53
54#if !defined(__GLIBCXX__) && !defined(_LIBCPP_ABI_MICROSOFT)
Peter Collingbournedc009952013-10-06 22:13:16 +000055
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000056// Implement all new and delete operators as weak definitions
Eric Fiseliercd4496b2015-08-20 05:23:16 +000057// in this shared library, so that they can be overridden by programs
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000058// that define non-weak copies of the functions.
59
Shoaib Meenai2d71db42016-11-16 22:18:10 +000060_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000061void *
Eric Fiselier162922f2016-10-14 06:46:30 +000062operator new(std::size_t size) _THROW_BAD_ALLOC
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000063{
64 if (size == 0)
65 size = 1;
66 void* p;
67 while ((p = ::malloc(size)) == 0)
68 {
Howard Hinnant34468d42010-08-22 13:53:14 +000069 // If malloc fails and there is a new_handler,
70 // call it to try free up memory.
Howard Hinnant8c65b452010-12-04 19:56:43 +000071 std::new_handler nh = std::get_new_handler();
Howard Hinnantf64dfce2010-12-04 19:54:11 +000072 if (nh)
73 nh();
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000074 else
Howard Hinnant72f73582010-08-11 17:04:31 +000075#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000076 throw std::bad_alloc();
Howard Hinnant72f73582010-08-11 17:04:31 +000077#else
78 break;
79#endif
Nick Kledzikd1a61bb2010-05-14 20:19:37 +000080 }
81 return p;
82}
83
Shoaib Meenai2d71db42016-11-16 22:18:10 +000084_LIBCPP_WEAK
Eric Fiselier850652f2017-01-20 01:13:49 +000085void*
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 if (ptr)
134 ::free(ptr);
135}
136
137_LIBCPP_WEAK
138void
139operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
140{
141 ::operator delete(ptr);
142}
143
144_LIBCPP_WEAK
145void
146operator delete(void* ptr, size_t) _NOEXCEPT
147{
148 ::operator delete(ptr);
149}
150
151_LIBCPP_WEAK
152void
153operator delete[] (void* ptr) _NOEXCEPT
154{
155 ::operator delete(ptr);
156}
157
158_LIBCPP_WEAK
159void
160operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
161{
162 ::operator delete[](ptr);
163}
164
165_LIBCPP_WEAK
166void
167operator delete[] (void* ptr, size_t) _NOEXCEPT
168{
169 ::operator delete[](ptr);
170}
171
Eric Fiselier44832fb2017-01-20 01:47:26 +0000172#if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
173
Eric Fiselier850652f2017-01-20 01:13:49 +0000174_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000175void *
176operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
177{
178 if (size == 0)
179 size = 1;
180 if (static_cast<size_t>(alignment) < sizeof(void*))
181 alignment = std::align_val_t(sizeof(void*));
182 void* p;
Saleem Abdulrasoolee302e42017-01-03 21:53:51 +0000183#if defined(_LIBCPP_MSVCRT)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000184 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
185#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000186 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
Eric Fiselier29aaae12016-12-23 20:17:23 +0000187#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000188 {
189 // If posix_memalign fails and there is a new_handler,
190 // call it to try free up memory.
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 p = nullptr; // posix_memalign doesn't initialize 'p' on failure
199 break;
200#endif
201 }
202 }
203 return p;
204}
205
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000206_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000207void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000208operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
209{
210 void* p = 0;
211#ifndef _LIBCPP_NO_EXCEPTIONS
212 try
213 {
214#endif // _LIBCPP_NO_EXCEPTIONS
215 p = ::operator new(size, alignment);
216#ifndef _LIBCPP_NO_EXCEPTIONS
217 }
218 catch (...)
219 {
220 }
221#endif // _LIBCPP_NO_EXCEPTIONS
222 return p;
223}
224
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000225_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000226void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000227operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
228{
229 return ::operator new(size, alignment);
230}
231
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000232_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000233void*
Eric Fiselier162922f2016-10-14 06:46:30 +0000234operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
235{
236 void* p = 0;
237#ifndef _LIBCPP_NO_EXCEPTIONS
238 try
239 {
240#endif // _LIBCPP_NO_EXCEPTIONS
241 p = ::operator new[](size, alignment);
242#ifndef _LIBCPP_NO_EXCEPTIONS
243 }
244 catch (...)
245 {
246 }
247#endif // _LIBCPP_NO_EXCEPTIONS
248 return p;
249}
250
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000251_LIBCPP_WEAK
Nick Kledzikd1a61bb2010-05-14 20:19:37 +0000252void
Eric Fiselier162922f2016-10-14 06:46:30 +0000253operator delete(void* ptr, std::align_val_t) _NOEXCEPT
254{
255 if (ptr)
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000256#if defined(_LIBCPP_MSVCRT)
257 ::_aligned_free(ptr);
258#else
Eric Fiselier162922f2016-10-14 06:46:30 +0000259 ::free(ptr);
Shoaib Meenai35d3e692017-01-12 06:22:36 +0000260#endif
Eric Fiselier162922f2016-10-14 06:46:30 +0000261}
262
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000263_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000264void
Eric Fiselier162922f2016-10-14 06:46:30 +0000265operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
266{
267 ::operator delete(ptr, alignment);
268}
269
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000270_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000271void
Eric Fiselier162922f2016-10-14 06:46:30 +0000272operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
273{
274 ::operator delete(ptr, alignment);
275}
276
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000277_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000278void
Eric Fiselier162922f2016-10-14 06:46:30 +0000279operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
280{
281 ::operator delete(ptr, alignment);
282}
283
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000284_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000285void
Eric Fiselier162922f2016-10-14 06:46:30 +0000286operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
287{
288 ::operator delete[](ptr, alignment);
289}
290
Shoaib Meenai2d71db42016-11-16 22:18:10 +0000291_LIBCPP_WEAK
Eric Fiselier162922f2016-10-14 06:46:30 +0000292void
Eric Fiselier162922f2016-10-14 06:46:30 +0000293operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
294{
295 ::operator delete[](ptr, alignment);
296}
297
Eric Fiselier4e5fccc2017-02-10 04:25:33 +0000298#endif // !_LIBCPP_HAS_NO_ALIGNED_ALLOCATION
299#endif // !__GLIBCXX__ && !_LIBCPP_ABI_MICROSOFT