blob: c28fcb5917cf30288f06888a8048380df5c138f4 [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
Howard Hinnanted569212011-05-26 18:23:59 +0000129operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000130{
131 ::operator delete(ptr);
132}
133
Howard Hinnant35a98a02013-10-06 20:53:24 +0000134_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000135void
Larisse Voufo74f95a02015-02-20 06:13:05 +0000136operator delete(void* ptr, size_t) _NOEXCEPT
Larisse Voufo19efe012015-02-15 05:18:55 +0000137{
138 ::operator delete(ptr);
139}
140
141_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
142void
Howard Hinnanted569212011-05-26 18:23:59 +0000143operator delete[] (void* ptr) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000144{
Larisse Voufo19efe012015-02-15 05:18:55 +0000145 ::operator delete(ptr);
146}
147
148_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
149void
Howard Hinnanted569212011-05-26 18:23:59 +0000150operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000151{
152 ::operator delete[](ptr);
153}
154
Larisse Voufo19efe012015-02-15 05:18:55 +0000155_LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
156void
Larisse Voufo74f95a02015-02-20 06:13:05 +0000157operator delete[] (void* ptr, size_t) _NOEXCEPT
Larisse Voufo19efe012015-02-15 05:18:55 +0000158{
159 ::operator delete[](ptr);
160}
161
Peter Collingbourne40455c62013-10-06 22:13:16 +0000162#endif // !__GLIBCXX__
163
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164namespace std
165{
166
Peter Collingbourne40455c62013-10-06 22:13:16 +0000167#ifndef __GLIBCXX__
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000168const nothrow_t nothrow = {};
Peter Collingbourne40455c62013-10-06 22:13:16 +0000169#endif
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000170
Howard Hinnantdea7f392012-02-02 20:48:35 +0000171#ifndef _LIBCPPABI_VERSION
172
Peter Collingbourne40455c62013-10-06 22:13:16 +0000173#ifndef __GLIBCXX__
174
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000175new_handler
Howard Hinnanted569212011-05-26 18:23:59 +0000176set_new_handler(new_handler handler) _NOEXCEPT
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000177{
Howard Hinnanta4451512010-12-02 16:45:21 +0000178 return __sync_lock_test_and_set(&__new_handler, handler);
179}
180
181new_handler
Howard Hinnanted569212011-05-26 18:23:59 +0000182get_new_handler() _NOEXCEPT
Howard Hinnanta4451512010-12-02 16:45:21 +0000183{
Joerg Sonnenberger4c6acb52014-01-04 17:43:00 +0000184 return __sync_fetch_and_add(&__new_handler, nullptr);
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000185}
186
Peter Collingbourne40455c62013-10-06 22:13:16 +0000187#endif // !__GLIBCXX__
188
David Chisnall1e8b3f92012-02-29 12:59:17 +0000189#ifndef LIBCXXRT
190
Howard Hinnanted569212011-05-26 18:23:59 +0000191bad_alloc::bad_alloc() _NOEXCEPT
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000192{
Nick Kledzik804b6e72010-05-14 20:19:37 +0000193}
194
Peter Collingbourne40455c62013-10-06 22:13:16 +0000195#ifndef __GLIBCXX__
196
Howard Hinnanted569212011-05-26 18:23:59 +0000197bad_alloc::~bad_alloc() _NOEXCEPT
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000198{
Nick Kledzik804b6e72010-05-14 20:19:37 +0000199}
200
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000201const char*
Howard Hinnanted569212011-05-26 18:23:59 +0000202bad_alloc::what() const _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000203{
204 return "std::bad_alloc";
205}
206
Peter Collingbourne40455c62013-10-06 22:13:16 +0000207#endif // !__GLIBCXX__
208
Howard Hinnanted569212011-05-26 18:23:59 +0000209bad_array_new_length::bad_array_new_length() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000210{
211}
212
Howard Hinnanted569212011-05-26 18:23:59 +0000213bad_array_new_length::~bad_array_new_length() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214{
215}
216
Eric Fiselierf53135f2014-11-01 00:11:25 +0000217const char*
218bad_array_new_length::what() const _NOEXCEPT
219{
220 return "bad_array_new_length";
221}
222
Eric Fiselier3fdac972014-10-29 23:14:53 +0000223#endif //LIBCXXRT
224
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225const char*
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000226bad_array_length::what() const _NOEXCEPT
227{
228 return "bad_array_length";
229}
230
231bad_array_length::bad_array_length() _NOEXCEPT
232{
233}
234
235bad_array_length::~bad_array_length() _NOEXCEPT
236{
237}
238
Marshall Clow7f9f52e2013-09-11 01:38:42 +0000239#endif // _LIBCPPABI_VERSION
Howard Hinnantdea7f392012-02-02 20:48:35 +0000240
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000241#ifndef LIBSTDCXX
242
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243void
244__throw_bad_alloc()
245{
Howard Hinnantd4444702010-08-11 17:04:31 +0000246#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247 throw bad_alloc();
Howard Hinnantd4444702010-08-11 17:04:31 +0000248#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249}
250
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000251#endif // !LIBSTDCXX
252
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253} // std