1 //===--------------------------- new.cpp ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <stdlib.h>
11
12 #include "new"
13
14 #ifndef __has_include
15 #define __has_include(inc) 0
16 #endif
17
18 #ifdef __APPLE__
19 #include <cxxabi.h>
20
21 #ifndef _LIBCPPABI_VERSION
22 // On Darwin, there are two STL shared libraries and a lower level ABI
23 // shared libray. The global holding the current new handler is
24 // in the ABI library and named __cxa_new_handler.
25 #define __new_handler __cxxabiapple::__cxa_new_handler
26 #endif
27 #else // __APPLE__
28 #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
29 #include <cxxabi.h>
30 #endif // __has_include(<cxxabi.h>)
31 #ifndef _LIBCPPABI_VERSION
32 static std::new_handler __new_handler;
33 #endif // _LIBCPPABI_VERSION
34 #endif
35
36 // Implement all new and delete operators as weak definitions
37 // in this shared library, so that they can be overriden by programs
38 // that define non-weak copies of the functions.
39
40 __attribute__((__weak__, __visibility__("default")))
41 void *
operator new(std::size_t size)42 operator new(std::size_t size)
43 #if !__has_feature(cxx_noexcept)
44 throw(std::bad_alloc)
45 #endif
46 {
47 if (size == 0)
48 size = 1;
49 void* p;
50 while ((p = ::malloc(size)) == 0)
51 {
52 // If malloc fails and there is a new_handler,
53 // call it to try free up memory.
54 std::new_handler nh = std::get_new_handler();
55 if (nh)
56 nh();
57 else
58 #ifndef _LIBCPP_NO_EXCEPTIONS
59 throw std::bad_alloc();
60 #else
61 break;
62 #endif
63 }
64 return p;
65 }
66
67 __attribute__((__weak__, __visibility__("default")))
68 void*
operator new(size_t size,const std::nothrow_t &)69 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
70 {
71 void* p = 0;
72 #ifndef _LIBCPP_NO_EXCEPTIONS
73 try
74 {
75 #endif // _LIBCPP_NO_EXCEPTIONS
76 p = ::operator new(size);
77 #ifndef _LIBCPP_NO_EXCEPTIONS
78 }
79 catch (...)
80 {
81 }
82 #endif // _LIBCPP_NO_EXCEPTIONS
83 return p;
84 }
85
86 __attribute__((__weak__, __visibility__("default")))
87 void*
operator new[](size_t size)88 operator new[](size_t size)
89 #if !__has_feature(cxx_noexcept)
90 throw(std::bad_alloc)
91 #endif
92 {
93 return ::operator new(size);
94 }
95
96 __attribute__((__weak__, __visibility__("default")))
97 void*
operator new[](size_t size,const std::nothrow_t &)98 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
99 {
100 void* p = 0;
101 #ifndef _LIBCPP_NO_EXCEPTIONS
102 try
103 {
104 #endif // _LIBCPP_NO_EXCEPTIONS
105 p = ::operator new[](size);
106 #ifndef _LIBCPP_NO_EXCEPTIONS
107 }
108 catch (...)
109 {
110 }
111 #endif // _LIBCPP_NO_EXCEPTIONS
112 return p;
113 }
114
115 __attribute__((__weak__, __visibility__("default")))
116 void
operator delete(void * ptr)117 operator delete(void* ptr) _NOEXCEPT
118 {
119 if (ptr)
120 ::free(ptr);
121 }
122
123 __attribute__((__weak__, __visibility__("default")))
124 void
operator delete(void * ptr,const std::nothrow_t &)125 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
126 {
127 ::operator delete(ptr);
128 }
129
130 __attribute__((__weak__, __visibility__("default")))
131 void
operator delete[](void * ptr)132 operator delete[] (void* ptr) _NOEXCEPT
133 {
134 ::operator delete (ptr);
135 }
136
137 __attribute__((__weak__, __visibility__("default")))
138 void
operator delete[](void * ptr,const std::nothrow_t &)139 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
140 {
141 ::operator delete[](ptr);
142 }
143
144 namespace std
145 {
146
147 #ifndef GABIXX_LIBCXX
148 const nothrow_t nothrow = {};
149 #endif
150
151 #if !defined(_LIBCPPABI_VERSION)
152
153 #if !defined(GABIXX_LIBCXX)
154 new_handler
set_new_handler(new_handler handler)155 set_new_handler(new_handler handler) _NOEXCEPT
156 {
157 return __sync_lock_test_and_set(&__new_handler, handler);
158 }
159 #endif
160
161 new_handler
get_new_handler()162 get_new_handler() _NOEXCEPT
163 {
164 return __sync_fetch_and_add(&__new_handler, (new_handler)0);
165 }
166
167 #if !defined(LIBCXXRT)
168
bad_alloc()169 bad_alloc::bad_alloc() _NOEXCEPT
170 {
171 }
172
~bad_alloc()173 bad_alloc::~bad_alloc() _NOEXCEPT
174 {
175 }
176
177 const char*
what() const178 bad_alloc::what() const _NOEXCEPT
179 {
180 return "std::bad_alloc";
181 }
182
183 #endif // !LIBCXXRT
184
bad_array_new_length()185 bad_array_new_length::bad_array_new_length() _NOEXCEPT
186 {
187 }
188
~bad_array_new_length()189 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
190 {
191 }
192
193 const char*
what() const194 bad_array_new_length::what() const _NOEXCEPT
195 {
196 return "bad_array_new_length";
197 }
198
199 #endif // !_LIBCPPABI_VERSION && !GABIXX_LIBCXX
200
201 void
__throw_bad_alloc()202 __throw_bad_alloc()
203 {
204 #ifndef _LIBCPP_NO_EXCEPTIONS
205 throw bad_alloc();
206 #endif
207 }
208
209 } // std
210