• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--------------------- stdlib_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_LIBRARY
13 #include "__cxxabi_config.h"
14 #include <new>
15 #include <cstdlib>
16 
17 #if !defined(_THROW_BAD_ALLOC) || !defined(_NOEXCEPT) || !defined(_LIBCXXABI_WEAK)
18 #error The _THROW_BAD_ALLOC, _NOEXCEPT, and _LIBCXXABI_WEAK libc++ macros must \
19        already be defined by libc++.
20 #endif
21 // Implement all new and delete operators as weak definitions
22 // in this shared library, so that they can be overridden by programs
23 // that define non-weak copies of the functions.
24 
25 _LIBCXXABI_WEAK
26 void *
operator new(std::size_t size)27 operator new(std::size_t size) _THROW_BAD_ALLOC
28 {
29     if (size == 0)
30         size = 1;
31     void* p;
32     while ((p = ::malloc(size)) == 0)
33     {
34         // If malloc fails and there is a new_handler,
35         // call it to try free up memory.
36         std::new_handler nh = std::get_new_handler();
37         if (nh)
38             nh();
39         else
40 #ifndef _LIBCXXABI_NO_EXCEPTIONS
41             throw std::bad_alloc();
42 #else
43             break;
44 #endif
45     }
46     return p;
47 }
48 
49 _LIBCXXABI_WEAK
50 void*
operator new(size_t size,const std::nothrow_t &)51 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
52 {
53     void* p = 0;
54 #ifndef _LIBCXXABI_NO_EXCEPTIONS
55     try
56     {
57 #endif  // _LIBCXXABI_NO_EXCEPTIONS
58         p = ::operator new(size);
59 #ifndef _LIBCXXABI_NO_EXCEPTIONS
60     }
61     catch (...)
62     {
63     }
64 #endif  // _LIBCXXABI_NO_EXCEPTIONS
65     return p;
66 }
67 
68 _LIBCXXABI_WEAK
69 void*
operator new[](size_t size)70 operator new[](size_t size) _THROW_BAD_ALLOC
71 {
72     return ::operator new(size);
73 }
74 
75 _LIBCXXABI_WEAK
76 void*
operator new[](size_t size,const std::nothrow_t &)77 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
78 {
79     void* p = 0;
80 #ifndef _LIBCXXABI_NO_EXCEPTIONS
81     try
82     {
83 #endif  // _LIBCXXABI_NO_EXCEPTIONS
84         p = ::operator new[](size);
85 #ifndef _LIBCXXABI_NO_EXCEPTIONS
86     }
87     catch (...)
88     {
89     }
90 #endif  // _LIBCXXABI_NO_EXCEPTIONS
91     return p;
92 }
93 
94 _LIBCXXABI_WEAK
95 void
operator delete(void * ptr)96 operator delete(void* ptr) _NOEXCEPT
97 {
98     if (ptr)
99         ::free(ptr);
100 }
101 
102 _LIBCXXABI_WEAK
103 void
operator delete(void * ptr,const std::nothrow_t &)104 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
105 {
106     ::operator delete(ptr);
107 }
108 
109 _LIBCXXABI_WEAK
110 void
operator delete(void * ptr,size_t)111 operator delete(void* ptr, size_t) _NOEXCEPT
112 {
113     ::operator delete(ptr);
114 }
115 
116 _LIBCXXABI_WEAK
117 void
operator delete[](void * ptr)118 operator delete[] (void* ptr) _NOEXCEPT
119 {
120     ::operator delete(ptr);
121 }
122 
123 _LIBCXXABI_WEAK
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 _LIBCXXABI_WEAK
131 void
operator delete[](void * ptr,size_t)132 operator delete[] (void* ptr, size_t) _NOEXCEPT
133 {
134     ::operator delete[](ptr);
135 }
136 
137 #if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
138 
139 _LIBCXXABI_WEAK
140 void *
operator new(std::size_t size,std::align_val_t alignment)141 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
142 {
143     if (size == 0)
144         size = 1;
145     if (static_cast<size_t>(alignment) < sizeof(void*))
146       alignment = std::align_val_t(sizeof(void*));
147     void* p;
148 #if defined(_LIBCPP_WIN32API)
149     while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
150 #else
151     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
152 #endif
153     {
154         // If posix_memalign fails and there is a new_handler,
155         // call it to try free up memory.
156         std::new_handler nh = std::get_new_handler();
157         if (nh)
158             nh();
159         else {
160 #ifndef _LIBCXXABI_NO_EXCEPTIONS
161             throw std::bad_alloc();
162 #else
163             p = nullptr; // posix_memalign doesn't initialize 'p' on failure
164             break;
165 #endif
166         }
167     }
168     return p;
169 }
170 
171 _LIBCXXABI_WEAK
172 void*
operator new(size_t size,std::align_val_t alignment,const std::nothrow_t &)173 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
174 {
175     void* p = 0;
176 #ifndef _LIBCXXABI_NO_EXCEPTIONS
177     try
178     {
179 #endif  // _LIBCXXABI_NO_EXCEPTIONS
180         p = ::operator new(size, alignment);
181 #ifndef _LIBCXXABI_NO_EXCEPTIONS
182     }
183     catch (...)
184     {
185     }
186 #endif  // _LIBCXXABI_NO_EXCEPTIONS
187     return p;
188 }
189 
190 _LIBCXXABI_WEAK
191 void*
operator new[](size_t size,std::align_val_t alignment)192 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
193 {
194     return ::operator new(size, alignment);
195 }
196 
197 _LIBCXXABI_WEAK
198 void*
operator new[](size_t size,std::align_val_t alignment,const std::nothrow_t &)199 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
200 {
201     void* p = 0;
202 #ifndef _LIBCXXABI_NO_EXCEPTIONS
203     try
204     {
205 #endif  // _LIBCXXABI_NO_EXCEPTIONS
206         p = ::operator new[](size, alignment);
207 #ifndef _LIBCXXABI_NO_EXCEPTIONS
208     }
209     catch (...)
210     {
211     }
212 #endif  // _LIBCXXABI_NO_EXCEPTIONS
213     return p;
214 }
215 
216 _LIBCXXABI_WEAK
217 void
operator delete(void * ptr,std::align_val_t)218 operator delete(void* ptr, std::align_val_t) _NOEXCEPT
219 {
220     if (ptr)
221 #if defined(_LIBCPP_WIN32API)
222         ::_aligned_free(ptr);
223 #else
224         ::free(ptr);
225 #endif
226 }
227 
228 _LIBCXXABI_WEAK
229 void
operator delete(void * ptr,std::align_val_t alignment,const std::nothrow_t &)230 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
231 {
232     ::operator delete(ptr, alignment);
233 }
234 
235 _LIBCXXABI_WEAK
236 void
operator delete(void * ptr,size_t,std::align_val_t alignment)237 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
238 {
239     ::operator delete(ptr, alignment);
240 }
241 
242 _LIBCXXABI_WEAK
243 void
operator delete[](void * ptr,std::align_val_t alignment)244 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
245 {
246     ::operator delete(ptr, alignment);
247 }
248 
249 _LIBCXXABI_WEAK
250 void
operator delete[](void * ptr,std::align_val_t alignment,const std::nothrow_t &)251 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
252 {
253     ::operator delete[](ptr, alignment);
254 }
255 
256 _LIBCXXABI_WEAK
257 void
operator delete[](void * ptr,size_t,std::align_val_t alignment)258 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
259 {
260     ::operator delete[](ptr, alignment);
261 }
262 
263 #endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
264