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