1 //===------------------------ cxa_new_delete.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 // This file implements the new and delete operators.
10 //===----------------------------------------------------------------------===//
11
12 #define _LIBCPP_BUILDING_NEW
13
14 #include <new>
15 #include <cstdlib>
16
17 /*
18 [new.delete.single]
19
20 * Executes a loop: Within the loop, the function first attempts to allocate
21 the requested storage. Whether the attempt involves a call to the Standard C
22 library function malloc is unspecified.
23
24 * Returns a pointer to the allocated storage if the attempt is successful.
25 Otherwise, if the current new_handler (18.6.2.5) is a null pointer value,
26 throws bad_alloc.
27
28 * Otherwise, the function calls the current new_handler function (18.6.2.3).
29 If the called function returns, the loop repeats.
30
31 * The loop terminates when an attempt to allocate the requested storage is
32 successful or when a called new_handler function does not return.
33 */
34 __attribute__((__weak__, __visibility__("default")))
35 void *
operator new(std::size_t size)36 operator new(std::size_t size)
37 #if !__has_feature(cxx_noexcept)
38 throw(std::bad_alloc)
39 #endif
40 {
41 if (size == 0)
42 size = 1;
43 void* p;
44 while ((p = std::malloc(size)) == 0)
45 {
46 std::new_handler nh = std::get_new_handler();
47 if (nh)
48 nh();
49 else
50 throw std::bad_alloc();
51 }
52 return p;
53 }
54
55 /*
56 Note: The relationships among these operators is both carefully considered
57 and standard in C++11. Please do not change them without fully understanding
58 the consequences of doing so. Reference:
59 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html
60 */
61 /*
62 [new.delete.single]
63
64 Calls operator new(size). If the call returns normally, returns the result of
65 that call. Otherwise, returns a null pointer.
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&)
70 #if __has_feature(cxx_noexcept)
71 noexcept
72 #else
73 throw()
74 #endif
75 {
76 void* p = 0;
77 try
78 {
79 p = ::operator new(size);
80 }
81 catch (...)
82 {
83 }
84 return p;
85 }
86
87 /*
88 [new.delete.array]
89
90 Returns operator new(size).
91 */
92 __attribute__((__weak__, __visibility__("default")))
93 void*
operator new[](size_t size)94 operator new[](size_t size)
95 #if !__has_feature(cxx_noexcept)
96 throw(std::bad_alloc)
97 #endif
98 {
99 return ::operator new(size);
100 }
101
102 /*
103 [new.delete.array]
104
105 Calls operator new[](size). If the call returns normally, returns the result
106 of that call. Otherwise, returns a null pointer.
107 */
108 __attribute__((__weak__, __visibility__("default")))
109 void*
operator new[](size_t size,const std::nothrow_t &)110 operator new[](size_t size, const std::nothrow_t&)
111 #if __has_feature(cxx_noexcept)
112 noexcept
113 #else
114 throw()
115 #endif
116 {
117 void* p = 0;
118 try
119 {
120 p = ::operator new[](size);
121 }
122 catch (...)
123 {
124 }
125 return p;
126 }
127
128 /*
129 [new.delete.single]
130
131 If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the
132 earlier call to operator new.
133 */
134 __attribute__((__weak__, __visibility__("default")))
135 void
operator delete(void * ptr)136 operator delete(void* ptr)
137 #if __has_feature(cxx_noexcept)
138 noexcept
139 #else
140 throw()
141 #endif
142 {
143 if (ptr)
144 std::free(ptr);
145 }
146
147 /*
148 [new.delete.single]
149
150 calls operator delete(ptr)
151 */
152 __attribute__((__weak__, __visibility__("default")))
153 void
operator delete(void * ptr,const std::nothrow_t &)154 operator delete(void* ptr, const std::nothrow_t&)
155 #if __has_feature(cxx_noexcept)
156 noexcept
157 #else
158 throw()
159 #endif
160 {
161 ::operator delete(ptr);
162 }
163
164 /*
165 [new.delete.array]
166
167 Calls operator delete(ptr)
168 */
169 __attribute__((__weak__, __visibility__("default")))
170 void
operator delete[](void * ptr)171 operator delete[] (void* ptr)
172 #if __has_feature(cxx_noexcept)
173 noexcept
174 #else
175 throw()
176 #endif
177 {
178 ::operator delete(ptr);
179 }
180
181 /*
182 [new.delete.array]
183
184 calls operator delete[](ptr)
185 */
186 __attribute__((__weak__, __visibility__("default")))
187 void
operator delete[](void * ptr,const std::nothrow_t &)188 operator delete[] (void* ptr, const std::nothrow_t&)
189 #if __has_feature(cxx_noexcept)
190 noexcept
191 #else
192 throw()
193 #endif
194 {
195 ::operator delete[](ptr);
196 }
197
198 namespace std
199 {
200
201 // bad_alloc
202
bad_alloc()203 bad_alloc::bad_alloc() _NOEXCEPT
204 {
205 }
206
~bad_alloc()207 bad_alloc::~bad_alloc() _NOEXCEPT
208 {
209 }
210
211 const char*
what() const212 bad_alloc::what() const _NOEXCEPT
213 {
214 return "std::bad_alloc";
215 }
216
217 // bad_array_new_length
218
bad_array_new_length()219 bad_array_new_length::bad_array_new_length() _NOEXCEPT
220 {
221 }
222
~bad_array_new_length()223 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
224 {
225 }
226
227 const char*
what() const228 bad_array_new_length::what() const _NOEXCEPT
229 {
230 return "bad_array_new_length";
231 }
232
233 // bad_array_length
234
235 #ifndef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
236
237 class _LIBCPP_EXCEPTION_ABI bad_array_length
238 : public bad_alloc
239 {
240 public:
241 bad_array_length() _NOEXCEPT;
242 virtual ~bad_array_length() _NOEXCEPT;
243 virtual const char* what() const _NOEXCEPT;
244 };
245
246 #endif // _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
247
bad_array_length()248 bad_array_length::bad_array_length() _NOEXCEPT
249 {
250 }
251
~bad_array_length()252 bad_array_length::~bad_array_length() _NOEXCEPT
253 {
254 }
255
256 const char*
what() const257 bad_array_length::what() const _NOEXCEPT
258 {
259 return "bad_array_length";
260 }
261
262 } // std
263