Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/c++/src/new.cpp
12346 views
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
#include "include/atomic_support.h"
14
15
#if defined(_LIBCPP_ABI_MICROSOFT)
16
#if defined(_LIBCPP_NO_VCRUNTIME)
17
#include "support/runtime/new_handler_fallback.ipp"
18
#endif
19
#elif defined(LIBCXX_BUILDING_LIBCXXABI)
20
#include <cxxabi.h>
21
#elif defined(LIBCXXRT)
22
#include <cxxabi.h>
23
#include "support/runtime/new_handler_fallback.ipp"
24
#elif defined(__GLIBCXX__)
25
// nothing todo
26
#else
27
# if defined(__APPLE__) && !defined(_LIBCPP_BUILDING_HAS_NO_ABI_LIBRARY)
28
# include <cxxabi.h> // FIXME: remove this once buildit is gone.
29
# else
30
# include "support/runtime/new_handler_fallback.ipp"
31
# endif
32
#endif
33
34
namespace std
35
{
36
37
#ifndef __GLIBCXX__
38
const nothrow_t nothrow = {};
39
#endif
40
41
#ifndef LIBSTDCXX
42
43
void
44
__throw_bad_alloc()
45
{
46
#ifndef _LIBCPP_NO_EXCEPTIONS
47
throw bad_alloc();
48
#else
49
_VSTD::abort();
50
#endif
51
}
52
53
#endif // !LIBSTDCXX
54
55
} // std
56
57
#if !defined(__GLIBCXX__) && \
58
!defined(_LIBCPP_DEFER_NEW_TO_VCRUNTIME) && \
59
!defined(_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
60
61
// Implement all new and delete operators as weak definitions
62
// in this shared library, so that they can be overridden by programs
63
// that define non-weak copies of the functions.
64
65
_LIBCPP_WEAK
66
void *
67
operator new(std::size_t size) _THROW_BAD_ALLOC
68
{
69
if (size == 0)
70
size = 1;
71
void* p;
72
while ((p = ::malloc(size)) == 0)
73
{
74
// If malloc fails and there is a new_handler,
75
// call it to try free up memory.
76
std::new_handler nh = std::get_new_handler();
77
if (nh)
78
nh();
79
else
80
#ifndef _LIBCPP_NO_EXCEPTIONS
81
throw std::bad_alloc();
82
#else
83
break;
84
#endif
85
}
86
return p;
87
}
88
89
_LIBCPP_WEAK
90
void*
91
operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
92
{
93
void* p = 0;
94
#ifndef _LIBCPP_NO_EXCEPTIONS
95
try
96
{
97
#endif // _LIBCPP_NO_EXCEPTIONS
98
p = ::operator new(size);
99
#ifndef _LIBCPP_NO_EXCEPTIONS
100
}
101
catch (...)
102
{
103
}
104
#endif // _LIBCPP_NO_EXCEPTIONS
105
return p;
106
}
107
108
_LIBCPP_WEAK
109
void*
110
operator new[](size_t size) _THROW_BAD_ALLOC
111
{
112
return ::operator new(size);
113
}
114
115
_LIBCPP_WEAK
116
void*
117
operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
118
{
119
void* p = 0;
120
#ifndef _LIBCPP_NO_EXCEPTIONS
121
try
122
{
123
#endif // _LIBCPP_NO_EXCEPTIONS
124
p = ::operator new[](size);
125
#ifndef _LIBCPP_NO_EXCEPTIONS
126
}
127
catch (...)
128
{
129
}
130
#endif // _LIBCPP_NO_EXCEPTIONS
131
return p;
132
}
133
134
_LIBCPP_WEAK
135
void
136
operator delete(void* ptr) _NOEXCEPT
137
{
138
::free(ptr);
139
}
140
141
_LIBCPP_WEAK
142
void
143
operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
144
{
145
::operator delete(ptr);
146
}
147
148
_LIBCPP_WEAK
149
void
150
operator delete(void* ptr, size_t) _NOEXCEPT
151
{
152
::operator delete(ptr);
153
}
154
155
_LIBCPP_WEAK
156
void
157
operator delete[] (void* ptr) _NOEXCEPT
158
{
159
::operator delete(ptr);
160
}
161
162
_LIBCPP_WEAK
163
void
164
operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
165
{
166
::operator delete[](ptr);
167
}
168
169
_LIBCPP_WEAK
170
void
171
operator delete[] (void* ptr, size_t) _NOEXCEPT
172
{
173
::operator delete[](ptr);
174
}
175
176
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
177
178
_LIBCPP_WEAK
179
void *
180
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
181
{
182
if (size == 0)
183
size = 1;
184
if (static_cast<size_t>(alignment) < sizeof(void*))
185
alignment = std::align_val_t(sizeof(void*));
186
void* p;
187
#if defined(_LIBCPP_MSVCRT_LIKE)
188
while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
189
#else
190
while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
191
#endif
192
{
193
// If posix_memalign fails and there is a new_handler,
194
// call it to try free up memory.
195
std::new_handler nh = std::get_new_handler();
196
if (nh)
197
nh();
198
else {
199
#ifndef _LIBCPP_NO_EXCEPTIONS
200
throw std::bad_alloc();
201
#else
202
p = nullptr; // posix_memalign doesn't initialize 'p' on failure
203
break;
204
#endif
205
}
206
}
207
return p;
208
}
209
210
_LIBCPP_WEAK
211
void*
212
operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
213
{
214
void* p = 0;
215
#ifndef _LIBCPP_NO_EXCEPTIONS
216
try
217
{
218
#endif // _LIBCPP_NO_EXCEPTIONS
219
p = ::operator new(size, alignment);
220
#ifndef _LIBCPP_NO_EXCEPTIONS
221
}
222
catch (...)
223
{
224
}
225
#endif // _LIBCPP_NO_EXCEPTIONS
226
return p;
227
}
228
229
_LIBCPP_WEAK
230
void*
231
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
232
{
233
return ::operator new(size, alignment);
234
}
235
236
_LIBCPP_WEAK
237
void*
238
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
239
{
240
void* p = 0;
241
#ifndef _LIBCPP_NO_EXCEPTIONS
242
try
243
{
244
#endif // _LIBCPP_NO_EXCEPTIONS
245
p = ::operator new[](size, alignment);
246
#ifndef _LIBCPP_NO_EXCEPTIONS
247
}
248
catch (...)
249
{
250
}
251
#endif // _LIBCPP_NO_EXCEPTIONS
252
return p;
253
}
254
255
_LIBCPP_WEAK
256
void
257
operator delete(void* ptr, std::align_val_t) _NOEXCEPT
258
{
259
#if defined(_LIBCPP_MSVCRT_LIKE)
260
::_aligned_free(ptr);
261
#else
262
::free(ptr);
263
#endif
264
}
265
266
_LIBCPP_WEAK
267
void
268
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
269
{
270
::operator delete(ptr, alignment);
271
}
272
273
_LIBCPP_WEAK
274
void
275
operator delete(void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
276
{
277
::operator delete(ptr, alignment);
278
}
279
280
_LIBCPP_WEAK
281
void
282
operator delete[] (void* ptr, std::align_val_t alignment) _NOEXCEPT
283
{
284
::operator delete(ptr, alignment);
285
}
286
287
_LIBCPP_WEAK
288
void
289
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) _NOEXCEPT
290
{
291
::operator delete[](ptr, alignment);
292
}
293
294
_LIBCPP_WEAK
295
void
296
operator delete[] (void* ptr, size_t, std::align_val_t alignment) _NOEXCEPT
297
{
298
::operator delete[](ptr, alignment);
299
}
300
301
#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
302
#endif // !__GLIBCXX__ && (!_LIBCPP_ABI_MICROSOFT || _LIBCPP_NO_VCRUNTIME) && !_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
303
304