| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- | 
 | 2 | //===----------------------------- new ------------------------------------===// | 
 | 3 | // | 
| Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | //                     The LLVM Compiler Infrastructure | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // | 
| Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open | 
 | 7 | // Source Licenses. See LICENSE.TXT for details. | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // | 
 | 9 | //===----------------------------------------------------------------------===// | 
 | 10 |  | 
 | 11 | #ifndef _LIBCPP_NEW | 
 | 12 | #define _LIBCPP_NEW | 
 | 13 |  | 
 | 14 | /* | 
 | 15 |     new synopsis | 
 | 16 |  | 
 | 17 | namespace std | 
 | 18 | { | 
 | 19 |  | 
 | 20 | class bad_alloc | 
 | 21 |     : public exception | 
 | 22 | { | 
 | 23 | public: | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 24 |     bad_alloc() noexcept; | 
 | 25 |     bad_alloc(const bad_alloc&) noexcept; | 
 | 26 |     bad_alloc& operator=(const bad_alloc&) noexcept; | 
 | 27 |     virtual const char* what() const noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | }; | 
 | 29 |  | 
| Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 30 | class bad_array_length : public bad_alloc // C++14 | 
 | 31 | { | 
 | 32 | public: | 
 | 33 |     bad_array_length() noexcept; | 
 | 34 | }; | 
 | 35 |  | 
 | 36 | class bad_array_new_length : public bad_alloc | 
 | 37 | { | 
 | 38 | public: | 
 | 39 |     bad_array_new_length() noexcept; | 
 | 40 | }; | 
 | 41 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | struct nothrow_t {}; | 
 | 43 | extern const nothrow_t nothrow; | 
 | 44 | typedef void (*new_handler)(); | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 45 | new_handler set_new_handler(new_handler new_p) noexcept; | 
 | 46 | new_handler get_new_handler() noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 |  | 
 | 48 | }  // std | 
 | 49 |  | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 50 | void* operator new(std::size_t size);                                   // replaceable | 
 | 51 | void* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable | 
 | 52 | void  operator delete(void* ptr) noexcept;                              // replaceable | 
 | 53 | void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 |  | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 55 | void* operator new[](std::size_t size);                                 // replaceable | 
 | 56 | void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable | 
 | 57 | void  operator delete[](void* ptr) noexcept;                            // replaceable | 
 | 58 | void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 |  | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 60 | void* operator new  (std::size_t size, void* ptr) noexcept; | 
 | 61 | void* operator new[](std::size_t size, void* ptr) noexcept; | 
 | 62 | void  operator delete  (void* ptr, void*) noexcept; | 
 | 63 | void  operator delete[](void* ptr, void*) noexcept; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 |  | 
 | 65 | */ | 
 | 66 |  | 
 | 67 | #include <__config> | 
 | 68 | #include <exception> | 
 | 69 | #include <cstddef> | 
 | 70 |  | 
| Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 71 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | #pragma GCC system_header | 
| Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 73 | #endif | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 74 |  | 
 | 75 | namespace std  // purposefully not using versioning namespace | 
 | 76 | { | 
 | 77 |  | 
 | 78 | class _LIBCPP_EXCEPTION_ABI bad_alloc | 
 | 79 |     : public exception | 
 | 80 | { | 
 | 81 | public: | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 82 |     bad_alloc() _NOEXCEPT; | 
 | 83 |     virtual ~bad_alloc() _NOEXCEPT; | 
 | 84 |     virtual const char* what() const _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | }; | 
 | 86 |  | 
 | 87 | class _LIBCPP_EXCEPTION_ABI bad_array_new_length | 
 | 88 |     : public bad_alloc | 
 | 89 | { | 
 | 90 | public: | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 91 |     bad_array_new_length() _NOEXCEPT; | 
 | 92 |     virtual ~bad_array_new_length() _NOEXCEPT; | 
 | 93 |     virtual const char* what() const _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 94 | }; | 
 | 95 |  | 
| Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 96 | #if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11) | 
| Howard Hinnant | 29250b7 | 2013-11-07 17:15:51 +0000 | [diff] [blame] | 97 |  | 
| Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 98 | class _LIBCPP_EXCEPTION_ABI bad_array_length | 
 | 99 |     : public bad_alloc | 
 | 100 | { | 
 | 101 | public: | 
 | 102 |     bad_array_length() _NOEXCEPT; | 
 | 103 |     virtual ~bad_array_length() _NOEXCEPT; | 
 | 104 |     virtual const char* what() const _NOEXCEPT; | 
 | 105 | }; | 
| Howard Hinnant | 29250b7 | 2013-11-07 17:15:51 +0000 | [diff] [blame] | 106 |  | 
 | 107 | #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED | 
 | 108 |  | 
 | 109 | #endif  // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11) | 
| Marshall Clow | 7f9f52e | 2013-09-11 01:38:42 +0000 | [diff] [blame] | 110 |  | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 111 | _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 |  | 
| Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 113 | struct _LIBCPP_TYPE_VIS nothrow_t {}; | 
 | 114 | extern _LIBCPP_FUNC_VIS const nothrow_t nothrow; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | typedef void (*new_handler)(); | 
| Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 116 | _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT; | 
 | 117 | _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT; | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 |  | 
 | 119 | }  // std | 
 | 120 |  | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 121 | #if defined(_WIN32) && !defined(cxx_EXPORTS) | 
 | 122 | # define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS_ONLY | 
 | 123 | #else | 
 | 124 | # define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 125 | #endif | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 |  | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 127 | _LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz) | 
| Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 128 | #if !__has_feature(cxx_noexcept) | 
 | 129 |     throw(std::bad_alloc) | 
 | 130 | #endif | 
 | 131 | ; | 
| Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 132 | _LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS; | 
 | 133 | _LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p) _NOEXCEPT; | 
 | 134 | _LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT; | 
 | 135 |  | 
 | 136 | _LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz) | 
 | 137 | #if !__has_feature(cxx_noexcept) | 
 | 138 |     throw(std::bad_alloc) | 
 | 139 | #endif | 
 | 140 | ; | 
 | 141 | _LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS; | 
 | 142 | _LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p) _NOEXCEPT; | 
 | 143 | _LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT; | 
 | 144 |  | 
| Howard Hinnant | 1e56424 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 145 | inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;} | 
 | 146 | inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;} | 
 | 147 | inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {} | 
 | 148 | inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {} | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 |  | 
| Richard Smith | 73c1fce | 2014-06-04 19:54:15 +0000 | [diff] [blame^] | 150 | _LIBCPP_BEGIN_NAMESPACE_STD | 
 | 151 |  | 
 | 152 | inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) { | 
 | 153 | #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE | 
 | 154 |   return ::operator new(__size); | 
 | 155 | #else | 
 | 156 |   return __builtin_operator_new(__size); | 
 | 157 | #endif | 
 | 158 | } | 
 | 159 |  | 
 | 160 | inline _LIBCPP_INLINE_VISIBILITY void __deallocate(void *__ptr) { | 
 | 161 | #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE | 
 | 162 |   ::operator delete(__ptr); | 
 | 163 | #else | 
 | 164 |   __builtin_operator_delete(__ptr); | 
 | 165 | #endif | 
 | 166 | } | 
 | 167 |  | 
 | 168 | _LIBCPP_END_NAMESPACE_STD | 
 | 169 |  | 
| Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 170 | #endif  // _LIBCPP_NEW |