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 #include <stdlib.h>
     11 
     12 #include "new"
     13 
     14 #ifndef __has_include
     15 #define __has_include(inc) 0
     16 #endif
     17 
     18 #if __APPLE__
     19     #include <cxxabi.h>
     20 
     21     #ifndef _LIBCPPABI_VERSION
     22         // On Darwin, there are two STL shared libraries and a lower level ABI
     23         // shared libray.  The global holding the current new handler is
     24         // in the ABI library and named __cxa_new_handler.
     25         #define __new_handler __cxxabiapple::__cxa_new_handler
     26     #endif
     27 #else  // __APPLE__
     28     #if defined(LIBCXXRT) || __has_include(<cxxabi.h>)
     29         #include <cxxabi.h>
     30     #endif  // __has_include(<cxxabi.h>)
     31     #ifndef _LIBCPPABI_VERSION
     32         static std::new_handler __new_handler;
     33     #endif  // _LIBCPPABI_VERSION
     34 #endif
     35 
     36 // Implement all new and delete operators as weak definitions
     37 // in this shared library, so that they can be overriden by programs
     38 // that define non-weak copies of the functions.
     39 
     40 __attribute__((__weak__, __visibility__("default")))
     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 __attribute__((__weak__, __visibility__("default")))
     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 __attribute__((__weak__, __visibility__("default")))
     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 __attribute__((__weak__, __visibility__("default")))
     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 __attribute__((__weak__, __visibility__("default")))
    116 void
    117 operator delete(void* ptr) _NOEXCEPT
    118 {
    119     if (ptr)
    120         ::free(ptr);
    121 }
    122 
    123 __attribute__((__weak__, __visibility__("default")))
    124 void
    125 operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
    126 {
    127     ::operator delete(ptr);
    128 }
    129 
    130 __attribute__((__weak__, __visibility__("default")))
    131 void
    132 operator delete[] (void* ptr) _NOEXCEPT
    133 {
    134     ::operator delete (ptr);
    135 }
    136 
    137 __attribute__((__weak__, __visibility__("default")))
    138 void
    139 operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
    140 {
    141     ::operator delete[](ptr);
    142 }
    143 
    144 namespace std
    145 {
    146 
    147 const nothrow_t nothrow = {};
    148 
    149 #ifndef _LIBCPPABI_VERSION
    150 
    151 new_handler
    152 set_new_handler(new_handler handler) _NOEXCEPT
    153 {
    154     return __sync_lock_test_and_set(&__new_handler, handler);
    155 }
    156 
    157 new_handler
    158 get_new_handler() _NOEXCEPT
    159 {
    160     return __sync_fetch_and_add(&__new_handler, (new_handler)0);
    161 }
    162 
    163 #ifndef LIBCXXRT
    164 
    165 bad_alloc::bad_alloc() _NOEXCEPT
    166 {
    167 }
    168 
    169 bad_alloc::~bad_alloc() _NOEXCEPT
    170 {
    171 }
    172 
    173 const char*
    174 bad_alloc::what() const _NOEXCEPT
    175 {
    176     return "std::bad_alloc";
    177 }
    178 
    179 #endif //LIBCXXRT
    180 
    181 bad_array_new_length::bad_array_new_length() _NOEXCEPT
    182 {
    183 }
    184 
    185 bad_array_new_length::~bad_array_new_length() _NOEXCEPT
    186 {
    187 }
    188 
    189 const char*
    190 bad_array_new_length::what() const _NOEXCEPT
    191 {
    192     return "bad_array_new_length";
    193 }
    194 
    195 #endif
    196 
    197 void
    198 __throw_bad_alloc()
    199 {
    200 #ifndef _LIBCPP_NO_EXCEPTIONS
    201     throw bad_alloc();
    202 #endif
    203 }
    204 
    205 }  // std
    206