Home | History | Annotate | Download | only in include
      1 // -*- C++ -*-
      2 //===----------------------------- new ------------------------------------===//
      3 //
      4 //                     The LLVM Compiler Infrastructure
      5 //
      6 // This file is dual licensed under the MIT and the University of Illinois Open
      7 // Source Licenses. See LICENSE.TXT for details.
      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:
     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;
     28 };
     29 
     30 class bad_array_length : public bad_alloc // FIXME: Not part of C++
     31 {
     32 public:
     33     bad_array_length() noexcept;
     34 };
     35 
     36 class bad_array_new_length : public bad_alloc // C++14
     37 {
     38 public:
     39     bad_array_new_length() noexcept;
     40 };
     41 
     42 enum class align_val_t : size_t {}; // C++17
     43 struct nothrow_t {};
     44 extern const nothrow_t nothrow;
     45 typedef void (*new_handler)();
     46 new_handler set_new_handler(new_handler new_p) noexcept;
     47 new_handler get_new_handler() noexcept;
     48 
     49 // 21.6.4, pointer optimization barrier
     50 template <class T> constexpr T* launder(T* p) noexcept; // C++17
     51 }  // std
     52 
     53 void* operator new(std::size_t size);                                   // replaceable, nodiscard in C++2a
     54 void* operator new(std::size_t size, std::align_val_t alignment);       // replaceable, C++17, nodiscard in C++2a
     55 void* operator new(std::size_t size, const std::nothrow_t&) noexcept;   // replaceable, nodiscard in C++2a
     56 void* operator new(std::size_t size, std::align_val_t alignment,
     57                    const std::nothrow_t&) noexcept;                     // replaceable, C++17, nodiscard in C++2a
     58 void  operator delete(void* ptr) noexcept;                              // replaceable
     59 void  operator delete(void* ptr, std::size_t size) noexcept;            // replaceable, C++14
     60 void  operator delete(void* ptr, std::align_val_t alignment) noexcept;  // replaceable, C++17
     61 void  operator delete(void* ptr, std::size_t size,
     62                       std::align_val_t alignment) noexcept;             // replaceable, C++17
     63 void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
     64 void  operator delete(void* ptr, std:align_val_t alignment,
     65                       const std::nothrow_t&) noexcept;                  // replaceable, C++17
     66 
     67 void* operator new[](std::size_t size);                                 // replaceable, nodiscard in C++2a
     68 void* operator new[](std::size_t size,
     69                      std::align_val_t alignment) noexcept;              // replaceable, C++17, nodiscard in C++2a
     70 void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable, nodiscard in C++2a
     71 void* operator new[](std::size_t size, std::align_val_t alignment,
     72                      const std::nothrow_t&) noexcept;                   // replaceable, C++17, nodiscard in C++2a
     73 void  operator delete[](void* ptr) noexcept;                            // replaceable
     74 void  operator delete[](void* ptr, std::size_t size) noexcept;          // replaceable, C++14
     75 void  operator delete[](void* ptr,
     76                         std::align_val_t alignment) noexcept;           // replaceable, C++17
     77 void  operator delete[](void* ptr, std::size_t size,
     78                         std::align_val_t alignment) noexcept;           // replaceable, C++17
     79 void  operator delete[](void* ptr, const std::nothrow_t&) noexcept;     // replaceable
     80 void  operator delete[](void* ptr, std::align_val_t alignment,
     81                         const std::nothrow_t&) noexcept;                // replaceable, C++17
     82 
     83 void* operator new  (std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
     84 void* operator new[](std::size_t size, void* ptr) noexcept;             // nodiscard in C++2a
     85 void  operator delete  (void* ptr, void*) noexcept;
     86 void  operator delete[](void* ptr, void*) noexcept;
     87 
     88 */
     89 
     90 #include <__config>
     91 #include <exception>
     92 #include <cstddef>
     93 #ifdef _LIBCPP_NO_EXCEPTIONS
     94 #include <cstdlib>
     95 #endif
     96 
     97 #if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
     98 #include <new.h>
     99 #endif
    100 
    101 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
    102 #pragma GCC system_header
    103 #endif
    104 
    105 #if !(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
    106     (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309))
    107 # define _LIBCPP_HAS_NO_SIZED_DEALLOCATION
    108 #endif
    109 
    110 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) && \
    111     (!(defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER > 14 || \
    112     (defined(__cpp_aligned_new) && __cpp_aligned_new >= 201606)))
    113 # define _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
    114 #endif
    115 
    116 namespace std  // purposefully not using versioning namespace
    117 {
    118 
    119 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
    120 struct _LIBCPP_TYPE_VIS nothrow_t {};
    121 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
    122 
    123 class _LIBCPP_EXCEPTION_ABI bad_alloc
    124     : public exception
    125 {
    126 public:
    127     bad_alloc() _NOEXCEPT;
    128     virtual ~bad_alloc() _NOEXCEPT;
    129     virtual const char* what() const _NOEXCEPT;
    130 };
    131 
    132 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
    133     : public bad_alloc
    134 {
    135 public:
    136     bad_array_new_length() _NOEXCEPT;
    137     virtual ~bad_array_new_length() _NOEXCEPT;
    138     virtual const char* what() const _NOEXCEPT;
    139 };
    140 
    141 typedef void (*new_handler)();
    142 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
    143 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
    144 
    145 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
    146 
    147 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
    148 
    149 #if defined(_LIBCPP_BUILDING_LIBRARY) || (_LIBCPP_STD_VER > 11)
    150 
    151 class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
    152     bad_array_length : public bad_alloc {
    153 public:
    154     bad_array_length() _NOEXCEPT;
    155     virtual ~bad_array_length() _NOEXCEPT;
    156     virtual const char* what() const _NOEXCEPT;
    157 };
    158 
    159 #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
    160 
    161 #endif  // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
    162 
    163 #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION) || _LIBCPP_STD_VER > 14
    164 #ifndef _LIBCPP_CXX03_LANG
    165 enum class _LIBCPP_ENUM_VIS align_val_t : size_t { };
    166 #else
    167 enum align_val_t { __zero = 0, __max = (size_t)-1 };
    168 #endif
    169 #endif
    170 
    171 }  // std
    172 
    173 #if defined(_LIBCPP_CXX03_LANG)
    174 #define _THROW_BAD_ALLOC throw(std::bad_alloc)
    175 #else
    176 #define _THROW_BAD_ALLOC
    177 #endif
    178 
    179 #if !defined(_LIBCPP_ABI_MICROSOFT) || defined(_LIBCPP_NO_VCRUNTIME)
    180 
    181 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
    182 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
    183 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
    184 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
    185 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
    186 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
    187 #endif
    188 
    189 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
    190 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
    191 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
    192 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
    193 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
    194 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
    195 #endif
    196 
    197 #ifndef _LIBCPP_HAS_NO_ALIGNED_ALLOCATION
    198 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
    199 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
    200 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
    201 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
    202 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
    203 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete(void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
    204 #endif
    205 
    206 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
    207 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
    208 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
    209 _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t, const std::nothrow_t&) _NOEXCEPT;
    210 #ifndef _LIBCPP_HAS_NO_SIZED_DEALLOCATION
    211 _LIBCPP_OVERRIDABLE_FUNC_VIS _LIBCPP_AVAILABILITY_SIZED_NEW_DELETE void  operator delete[](void* __p, std::size_t __sz, std::align_val_t) _NOEXCEPT;
    212 #endif
    213 #endif
    214 
    215 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
    216 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
    217 inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
    218 inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
    219 
    220 #endif // !_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME
    221 
    222 _LIBCPP_BEGIN_NAMESPACE_STD
    223 
    224 inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
    225 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
    226   return ::operator new(__size);
    227 #else
    228   return __builtin_operator_new(__size);
    229 #endif
    230 }
    231 
    232 inline _LIBCPP_INLINE_VISIBILITY void __libcpp_deallocate(void *__ptr) {
    233 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
    234   ::operator delete(__ptr);
    235 #else
    236   __builtin_operator_delete(__ptr);
    237 #endif
    238 }
    239 
    240 #ifdef _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
    241 _LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
    242 #ifndef _LIBCPP_NO_EXCEPTIONS
    243 _LIBCPP_AVAILABILITY_BAD_ARRAY_LENGTH
    244 #endif
    245 void __throw_bad_array_length()
    246 {
    247 #ifndef _LIBCPP_NO_EXCEPTIONS
    248     throw bad_array_length();
    249 #else
    250 	_VSTD::abort();
    251 #endif
    252 }
    253 #endif
    254 
    255 template <class _Tp>
    256 _LIBCPP_NODISCARD_AFTER_CXX17 inline 
    257 _LIBCPP_CONSTEXPR _Tp* __launder(_Tp* __p) _NOEXCEPT
    258 {
    259     static_assert (!(is_function<_Tp>::value), "can't launder functions" );
    260     static_assert (!(is_same<void, typename remove_cv<_Tp>::type>::value), "can't launder cv-void" );
    261 #ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
    262     return __builtin_launder(__p);
    263 #else
    264     return __p;
    265 #endif
    266 }
    267 
    268 
    269 #if _LIBCPP_STD_VER > 14
    270 template <class _Tp>
    271 _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY
    272 constexpr _Tp* launder(_Tp* __p) noexcept
    273 {
    274     return _VSTD::__launder(__p);
    275 }
    276 #endif
    277 
    278 _LIBCPP_END_NAMESPACE_STD
    279 
    280 #endif  // _LIBCPP_NEW
    281