blob: a9b3733df08cf11d6dc88f60852c9dde6874c02d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===--------------------------- new.cpp ----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Marshall Clow7f9f52e2013-09-11 01:38:42 +000010#define _LIBCPP_BUILDING_NEW
11
Howard Hinnantadff4892010-05-24 17:49:41 +000012#include <stdlib.h>
Nick Kledzik804b6e72010-05-14 20:19:37 +000013
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014#include "new"
15
Howard Hinnant8b5bb3c2012-07-26 17:42:39 +000016#ifndef __has_include
17#define __has_include(inc) 0
18#endif
19
Eric Fiselier3be7f192014-11-01 00:41:42 +000020#if defined(__APPLE__) && !defined(LIBCXXRT)
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000021 #include <cxxabi.h>
Howard Hinnantdea7f392012-02-02 20:48:35 +000022
23 #ifndef _LIBCPPABI_VERSION
24 // On Darwin, there are two STL shared libraries and a lower level ABI
Marshall Clowa46a0ad2013-11-11 23:27:19 +000025 // shared library. The global holding the current new handler is
Howard Hinnantdea7f392012-02-02 20:48:35 +000026 // in the ABI library and named __cxa_new_handler.
27 #define __new_handler __cxxabiapple::__cxa_new_handler
28 #endif
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000029#else // __APPLE__
Howard Hinnant8b5bb3c2012-07-26 17:42:39 +000030 #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
31 #include <cxxabi.h>
32 #endif // __has_include(<cxxabi.h>)
Peter Collingbourne40455c62013-10-06 22:13:16 +000033 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
Howard Hinnant8b5bb3c2012-07-26 17:42:39 +000034 static std::new_handler __new_handler;
35 #endif // _LIBCPPABI_VERSION
Nick Kledzik804b6e72010-05-14 20:19:37 +000036#endif
37
Peter Collingbourne40455c62013-10-06 22:13:16 +000038#ifndef __GLIBCXX__
39
Nick Kledzik804b6e72010-05-14 20:19:37 +000040// Implement all new and delete operators as weak definitions
41// in this shared library, so that they can be overriden by programs
42// that define non-weak copies of the functions.
43
Howard Hinnant35a98a02013-10-06 20:53:24 +000044_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +000045void *
Howard Hinnanted569212011-05-26 18:23:59 +000046operator new(std::size_t size)
Howard Hinnant912012e2011-07-14 01:34:46 +000047#if !__has_feature(cxx_noexcept)
48 throw(std::bad_alloc)
49#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +000050{
51 if (size == 0)
52 size = 1;
53 void* p;
54 while ((p = ::malloc(size)) == 0)
55 {
Howard Hinnantd5109772010-08-22 13:53:14 +000056 // If malloc fails and there is a new_handler,
57 // call it to try free up memory.
Howard Hinnantd42c4be2010-12-04 19:56:43 +000058 std::new_handler nh = std::get_new_handler();
Howard Hinnant1f970ad2010-12-04 19:54:11 +000059 if (nh)
60 nh();
Nick Kledzik804b6e72010-05-14 20:19:37 +000061 else
Howard Hinnantd4444702010-08-11 17:04:31 +000062#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +000063 throw std::bad_alloc();
Howard Hinnantd4444702010-08-11 17:04:31 +000064#else
65 break;
66#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +000067 }
68 return p;
69}
70
Howard Hinnant35a98a02013-10-06 20:53:24 +000071_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +000072void*
Howard Hinnanted569212011-05-26 18:23:59 +000073operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000074{
75 void* p = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +000076#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +000077 try
78 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000079#endif // _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +000080 p = ::operator new(size);
Howard Hinnantd4444702010-08-11 17:04:31 +000081#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +000082 }
83 catch (...)
84 {
85 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000086#endif // _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +000087 return p;
88}
89
Howard Hinnant35a98a02013-10-06 20:53:24 +000090_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +000091void*
Howard Hinnanted569212011-05-26 18:23:59 +000092operator new[](size_t size)
Howard Hinnant912012e2011-07-14 01:34:46 +000093#if !__has_feature(cxx_noexcept)
94 throw(std::bad_alloc)
95#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +000096{
97 return ::operator new(size);
98}
99
Howard Hinnant35a98a02013-10-06 20:53:24 +0000100_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000101void*
Howard Hinnantec3773c2011-12-01 20:21:04 +0000102operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000103{
104 void* p = 0;
Howard Hinnantd4444702010-08-11 17:04:31 +0000105#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000106 try
107 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000108#endif // _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000109 p = ::operator new[](size);
Howard Hinnantd4444702010-08-11 17:04:31 +0000110#ifndef _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000111 }
112 catch (...)
113 {
114 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000115#endif // _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000116 return p;
117}
118
Howard Hinnant35a98a02013-10-06 20:53:24 +0000119_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000120void
Howard Hinnanted569212011-05-26 18:23:59 +0000121operator delete(void* ptr) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000122{
123 if (ptr)
124 ::free(ptr);
125}
126
Howard Hinnant35a98a02013-10-06 20:53:24 +0000127_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000128void
Larisse Voufo19efe012015-02-15 05:18:55 +0000129operator delete(void* ptr, size_t) _NOEXCEPT
130{
131 ::operator delete(ptr);
132}
133
134_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
135void
Howard Hinnanted569212011-05-26 18:23:59 +0000136operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000137{
138 ::operator delete(ptr);
139}
140
Howard Hinnant35a98a02013-10-06 20:53:24 +0000141_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000142void
Larisse Voufo19efe012015-02-15 05:18:55 +0000143operator delete(void* ptr, size_t, const std::nothrow_t&) _NOEXCEPT
144{
145 ::operator delete(ptr);
146}
147
148_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
149void
Howard Hinnanted569212011-05-26 18:23:59 +0000150operator delete[] (void* ptr) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000151{
Larisse Voufo19efe012015-02-15 05:18:55 +0000152 ::operator delete(ptr);
153}
154
155_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
156void
157operator delete[] (void* ptr, size_t) _NOEXCEPT
158{
159 ::operator delete[](ptr);
Nick Kledzik804b6e72010-05-14 20:19:37 +0000160}
161
Howard Hinnant35a98a02013-10-06 20:53:24 +0000162_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000163void
Howard Hinnanted569212011-05-26 18:23:59 +0000164operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000165{
166 ::operator delete[](ptr);
167}
168
Larisse Voufo19efe012015-02-15 05:18:55 +0000169_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
170void
171operator delete[] (void* ptr, size_t, const std::nothrow_t&) _NOEXCEPT
172{
173 ::operator delete[](ptr);
174}
175
Peter Collingbourne40455c62013-10-06 22:13:16 +0000176#endif // !__GLIBCXX__
177
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000178namespace std
179{
180
Peter Collingbourne40455c62013-10-06 22:13:16 +0000181#ifndef __GLIBCXX__
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000182const nothrow_t nothrow = {};
Peter Collingbourne40455c62013-10-06 22:13:16 +0000183#endif
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000184
Howard Hinnantdea7f392012-02-02 20:48:35 +0000185#ifndef _LIBCPPABI_VERSION
186
Peter Collingbourne40455c62013-10-06 22:13:16 +0000187#ifndef __GLIBCXX__
188
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000189new_handler
Howard Hinnanted569212011-05-26 18:23:59 +0000190set_new_handler(new_handler handler) _NOEXCEPT
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000191{
Howard Hinnanta4451512010-12-02 16:45:21 +0000192 return __sync_lock_test_and_set(&__new_handler, handler);
193}
194
195new_handler
Howard Hinnanted569212011-05-26 18:23:59 +0000196get_new_handler() _NOEXCEPT
Howard Hinnanta4451512010-12-02 16:45:21 +0000197{
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000198 return __sync_fetch_and_add(&__new_handler, nullptr);
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000199}
200
Peter Collingbourne40455c62013-10-06 22:13:16 +0000201#endif // !__GLIBCXX__
202
David Chisnall1e8b3f92012-02-29 12:59:17 +0000203#ifndef LIBCXXRT
204
Howard Hinnanted569212011-05-26 18:23:59 +0000205bad_alloc::bad_alloc() _NOEXCEPT
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000206{
Nick Kledzik804b6e72010-05-14 20:19:37 +0000207}
208
Peter Collingbourne40455c62013-10-06 22:13:16 +0000209#ifndef __GLIBCXX__
210
Howard Hinnanted569212011-05-26 18:23:59 +0000211bad_alloc::~bad_alloc() _NOEXCEPT
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000212{
Nick Kledzik804b6e72010-05-14 20:19:37 +0000213}
214
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000215const char*
Howard Hinnanted569212011-05-26 18:23:59 +0000216bad_alloc::what() const _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000217{
218 return "std::bad_alloc";
219}
220
Peter Collingbourne40455c62013-10-06 22:13:16 +0000221#endif // !__GLIBCXX__
222
Howard Hinnanted569212011-05-26 18:23:59 +0000223bad_array_new_length::bad_array_new_length() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000224{
225}
226
Howard Hinnanted569212011-05-26 18:23:59 +0000227bad_array_new_length::~bad_array_new_length() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228{
229}
230
Eric Fiselierf53135f2014-11-01 00:11:25 +0000231const char*
232bad_array_new_length::what() const _NOEXCEPT
233{
234 return "bad_array_new_length";
235}
236
Eric Fiselier3fdac972014-10-29 23:14:53 +0000237#endif //LIBCXXRT
238
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239const char*
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000240bad_array_length::what() const _NOEXCEPT
241{
242 return "bad_array_length";
243}
244
245bad_array_length::bad_array_length() _NOEXCEPT
246{
247}
248
249bad_array_length::~bad_array_length() _NOEXCEPT
250{
251}
252
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000253#endif // _LIBCPPABI_VERSION
Howard Hinnantdea7f392012-02-02 20:48:35 +0000254
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000255#ifndef LIBSTDCXX
256
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257void
258__throw_bad_alloc()
259{
Howard Hinnantd4444702010-08-11 17:04:31 +0000260#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261 throw bad_alloc();
Howard Hinnantd4444702010-08-11 17:04:31 +0000262#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263}
264
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000265#endif // !LIBSTDCXX
266
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267} // std