Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===--------------------------- new.cpp ----------------------------------===// |
2 | // | ||||
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
6 | // Source Licenses. See LICENSE.TXT for details. | ||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
8 | //===----------------------------------------------------------------------===// | ||||
9 | |||||
Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 10 | #define _LIBCPP_BUILDING_NEW |
11 | |||||
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 12 | #include <stdlib.h> |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 13 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | #include "new" |
15 | |||||
Howard Hinnant | 8b5bb3c | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 16 | #ifndef __has_include |
17 | #define __has_include(inc) 0 | ||||
18 | #endif | ||||
19 | |||||
Marshall Clow | dece7fe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 20 | #ifdef __APPLE__ |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 21 | #include <cxxabi.h> |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 22 | |
23 | #ifndef _LIBCPPABI_VERSION | ||||
24 | // On Darwin, there are two STL shared libraries and a lower level ABI | ||||
Marshall Clow | a46a0ad | 2013-11-11 23:27:19 +0000 | [diff] [blame] | 25 | // shared library. The global holding the current new handler is |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 26 | // in the ABI library and named __cxa_new_handler. |
27 | #define __new_handler __cxxabiapple::__cxa_new_handler | ||||
28 | #endif | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 29 | #else // __APPLE__ |
Howard Hinnant | 8b5bb3c | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 30 | #if defined(LIBCXXRT) || __has_include(<cxxabi.h>) |
31 | #include <cxxabi.h> | ||||
32 | #endif // __has_include(<cxxabi.h>) | ||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 33 | #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) |
Howard Hinnant | 8b5bb3c | 2012-07-26 17:42:39 +0000 | [diff] [blame] | 34 | static std::new_handler __new_handler; |
35 | #endif // _LIBCPPABI_VERSION | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 36 | #endif |
37 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 38 | #ifndef __GLIBCXX__ |
39 | |||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 40 | // 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 Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 44 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 45 | void * |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 46 | operator new(std::size_t size) |
Howard Hinnant | 912012e | 2011-07-14 01:34:46 +0000 | [diff] [blame] | 47 | #if !__has_feature(cxx_noexcept) |
48 | throw(std::bad_alloc) | ||||
49 | #endif | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 50 | { |
51 | if (size == 0) | ||||
52 | size = 1; | ||||
53 | void* p; | ||||
54 | while ((p = ::malloc(size)) == 0) | ||||
55 | { | ||||
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 56 | // If malloc fails and there is a new_handler, |
57 | // call it to try free up memory. | ||||
Howard Hinnant | d42c4be | 2010-12-04 19:56:43 +0000 | [diff] [blame] | 58 | std::new_handler nh = std::get_new_handler(); |
Howard Hinnant | 1f970ad | 2010-12-04 19:54:11 +0000 | [diff] [blame] | 59 | if (nh) |
60 | nh(); | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 61 | else |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 62 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 63 | throw std::bad_alloc(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 64 | #else |
65 | break; | ||||
66 | #endif | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 67 | } |
68 | return p; | ||||
69 | } | ||||
70 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 71 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 72 | void* |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 73 | operator new(size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 74 | { |
75 | void* p = 0; | ||||
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 76 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 77 | try |
78 | { | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 79 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 80 | p = ::operator new(size); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 81 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 82 | } |
83 | catch (...) | ||||
84 | { | ||||
85 | } | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 86 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 87 | return p; |
88 | } | ||||
89 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 90 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 91 | void* |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 92 | operator new[](size_t size) |
Howard Hinnant | 912012e | 2011-07-14 01:34:46 +0000 | [diff] [blame] | 93 | #if !__has_feature(cxx_noexcept) |
94 | throw(std::bad_alloc) | ||||
95 | #endif | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 96 | { |
97 | return ::operator new(size); | ||||
98 | } | ||||
99 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 100 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 101 | void* |
Howard Hinnant | ec3773c | 2011-12-01 20:21:04 +0000 | [diff] [blame] | 102 | operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 103 | { |
104 | void* p = 0; | ||||
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 105 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 106 | try |
107 | { | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 108 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 109 | p = ::operator new[](size); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 110 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 111 | } |
112 | catch (...) | ||||
113 | { | ||||
114 | } | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 115 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 116 | return p; |
117 | } | ||||
118 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 119 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 120 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 121 | operator delete(void* ptr) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 122 | { |
123 | if (ptr) | ||||
124 | ::free(ptr); | ||||
125 | } | ||||
126 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 127 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 128 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 129 | operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 130 | { |
131 | ::operator delete(ptr); | ||||
132 | } | ||||
133 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 134 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 135 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 136 | operator delete[] (void* ptr) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 137 | { |
138 | ::operator delete (ptr); | ||||
139 | } | ||||
140 | |||||
Howard Hinnant | 35a98a0 | 2013-10-06 20:53:24 +0000 | [diff] [blame] | 141 | _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 142 | void |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 143 | operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 144 | { |
145 | ::operator delete[](ptr); | ||||
146 | } | ||||
147 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 148 | #endif // !__GLIBCXX__ |
149 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 150 | namespace std |
151 | { | ||||
152 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 153 | #ifndef __GLIBCXX__ |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 154 | const nothrow_t nothrow = {}; |
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 155 | #endif |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 156 | |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 157 | #ifndef _LIBCPPABI_VERSION |
158 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 159 | #ifndef __GLIBCXX__ |
160 | |||||
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 161 | new_handler |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 162 | set_new_handler(new_handler handler) _NOEXCEPT |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 163 | { |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 164 | return __sync_lock_test_and_set(&__new_handler, handler); |
165 | } | ||||
166 | |||||
167 | new_handler | ||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 168 | get_new_handler() _NOEXCEPT |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 169 | { |
Joerg Sonnenberger | 4c6acb5 | 2014-01-04 17:43:00 +0000 | [diff] [blame] | 170 | return __sync_fetch_and_add(&__new_handler, nullptr); |
Nick Kledzik | 76fdaa7 | 2010-05-18 22:17:13 +0000 | [diff] [blame] | 171 | } |
172 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 173 | #endif // !__GLIBCXX__ |
174 | |||||
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 175 | #ifndef LIBCXXRT |
176 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 177 | bad_alloc::bad_alloc() _NOEXCEPT |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 178 | { |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 179 | } |
180 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 181 | #ifndef __GLIBCXX__ |
182 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 183 | bad_alloc::~bad_alloc() _NOEXCEPT |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 184 | { |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 185 | } |
186 | |||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 187 | const char* |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 188 | bad_alloc::what() const _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 189 | { |
190 | return "std::bad_alloc"; | ||||
191 | } | ||||
192 | |||||
Peter Collingbourne | 40455c6 | 2013-10-06 22:13:16 +0000 | [diff] [blame] | 193 | #endif // !__GLIBCXX__ |
194 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 195 | bad_array_new_length::bad_array_new_length() _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 196 | { |
197 | } | ||||
198 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 199 | bad_array_new_length::~bad_array_new_length() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | { |
201 | } | ||||
202 | |||||
Eric Fiselier | f53135f | 2014-11-01 00:11:25 +0000 | [diff] [blame^] | 203 | const char* |
204 | bad_array_new_length::what() const _NOEXCEPT | ||||
205 | { | ||||
206 | return "bad_array_new_length"; | ||||
207 | } | ||||
208 | |||||
Eric Fiselier | 3fdac97 | 2014-10-29 23:14:53 +0000 | [diff] [blame] | 209 | #endif //LIBCXXRT |
210 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 211 | const char* |
Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 212 | bad_array_length::what() const _NOEXCEPT |
213 | { | ||||
214 | return "bad_array_length"; | ||||
215 | } | ||||
216 | |||||
217 | bad_array_length::bad_array_length() _NOEXCEPT | ||||
218 | { | ||||
219 | } | ||||
220 | |||||
221 | bad_array_length::~bad_array_length() _NOEXCEPT | ||||
222 | { | ||||
223 | } | ||||
224 | |||||
Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 225 | #endif // _LIBCPPABI_VERSION |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 226 | |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 227 | #ifndef LIBSTDCXX |
228 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | void |
230 | __throw_bad_alloc() | ||||
231 | { | ||||
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 232 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | throw bad_alloc(); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 234 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | } |
236 | |||||
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 237 | #endif // !LIBSTDCXX |
238 | |||||
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 239 | } // std |