~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to system/lib/libcxxabi/src/cxa_new_delete.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===------------------------ cxa_new_delete.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
// This file implements the new and delete operators.
 
10
//===----------------------------------------------------------------------===//
 
11
 
 
12
#include <new>
 
13
#include <cstdlib>
 
14
 
 
15
/*
 
16
[new.delete.single]
 
17
 
 
18
* Executes a loop: Within the loop, the function first attempts to allocate
 
19
  the requested storage. Whether the attempt involves a call to the Standard C
 
20
  library function malloc is unspecified.
 
21
 
 
22
* Returns a pointer to the allocated storage if the attempt is successful.
 
23
  Otherwise, if the current new_handler (18.6.2.5) is a null pointer value,
 
24
  throws bad_alloc.
 
25
 
 
26
* Otherwise, the function calls the current new_handler function (18.6.2.3).
 
27
  If the called function returns, the loop repeats.
 
28
 
 
29
* The loop terminates when an attempt to allocate the requested storage is
 
30
  successful or when a called new_handler function does not return.
 
31
*/
 
32
__attribute__((__weak__, __visibility__("default")))
 
33
void *
 
34
operator new(std::size_t size)
 
35
#if !__has_feature(cxx_noexcept)
 
36
    throw(std::bad_alloc)
 
37
#endif
 
38
{
 
39
    if (size == 0)
 
40
        size = 1;
 
41
    void* p;
 
42
    while ((p = std::malloc(size)) == 0)
 
43
    {
 
44
        std::new_handler nh = std::get_new_handler();
 
45
        if (nh)
 
46
            nh();
 
47
        else
 
48
            throw std::bad_alloc();
 
49
    }
 
50
    return p;
 
51
}
 
52
 
 
53
/*
 
54
Note:  The relationships among these operators is both carefully considered
 
55
and standard in C++11.  Please do not change them without fully understanding
 
56
the consequences of doing so.  Reference:
 
57
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2158.html
 
58
*/
 
59
/*
 
60
[new.delete.single]
 
61
 
 
62
Calls operator new(size). If the call returns normally, returns the result of
 
63
that call. Otherwise, returns a null pointer.
 
64
*/
 
65
__attribute__((__weak__, __visibility__("default")))
 
66
void*
 
67
operator new(size_t size, const std::nothrow_t&)
 
68
#if __has_feature(cxx_noexcept)
 
69
    noexcept
 
70
#else
 
71
    throw()
 
72
#endif
 
73
{
 
74
    void* p = 0;
 
75
    try
 
76
    {
 
77
        p = ::operator new(size);
 
78
    }
 
79
    catch (...)
 
80
    {
 
81
    }
 
82
    return p;
 
83
}
 
84
 
 
85
/*
 
86
[new.delete.array]
 
87
 
 
88
Returns operator new(size).
 
89
*/
 
90
__attribute__((__weak__, __visibility__("default")))
 
91
void*
 
92
operator new[](size_t size)
 
93
#if !__has_feature(cxx_noexcept)
 
94
    throw(std::bad_alloc)
 
95
#endif
 
96
{
 
97
    return ::operator new(size);
 
98
}
 
99
 
 
100
/*
 
101
[new.delete.array]
 
102
 
 
103
Calls operator new[](size). If the call returns normally, returns the result
 
104
of that call. Otherwise, returns a null pointer.
 
105
*/
 
106
__attribute__((__weak__, __visibility__("default")))
 
107
void*
 
108
operator new[](size_t size, const std::nothrow_t&)
 
109
#if __has_feature(cxx_noexcept)
 
110
    noexcept
 
111
#else
 
112
    throw()
 
113
#endif
 
114
{
 
115
    void* p = 0;
 
116
    try
 
117
    {
 
118
        p = ::operator new[](size);
 
119
    }
 
120
    catch (...)
 
121
    {
 
122
    }
 
123
    return p;
 
124
}
 
125
 
 
126
/*
 
127
[new.delete.single]
 
128
 
 
129
If ptr is null, does nothing. Otherwise, reclaims the storage allocated by the
 
130
earlier call to operator new.
 
131
*/
 
132
__attribute__((__weak__, __visibility__("default")))
 
133
void
 
134
operator delete(void* ptr)
 
135
#if __has_feature(cxx_noexcept)
 
136
    noexcept
 
137
#else
 
138
    throw()
 
139
#endif
 
140
{
 
141
    if (ptr)
 
142
        std::free(ptr);
 
143
}
 
144
 
 
145
/*
 
146
[new.delete.single]
 
147
 
 
148
calls operator delete(ptr)
 
149
*/
 
150
__attribute__((__weak__, __visibility__("default")))
 
151
void
 
152
operator delete(void* ptr, const std::nothrow_t&)
 
153
#if __has_feature(cxx_noexcept)
 
154
    noexcept
 
155
#else
 
156
    throw()
 
157
#endif
 
158
{
 
159
    ::operator delete(ptr);
 
160
}
 
161
 
 
162
/*
 
163
[new.delete.array]
 
164
 
 
165
Calls operator delete(ptr)
 
166
*/
 
167
__attribute__((__weak__, __visibility__("default")))
 
168
void
 
169
operator delete[] (void* ptr)
 
170
#if __has_feature(cxx_noexcept)
 
171
    noexcept
 
172
#else
 
173
    throw()
 
174
#endif
 
175
{
 
176
    ::operator delete(ptr);
 
177
}
 
178
 
 
179
/*
 
180
[new.delete.array]
 
181
 
 
182
calls operator delete[](ptr)
 
183
*/
 
184
__attribute__((__weak__, __visibility__("default")))
 
185
void
 
186
operator delete[] (void* ptr, const std::nothrow_t&)
 
187
#if __has_feature(cxx_noexcept)
 
188
    noexcept
 
189
#else
 
190
    throw()
 
191
#endif
 
192
{
 
193
    ::operator delete[](ptr);
 
194
}
 
195
 
 
196
namespace std
 
197
{
 
198
 
 
199
//  bad_alloc
 
200
 
 
201
bad_alloc::bad_alloc() _NOEXCEPT
 
202
{
 
203
}
 
204
 
 
205
bad_alloc::~bad_alloc() _NOEXCEPT
 
206
{
 
207
}
 
208
 
 
209
const char*
 
210
bad_alloc::what() const _NOEXCEPT
 
211
{
 
212
    return "std::bad_alloc";
 
213
}
 
214
 
 
215
// bad_array_new_length
 
216
 
 
217
bad_array_new_length::bad_array_new_length() _NOEXCEPT
 
218
{
 
219
}
 
220
 
 
221
bad_array_new_length::~bad_array_new_length() _NOEXCEPT
 
222
{
 
223
}
 
224
 
 
225
const char*
 
226
bad_array_new_length::what() const _NOEXCEPT
 
227
{
 
228
    return "bad_array_new_length";
 
229
}
 
230
 
 
231
#ifdef EMSCRIPTEN
 
232
// We don't build the new.cpp from libcxx, so we need to define this.
 
233
void
 
234
__throw_bad_alloc()
 
235
{
 
236
#ifndef _LIBCPP_NO_EXCEPTIONS
 
237
    throw bad_alloc();
 
238
#endif
 
239
}
 
240
#endif
 
241
 
 
242
}  // std