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#ifdef __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")))
41void *
42operator 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")))
68void*
69operator 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")))
87void*
88operator 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")))
97void*
98operator 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")))
116void
117operator delete(void* ptr) _NOEXCEPT
118{
119    if (ptr)
120        ::free(ptr);
121}
122
123__attribute__((__weak__, __visibility__("default")))
124void
125operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
126{
127    ::operator delete(ptr);
128}
129
130__attribute__((__weak__, __visibility__("default")))
131void
132operator delete[] (void* ptr) _NOEXCEPT
133{
134    ::operator delete (ptr);
135}
136
137__attribute__((__weak__, __visibility__("default")))
138void
139operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
140{
141    ::operator delete[](ptr);
142}
143
144namespace std
145{
146
147const nothrow_t nothrow = {};
148
149#ifndef _LIBCPPABI_VERSION
150
151new_handler
152set_new_handler(new_handler handler) _NOEXCEPT
153{
154    return __sync_lock_test_and_set(&__new_handler, handler);
155}
156
157new_handler
158get_new_handler() _NOEXCEPT
159{
160    return __sync_fetch_and_add(&__new_handler, (new_handler)0);
161}
162
163#ifndef LIBCXXRT
164
165bad_alloc::bad_alloc() _NOEXCEPT
166{
167}
168
169bad_alloc::~bad_alloc() _NOEXCEPT
170{
171}
172
173const char*
174bad_alloc::what() const _NOEXCEPT
175{
176    return "std::bad_alloc";
177}
178
179#endif //LIBCXXRT
180
181bad_array_new_length::bad_array_new_length() _NOEXCEPT
182{
183}
184
185bad_array_new_length::~bad_array_new_length() _NOEXCEPT
186{
187}
188
189const char*
190bad_array_new_length::what() const _NOEXCEPT
191{
192    return "bad_array_new_length";
193}
194
195#endif
196
197void
198__throw_bad_alloc()
199{
200#ifndef _LIBCPP_NO_EXCEPTIONS
201    throw bad_alloc();
202#endif
203}
204
205}  // std
206