blob: 964d87b89cc2ec7ef7ad600a67cc36ad933af4fc [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===--------------------------- new.cpp ----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Howard Hinnantadff4892010-05-24 17:49:41 +000010#include <stdlib.h>
Nick Kledzik804b6e72010-05-14 20:19:37 +000011
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000012#include "new"
13
Nick Kledzik804b6e72010-05-14 20:19:37 +000014
15#if __APPLE__
Howard Hinnantadff4892010-05-24 17:49:41 +000016 #include <cxxabi.h>
Nick Kledzik804b6e72010-05-14 20:19:37 +000017 // On Darwin, there are two STL shared libraries and a lower level ABI
18 // shared libray. The global holding the current new handler is
19 // in the ABI library and named __cxa_new_handler.
20 #define __new_handler __cxxabiapple::__cxa_new_handler
21#else
22 static std::new_handler __new_handler;
23#endif
24
25
26// Implement all new and delete operators as weak definitions
27// in this shared library, so that they can be overriden by programs
28// that define non-weak copies of the functions.
29
30
31__attribute__((__weak__, __visibility__("default")))
32void *
33operator new(std::size_t size) throw (std::bad_alloc)
34{
35 if (size == 0)
36 size = 1;
37 void* p;
38 while ((p = ::malloc(size)) == 0)
39 {
40 // If malloc fails and there is a new_handler,
41 // call it to try free up memory.
42 if (__new_handler)
43 __new_handler();
44 else
45 throw std::bad_alloc();
46 }
47 return p;
48}
49
50__attribute__((__weak__, __visibility__("default")))
51void*
52operator new(size_t size, const std::nothrow_t&) throw()
53{
54 void* p = 0;
55 try
56 {
57 p = ::operator new(size);
58 }
59 catch (...)
60 {
61 }
62 return p;
63}
64
65__attribute__((__weak__, __visibility__("default")))
66void*
67operator new[](size_t size) throw (std::bad_alloc)
68{
69 return ::operator new(size);
70}
71
72__attribute__((__weak__, __visibility__("default")))
73void*
74operator new[](size_t size, const std::nothrow_t& nothrow) throw()
75{
76 void* p = 0;
77 try
78 {
79 p = ::operator new[](size);
80 }
81 catch (...)
82 {
83 }
84 return p;
85}
86
87__attribute__((__weak__, __visibility__("default")))
88void
89operator delete(void* ptr) throw ()
90{
91 if (ptr)
92 ::free(ptr);
93}
94
95__attribute__((__weak__, __visibility__("default")))
96void
97operator delete(void* ptr, const std::nothrow_t&) throw ()
98{
99 ::operator delete(ptr);
100}
101
102
103__attribute__((__weak__, __visibility__("default")))
104void
105operator delete[] (void* ptr) throw ()
106{
107 ::operator delete (ptr);
108}
109
110__attribute__((__weak__, __visibility__("default")))
111void
112operator delete[] (void* ptr, const std::nothrow_t&) throw ()
113{
114 ::operator delete[](ptr);
115}
116
117
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118namespace std
119{
120
Nick Kledzik76fdaa72010-05-18 22:17:13 +0000121const nothrow_t nothrow = {};
122
123new_handler
124set_new_handler(new_handler handler) throw()
125{
126 new_handler r = __new_handler;
127 __new_handler = handler;
128 return r;
129}
130
Nick Kledzik804b6e72010-05-14 20:19:37 +0000131bad_alloc::bad_alloc() throw()
132{
133}
134
135bad_alloc::~bad_alloc() throw()
136{
137}
138
139const char*
140bad_alloc::what() const throw()
141{
142 return "std::bad_alloc";
143}
144
145
146bad_array_new_length::bad_array_new_length() throw()
147{
148}
149
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150bad_array_new_length::~bad_array_new_length() throw()
151{
152}
153
154const char*
155bad_array_new_length::what() const throw()
156{
157 return "bad_array_new_length";
158}
159
Nick Kledzik804b6e72010-05-14 20:19:37 +0000160
161
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000162void
163__throw_bad_alloc()
164{
165 throw bad_alloc();
166}
167
168} // std