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