blob: 25a5454e05eeaae93d2594de9801cc2962982da1 [file] [log] [blame]
Howard Hinnant4a6e1032011-12-20 20:38:05 +00001//===------------------------ 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
Marshall Clow6c3f6752013-09-11 01:42:02 +000012#define _LIBCPP_BUILDING_NEW
13
Howard Hinnant4a6e1032011-12-20 20:38:05 +000014#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")))
35void *
36operator 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/*
56Note: The relationships among these operators is both carefully considered
57and standard in C++11. Please do not change them without fully understanding
58the consequences of doing so. Reference:
59http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html
60*/
61/*
62[new.delete.single]
63
64Calls operator new(size). If the call returns normally, returns the result of
65that call. Otherwise, returns a null pointer.
66*/
67__attribute__((__weak__, __visibility__("default")))
68void*
69operator 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
90Returns operator new(size).
91*/
92__attribute__((__weak__, __visibility__("default")))
93void*
94operator 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
105Calls operator new[](size). If the call returns normally, returns the result
106of that call. Otherwise, returns a null pointer.
107*/
108__attribute__((__weak__, __visibility__("default")))
109void*
110operator 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
131If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the
132earlier call to operator new.
133*/
134__attribute__((__weak__, __visibility__("default")))
135void
136operator 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
150calls operator delete(ptr)
151*/
152__attribute__((__weak__, __visibility__("default")))
153void
154operator 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
167Calls operator delete(ptr)
168*/
169__attribute__((__weak__, __visibility__("default")))
170void
171operator 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
184calls operator delete[](ptr)
185*/
186__attribute__((__weak__, __visibility__("default")))
187void
188operator 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}
Howard Hinnant35b2c2a2012-01-24 21:35:18 +0000197
198namespace std
199{
200
201// bad_alloc
202
203bad_alloc::bad_alloc() _NOEXCEPT
204{
205}
206
207bad_alloc::~bad_alloc() _NOEXCEPT
208{
209}
210
211const char*
212bad_alloc::what() const _NOEXCEPT
213{
214 return "std::bad_alloc";
215}
216
217// bad_array_new_length
218
219bad_array_new_length::bad_array_new_length() _NOEXCEPT
220{
221}
222
223bad_array_new_length::~bad_array_new_length() _NOEXCEPT
224{
225}
226
227const char*
228bad_array_new_length::what() const _NOEXCEPT
229{
230 return "bad_array_new_length";
231}
232
Marshall Clow6c3f6752013-09-11 01:42:02 +0000233// bad_array_length
234
Howard Hinnant38a0eeb2013-11-07 17:16:37 +0000235#ifndef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
236
237class _LIBCPP_EXCEPTION_ABI bad_array_length
238 : public bad_alloc
239{
240public:
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
Marshall Clow6c3f6752013-09-11 01:42:02 +0000248bad_array_length::bad_array_length() _NOEXCEPT
249{
250}
251
252bad_array_length::~bad_array_length() _NOEXCEPT
253{
254}
255
256const char*
257bad_array_length::what() const _NOEXCEPT
258{
259 return "bad_array_length";
260}
261
Howard Hinnant35b2c2a2012-01-24 21:35:18 +0000262} // std