Home | History | Annotate | Download | only in src
      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 #ifndef __has_include
     17 #define __has_include(inc) 0
     18 #endif
     19 
     20 #ifdef __APPLE__
     21     #include <cxxabi.h>
     22 
     23     #ifndef _LIBCPPABI_VERSION
     24         // On Darwin, there are two STL shared libraries and a lower level ABI
     25         // shared library.  The global holding the current new handler is
     26         // in the ABI library and named __cxa_new_handler.
     27         #define __new_handler __cxxabiapple::__cxa_new_handler
     28     #endif
     29 #else  // __APPLE__
     30     #if defined(LIBCXXRT) || __has_include(<cxxabi.h>) || defined(__ANDROID__)
     31         #include <cxxabi.h>
     32     #endif  // __has_include(<cxxabi.h>)
     33     #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
     34         static std::new_handler __new_handler;
     35     #endif  // _LIBCPPABI_VERSION
     36 #endif
     37 
     38 // libc++abi provides operator new and delete as part of the abi layer.
     39 #if !defined(__GLIBCXX__) && !defined(_LIBCPPABI_VERSION)
     40 
     41 // Implement all new and delete operators as weak definitions
     42 // in this shared library, so that they can be overriden by programs
     43 // that define non-weak copies of the functions.
     44 
     45 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
     46 void *
     47 operator new(std::size_t size)
     48 #if !__has_feature(cxx_noexcept)
     49     throw(std::bad_alloc)
     50 #endif
     51 {
     52     if (size == 0)
     53         size = 1;
     54     void* p;
     55     while ((p = ::malloc(size)) == 0)
     56     {
     57         // If malloc fails and there is a new_handler,
     58         // call it to try free up memory.
     59         std::new_handler nh = std::get_new_handler();
     60         if (nh)
     61             nh();
     62         else
     63 #ifndef _LIBCPP_NO_EXCEPTIONS
     64             throw std::bad_alloc();
     65 #else
     66             break;
     67 #endif
     68     }
     69     return p;
     70 }
     71 
     72 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
     73 void*
     74 operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
     75 {
     76     void* p = 0;
     77 #ifndef _LIBCPP_NO_EXCEPTIONS
     78     try
     79     {
     80 #endif  // _LIBCPP_NO_EXCEPTIONS
     81         p = ::operator new(size);
     82 #ifndef _LIBCPP_NO_EXCEPTIONS
     83     }
     84     catch (...)
     85     {
     86     }
     87 #endif  // _LIBCPP_NO_EXCEPTIONS
     88     return p;
     89 }
     90 
     91 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
     92 void*
     93 operator new[](size_t size)
     94 #if !__has_feature(cxx_noexcept)
     95     throw(std::bad_alloc)
     96 #endif
     97 {
     98     return ::operator new(size);
     99 }
    100 
    101 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
    102 void*
    103 operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
    104 {
    105     void* p = 0;
    106 #ifndef _LIBCPP_NO_EXCEPTIONS
    107     try
    108     {
    109 #endif  // _LIBCPP_NO_EXCEPTIONS
    110         p = ::operator new[](size);
    111 #ifndef _LIBCPP_NO_EXCEPTIONS
    112     }
    113     catch (...)
    114     {
    115     }
    116 #endif  // _LIBCPP_NO_EXCEPTIONS
    117     return p;
    118 }
    119 
    120 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
    121 void
    122 operator delete(void* ptr) _NOEXCEPT
    123 {
    124     if (ptr)
    125         ::free(ptr);
    126 }
    127 
    128 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
    129 void
    130 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
    131 {
    132     ::operator delete(ptr);
    133 }
    134 
    135 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
    136 void
    137 operator delete[] (void* ptr) _NOEXCEPT
    138 {
    139     ::operator delete (ptr);
    140 }
    141 
    142 _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
    143 void
    144 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
    145 {
    146     ::operator delete[](ptr);
    147 }
    148 
    149 #endif // !defined(__GLIBCXX__) && !defined(_LIBCPPABI_VERSION)
    150 
    151 namespace std
    152 {
    153 
    154 #ifndef __GLIBCXX__
    155 const nothrow_t nothrow = {};
    156 #endif
    157 
    158 #ifndef _LIBCPPABI_VERSION
    159 
    160 #ifndef __GLIBCXX__
    161 
    162 new_handler
    163 set_new_handler(new_handler handler) _NOEXCEPT
    164 {
    165     return __sync_lock_test_and_set(&__new_handler, handler);
    166 }
    167 
    168 new_handler
    169 get_new_handler() _NOEXCEPT
    170 {
    171     return __sync_fetch_and_add(&__new_handler, nullptr);
    172 }
    173 
    174 #endif // !__GLIBCXX__
    175 
    176 #ifndef LIBCXXRT
    177 
    178 bad_alloc::bad_alloc() _NOEXCEPT
    179 {
    180 }
    181 
    182 #ifndef __GLIBCXX__
    183 
    184 bad_alloc::~bad_alloc() _NOEXCEPT
    185 {
    186 }
    187 
    188 const char*
    189 bad_alloc::what() const _NOEXCEPT
    190 {
    191     return "std::bad_alloc";
    192 }
    193 
    194 #endif // !__GLIBCXX__
    195 
    196 #endif //LIBCXXRT
    197 
    198 bad_array_new_length::bad_array_new_length() _NOEXCEPT
    199 {
    200 }
    201 
    202 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
    203 {
    204 }
    205 
    206 const char*
    207 bad_array_length::what() const _NOEXCEPT
    208 {
    209     return "bad_array_length";
    210 }
    211 
    212 bad_array_length::bad_array_length() _NOEXCEPT
    213 {
    214 }
    215 
    216 bad_array_length::~bad_array_length() _NOEXCEPT
    217 {
    218 }
    219 
    220 const char*
    221 bad_array_new_length::what() const _NOEXCEPT
    222 {
    223     return "bad_array_new_length";
    224 }
    225 
    226 #endif // _LIBCPPABI_VERSION
    227 
    228 #ifndef LIBSTDCXX
    229 
    230 void
    231 __throw_bad_alloc()
    232 {
    233 #ifndef _LIBCPP_NO_EXCEPTIONS
    234     throw bad_alloc();
    235 #endif
    236 }
    237 
    238 #endif // !LIBSTDCXX
    239 
    240 }  // std
    241