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 #define _LIBCPP_BUILDING_NEW 11 12 #include <stdlib.h> 13 14 #include "new" 15 16 #if defined(__APPLE__) && !defined(LIBCXXRT) && \ 17 !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) 18 #include <cxxabi.h> 19 20 #ifndef _LIBCPPABI_VERSION 21 // On Darwin, there are two STL shared libraries and a lower level ABI 22 // shared library. The global holding the current new handler is 23 // in the ABI library and named __cxa_new_handler. 24 #define __new_handler __cxxabiapple::__cxa_new_handler 25 #endif 26 #else // __APPLE__ 27 #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) 28 #include <cxxabi.h> 29 #endif // defined(LIBCXX_BUILDING_LIBCXXABI) 30 #if defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY) || \ 31 (!defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)) 32 static std::new_handler __new_handler; 33 #endif // _LIBCPPABI_VERSION 34 #endif 35 36 #ifndef __GLIBCXX__ 37 38 // Implement all new and delete operators as weak definitions 39 // in this shared library, so that they can be overridden by programs 40 // that define non-weak copies of the functions. 41 42 _LIBCPP_WEAK 43 void * 44 operator new(std::size_t size) _THROW_BAD_ALLOC 45 { 46 if (size == 0) 47 size = 1; 48 void* p; 49 while ((p = ::malloc(size)) == 0) 50 { 51 // If malloc fails and there is a new_handler, 52 // call it to try free up memory. 53 std::new_handler nh = std::get_new_handler(); 54 if (nh) 55 nh(); 56 else 57 #ifndef _LIBCPP_NO_EXCEPTIONS 58 throw std::bad_alloc(); 59 #else 60 break; 61 #endif 62 } 63 return p; 64 } 65 66 _LIBCPP_WEAK 67 void* 68 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT 69 { 70 void* p = 0; 71 #ifndef _LIBCPP_NO_EXCEPTIONS 72 try 73 { 74 #endif // _LIBCPP_NO_EXCEPTIONS 75 p = ::operator new(size); 76 #ifndef _LIBCPP_NO_EXCEPTIONS 77 } 78 catch (...) 79 { 80 } 81 #endif // _LIBCPP_NO_EXCEPTIONS 82 return p; 83 } 84 85 _LIBCPP_WEAK 86 void* 87 operator new[](size_t size) _THROW_BAD_ALLOC 88 { 89 return ::operator new(size); 90 } 91 92 _LIBCPP_WEAK 93 void* 94 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT 95 { 96 void* p = 0; 97 #ifndef _LIBCPP_NO_EXCEPTIONS 98 try 99 { 100 #endif // _LIBCPP_NO_EXCEPTIONS 101 p = ::operator new[](size); 102 #ifndef _LIBCPP_NO_EXCEPTIONS 103 } 104 catch (...) 105 { 106 } 107 #endif // _LIBCPP_NO_EXCEPTIONS 108 return p; 109 } 110 111 _LIBCPP_WEAK 112 void 113 operator delete(void* ptr) _NOEXCEPT 114 { 115 if (ptr) 116 ::free(ptr); 117 } 118 119 _LIBCPP_WEAK 120 void 121 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT 122 { 123 ::operator delete(ptr); 124 } 125 126 _LIBCPP_WEAK 127 void 128 operator delete(void* ptr, size_t) _NOEXCEPT 129 { 130 ::operator delete(ptr); 131 } 132 133 _LIBCPP_WEAK 134 void 135 operator delete[] (void* ptr) _NOEXCEPT 136 { 137 ::operator delete(ptr); 138 } 139 140 _LIBCPP_WEAK 141 void 142 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT 143 { 144 ::operator delete[](ptr); 145 } 146 147 _LIBCPP_WEAK 148 void 149 operator delete[] (void* ptr, size_t) _NOEXCEPT 150 { 151 ::operator delete[](ptr); 152 } 153 154 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 155 156 _LIBCPP_WEAK 157 void * 158 operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC 159 { 160 if (size == 0) 161 size = 1; 162 if (static_cast<size_t>(alignment) < sizeof(void*)) 163 alignment = std::align_val_t(sizeof(void*)); 164 void* p; 165 #if defined(_LIBCPP_MSVCRT) 166 while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr) 167 #else 168 while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0) 169 #endif 170 { 171 // If posix_memalign fails and there is a new_handler, 172 // call it to try free up memory. 173 std::new_handler nh = std::get_new_handler(); 174 if (nh) 175 nh(); 176 else { 177 #ifndef _LIBCPP_NO_EXCEPTIONS 178 throw std::bad_alloc(); 179 #else 180 p = nullptr; // posix_memalign doesn't initialize 'p' on failure 181 break; 182 #endif 183 } 184 } 185 return p; 186 } 187 188 _LIBCPP_WEAK 189 void* 190 operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 191 { 192 void* p = 0; 193 #ifndef _LIBCPP_NO_EXCEPTIONS 194 try 195 { 196 #endif // _LIBCPP_NO_EXCEPTIONS 197 p = ::operator new(size, alignment); 198 #ifndef _LIBCPP_NO_EXCEPTIONS 199 } 200 catch (...) 201 { 202 } 203 #endif // _LIBCPP_NO_EXCEPTIONS 204 return p; 205 } 206 207 _LIBCPP_WEAK 208 void* 209 operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC 210 { 211 return ::operator new(size, alignment); 212 } 213 214 _LIBCPP_WEAK 215 void* 216 operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 217 { 218 void* p = 0; 219 #ifndef _LIBCPP_NO_EXCEPTIONS 220 try 221 { 222 #endif // _LIBCPP_NO_EXCEPTIONS 223 p = ::operator new[](size, alignment); 224 #ifndef _LIBCPP_NO_EXCEPTIONS 225 } 226 catch (...) 227 { 228 } 229 #endif // _LIBCPP_NO_EXCEPTIONS 230 return p; 231 } 232 233 _LIBCPP_WEAK 234 void 235 operator delete(void* ptr, std::align_val_t) _NOEXCEPT 236 { 237 if (ptr) 238 #if defined(_LIBCPP_MSVCRT) 239 ::_aligned_free(ptr); 240 #else 241 ::free(ptr); 242 #endif 243 } 244 245 _LIBCPP_WEAK 246 void 247 operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 248 { 249 ::operator delete(ptr, alignment); 250 } 251 252 _LIBCPP_WEAK 253 void 254 operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT 255 { 256 ::operator delete(ptr, alignment); 257 } 258 259 _LIBCPP_WEAK 260 void 261 operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT 262 { 263 ::operator delete(ptr, alignment); 264 } 265 266 _LIBCPP_WEAK 267 void 268 operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT 269 { 270 ::operator delete[](ptr, alignment); 271 } 272 273 _LIBCPP_WEAK 274 void 275 operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT 276 { 277 ::operator delete[](ptr, alignment); 278 } 279 280 #endif // !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) 281 282 #endif // !__GLIBCXX__ 283 284 namespace std 285 { 286 287 #ifndef __GLIBCXX__ 288 const nothrow_t nothrow = {}; 289 #endif 290 291 #ifndef _LIBCPPABI_VERSION 292 293 #ifndef __GLIBCXX__ 294 295 new_handler 296 set_new_handler(new_handler handler) _NOEXCEPT 297 { 298 return __sync_lock_test_and_set(&__new_handler, handler); 299 } 300 301 new_handler 302 get_new_handler() _NOEXCEPT 303 { 304 return __sync_fetch_and_add(&__new_handler, nullptr); 305 } 306 307 #endif // !__GLIBCXX__ 308 309 #ifndef LIBCXXRT 310 311 bad_alloc::bad_alloc() _NOEXCEPT 312 { 313 } 314 315 #ifndef __GLIBCXX__ 316 317 bad_alloc::~bad_alloc() _NOEXCEPT 318 { 319 } 320 321 const char* 322 bad_alloc::what() const _NOEXCEPT 323 { 324 return "std::bad_alloc"; 325 } 326 327 #endif // !__GLIBCXX__ 328 329 bad_array_new_length::bad_array_new_length() _NOEXCEPT 330 { 331 } 332 333 #ifndef __GLIBCXX__ 334 335 bad_array_new_length::~bad_array_new_length() _NOEXCEPT 336 { 337 } 338 339 const char* 340 bad_array_new_length::what() const _NOEXCEPT 341 { 342 return "bad_array_new_length"; 343 } 344 345 #endif // !__GLIBCXX__ 346 347 #endif //LIBCXXRT 348 349 bad_array_length::bad_array_length() _NOEXCEPT 350 { 351 } 352 353 #ifndef __GLIBCXX__ 354 355 bad_array_length::~bad_array_length() _NOEXCEPT 356 { 357 } 358 359 const char* 360 bad_array_length::what() const _NOEXCEPT 361 { 362 return "bad_array_length"; 363 } 364 365 #endif // !__GLIBCXX__ 366 367 #endif // _LIBCPPABI_VERSION 368 369 #ifndef LIBSTDCXX 370 371 void 372 __throw_bad_alloc() 373 { 374 #ifndef _LIBCPP_NO_EXCEPTIONS 375 throw bad_alloc(); 376 #else 377 _VSTD::abort(); 378 #endif 379 } 380 381 #endif // !LIBSTDCXX 382 383 } // std 384