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 // 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 
     42 struct nothrow_t {};
     43 extern const nothrow_t nothrow;
     44 typedef void (*new_handler)();
     45 new_handler set_new_handler(new_handler new_p) noexcept;
     46 new_handler get_new_handler() noexcept;
     47 
     48 }  // std
     49 
     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, std::size_t size) noexcept;            // replaceable, C++14
     54 void  operator delete(void* ptr, const std::nothrow_t&) noexcept;       // replaceable
     55 
     56 void* operator new[](std::size_t size);                                 // replaceable
     57 void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
     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, const std::nothrow_t&) noexcept;     // replaceable
     61 
     62 void* operator new  (std::size_t size, void* ptr) noexcept;
     63 void* operator new[](std::size_t size, void* ptr) noexcept;
     64 void  operator delete  (void* ptr, void*) noexcept;
     65 void  operator delete[](void* ptr, void*) noexcept;
     66 
     67 */
     68 
     69 #include <__config>
     70 #include <exception>
     71 #include <cstddef>
     72 
     73 #include <__undef___deallocate>
     74 
     75 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
     76 #pragma GCC system_header
     77 #endif
     78 
     79 namespace std  // purposefully not using versioning namespace
     80 {
     81 
     82 class _LIBCPP_EXCEPTION_ABI bad_alloc
     83     : public exception
     84 {
     85 public:
     86     bad_alloc() _NOEXCEPT;
     87     virtual ~bad_alloc() _NOEXCEPT;
     88     virtual const char* what() const _NOEXCEPT;
     89 };
     90 
     91 class _LIBCPP_EXCEPTION_ABI bad_array_new_length
     92     : public bad_alloc
     93 {
     94 public:
     95     bad_array_new_length() _NOEXCEPT;
     96     virtual ~bad_array_new_length() _NOEXCEPT;
     97     virtual const char* what() const _NOEXCEPT;
     98 };
     99 
    100 #if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
    101 
    102 class _LIBCPP_EXCEPTION_ABI bad_array_length
    103     : public bad_alloc
    104 {
    105 public:
    106     bad_array_length() _NOEXCEPT;
    107     virtual ~bad_array_length() _NOEXCEPT;
    108     virtual const char* what() const _NOEXCEPT;
    109 };
    110 
    111 #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
    112 
    113 #endif  // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
    114 
    115 _LIBCPP_FUNC_VIS void __throw_bad_alloc();  // not in C++ spec
    116 
    117 struct _LIBCPP_TYPE_VIS nothrow_t {};
    118 extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
    119 typedef void (*new_handler)();
    120 _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
    121 _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
    122 
    123 }  // std
    124 
    125 #if defined(_WIN32) && !defined(cxx_EXPORTS)
    126 # define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS_ONLY
    127 #else
    128 # define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
    129 #endif
    130 
    131 _LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz)
    132 #if !__has_feature(cxx_noexcept)
    133     throw(std::bad_alloc)
    134 #endif
    135 ;
    136 _LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
    137 _LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p) _NOEXCEPT;
    138 _LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
    139 #if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
    140     (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
    141 _LIBCPP_NEW_DELETE_VIS void  operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
    142 #endif
    143 
    144 _LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz)
    145 #if !__has_feature(cxx_noexcept)
    146     throw(std::bad_alloc)
    147 #endif
    148 ;
    149 _LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
    150 _LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p) _NOEXCEPT;
    151 _LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
    152 #if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
    153     (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
    154 _LIBCPP_NEW_DELETE_VIS void  operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
    155 #endif
    156 
    157 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
    158 inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
    159 inline _LIBCPP_INLINE_VISIBILITY void  operator delete  (void*, void*) _NOEXCEPT {}
    160 inline _LIBCPP_INLINE_VISIBILITY void  operator delete[](void*, void*) _NOEXCEPT {}
    161 
    162 _LIBCPP_BEGIN_NAMESPACE_STD
    163 
    164 inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
    165 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
    166   return ::operator new(__size);
    167 #else
    168   return __builtin_operator_new(__size);
    169 #endif
    170 }
    171 
    172 inline _LIBCPP_INLINE_VISIBILITY void __deallocate(void *__ptr) {
    173 #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
    174   ::operator delete(__ptr);
    175 #else
    176   __builtin_operator_delete(__ptr);
    177 #endif
    178 }
    179 
    180 _LIBCPP_END_NAMESPACE_STD
    181 
    182 #endif  // _LIBCPP_NEW
    183