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