blob: 7435bac824473db1894b2995986329d656a869ce [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
12#include <new>
13#include <cstdlib>
14
15/*
16[new.delete.single]
17
18* Executes a loop: Within the loop, the function first attempts to allocate
19 the requested storage. Whether the attempt involves a call to the Standard C
20 library function malloc is unspecified.
21
22* Returns a pointer to the allocated storage if the attempt is successful.
23 Otherwise, if the current new_handler (18.6.2.5) is a null pointer value,
24 throws bad_alloc.
25
26* Otherwise, the function calls the current new_handler function (18.6.2.3).
27 If the called function returns, the loop repeats.
28
29* The loop terminates when an attempt to allocate the requested storage is
30 successful or when a called new_handler function does not return.
31*/
32__attribute__((__weak__, __visibility__("default")))
33void *
34operator new(std::size_t size)
35#if !__has_feature(cxx_noexcept)
36 throw(std::bad_alloc)
37#endif
38{
39 if (size == 0)
40 size = 1;
41 void* p;
42 while ((p = std::malloc(size)) == 0)
43 {
44 std::new_handler nh = std::get_new_handler();
45 if (nh)
46 nh();
47 else
48 throw std::bad_alloc();
49 }
50 return p;
51}
52
53/*
54Note: The relationships among these operators is both carefully considered
55and standard in C++11. Please do not change them without fully understanding
56the consequences of doing so. Reference:
57http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html
58*/
59/*
60[new.delete.single]
61
62Calls operator new(size). If the call returns normally, returns the result of
63that call. Otherwise, returns a null pointer.
64*/
65__attribute__((__weak__, __visibility__("default")))
66void*
67operator new(size_t size, const std::nothrow_t&)
68#if __has_feature(cxx_noexcept)
69 noexcept
70#else
71 throw()
72#endif
73{
74 void* p = 0;
75 try
76 {
77 p = ::operator new(size);
78 }
79 catch (...)
80 {
81 }
82 return p;
83}
84
85/*
86[new.delete.array]
87
88Returns operator new(size).
89*/
90__attribute__((__weak__, __visibility__("default")))
91void*
92operator new[](size_t size)
93#if !__has_feature(cxx_noexcept)
94 throw(std::bad_alloc)
95#endif
96{
97 return ::operator new(size);
98}
99
100/*
101[new.delete.array]
102
103Calls operator new[](size). If the call returns normally, returns the result
104of that call. Otherwise, returns a null pointer.
105*/
106__attribute__((__weak__, __visibility__("default")))
107void*
108operator new[](size_t size, const std::nothrow_t&)
109#if __has_feature(cxx_noexcept)
110 noexcept
111#else
112 throw()
113#endif
114{
115 void* p = 0;
116 try
117 {
118 p = ::operator new[](size);
119 }
120 catch (...)
121 {
122 }
123 return p;
124}
125
126/*
127[new.delete.single]
128
129If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the
130earlier call to operator new.
131*/
132__attribute__((__weak__, __visibility__("default")))
133void
134operator delete(void* ptr)
135#if __has_feature(cxx_noexcept)
136 noexcept
137#else
138 throw()
139#endif
140{
141 if (ptr)
142 std::free(ptr);
143}
144
145/*
146[new.delete.single]
147
148calls operator delete(ptr)
149*/
150__attribute__((__weak__, __visibility__("default")))
151void
152operator delete(void* ptr, const std::nothrow_t&)
153#if __has_feature(cxx_noexcept)
154 noexcept
155#else
156 throw()
157#endif
158{
159 ::operator delete(ptr);
160}
161
162/*
163[new.delete.array]
164
165Calls operator delete(ptr)
166*/
167__attribute__((__weak__, __visibility__("default")))
168void
169operator delete[] (void* ptr)
170#if __has_feature(cxx_noexcept)
171 noexcept
172#else
173 throw()
174#endif
175{
176 ::operator delete(ptr);
177}
178
179/*
180[new.delete.array]
181
182calls operator delete[](ptr)
183*/
184__attribute__((__weak__, __visibility__("default")))
185void
186operator delete[] (void* ptr, const std::nothrow_t&)
187#if __has_feature(cxx_noexcept)
188 noexcept
189#else
190 throw()
191#endif
192{
193 ::operator delete[](ptr);
194}