6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
- #include " include/overridable_function.h"
10
- #include < __assert>
11
9
#include < __memory/aligned_alloc.h>
12
- #include < cstddef>
13
10
#include < cstdlib>
14
11
#include < new>
15
12
18
15
// The code below is copied as-is into libc++abi's libcxxabi/src/stdlib_new_delete.cpp
19
16
// file. The version in this file is the canonical one.
20
17
21
- inline void __throw_bad_alloc_shim () { std::__throw_bad_alloc (); }
22
-
23
- # define _LIBCPP_ASSERT_SHIM (expr, str ) _LIBCPP_ASSERT(expr, str)
24
-
25
18
// ------------------ BEGIN COPY ------------------
26
19
// Implement all new and delete operators as weak definitions
27
20
// in this shared library, so that they can be overridden by programs
@@ -43,63 +36,41 @@ static void* operator_new_impl(std::size_t size) {
43
36
return p;
44
37
}
45
38
46
- _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void * operator new (std::size_t size) _THROW_BAD_ALLOC {
39
+ _LIBCPP_WEAK void * operator new (std::size_t size) _THROW_BAD_ALLOC {
47
40
void * p = operator_new_impl (size);
41
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
48
42
if (p == nullptr )
49
- __throw_bad_alloc_shim ();
43
+ throw std::bad_alloc ();
44
+ # endif
50
45
return p;
51
46
}
52
47
53
48
_LIBCPP_WEAK void * operator new (size_t size, const std::nothrow_t &) noexcept {
54
- # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
55
- # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
56
- _LIBCPP_ASSERT_SHIM (
57
- !std::__is_function_overridden (static_cast <void * (*)(std::size_t )>(&operator new )),
58
- " libc++ was configured with exceptions disabled and `operator new(size_t)` has been overridden, "
59
- " but `operator new(size_t, nothrow_t)` has not been overridden. This is problematic because "
60
- " `operator new(size_t, nothrow_t)` must call `operator new(size_t)`, which will terminate in case "
61
- " it fails to allocate, making it impossible for `operator new(size_t, nothrow_t)` to fulfill its "
62
- " contract (since it should return nullptr upon failure). Please make sure you override "
63
- " `operator new(size_t, nothrow_t)` as well." );
64
- # endif
65
-
66
- return operator_new_impl (size);
67
- # else
68
49
void * p = nullptr ;
50
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
69
51
try {
52
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
70
53
p = ::operator new (size);
54
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
71
55
} catch (...) {
72
56
}
57
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
73
58
return p;
74
- # endif
75
59
}
76
60
77
- _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void * operator new [](size_t size) _THROW_BAD_ALLOC {
78
- return ::operator new (size);
79
- }
61
+ _LIBCPP_WEAK void * operator new [](size_t size) _THROW_BAD_ALLOC { return ::operator new (size); }
80
62
81
63
_LIBCPP_WEAK void * operator new [](size_t size, const std::nothrow_t &) noexcept {
82
- # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
83
- # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
84
- _LIBCPP_ASSERT_SHIM (
85
- !std::__is_function_overridden (static_cast <void * (*)(std::size_t )>(&operator new [])),
86
- " libc++ was configured with exceptions disabled and `operator new[](size_t)` has been overridden, "
87
- " but `operator new[](size_t, nothrow_t)` has not been overridden. This is problematic because "
88
- " `operator new[](size_t, nothrow_t)` must call `operator new[](size_t)`, which will terminate in case "
89
- " it fails to allocate, making it impossible for `operator new[](size_t, nothrow_t)` to fulfill its "
90
- " contract (since it should return nullptr upon failure). Please make sure you override "
91
- " `operator new[](size_t, nothrow_t)` as well." );
92
- # endif
93
-
94
- return operator_new_impl (size);
95
- # else
96
64
void * p = nullptr ;
65
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
97
66
try {
67
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
98
68
p = ::operator new [](size);
69
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
99
70
} catch (...) {
100
71
}
72
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
101
73
return p;
102
- # endif
103
74
}
104
75
105
76
_LIBCPP_WEAK void operator delete (void * ptr) noexcept { std::free (ptr); }
@@ -136,66 +107,43 @@ static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignm
136
107
return p;
137
108
}
138
109
139
- _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void *
140
- operator new (std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
110
+ _LIBCPP_WEAK void * operator new (std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
141
111
void * p = operator_new_aligned_impl (size, alignment);
112
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
142
113
if (p == nullptr )
143
- __throw_bad_alloc_shim ();
114
+ throw std::bad_alloc ();
115
+ # endif
144
116
return p;
145
117
}
146
118
147
119
_LIBCPP_WEAK void * operator new (size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept {
148
- # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
149
- # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
150
- _LIBCPP_ASSERT_SHIM (
151
- !std::__is_function_overridden (static_cast <void * (*)(std::size_t , std::align_val_t )>(&operator new )),
152
- " libc++ was configured with exceptions disabled and `operator new(size_t, align_val_t)` has been overridden, "
153
- " but `operator new(size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "
154
- " `operator new(size_t, align_val_t, nothrow_t)` must call `operator new(size_t, align_val_t)`, which will "
155
- " terminate in case it fails to allocate, making it impossible for `operator new(size_t, align_val_t, nothrow_t)` "
156
- " to fulfill its contract (since it should return nullptr upon failure). Please make sure you override "
157
- " `operator new(size_t, align_val_t, nothrow_t)` as well." );
158
- # endif
159
-
160
- return operator_new_aligned_impl (size, alignment);
161
- # else
162
120
void * p = nullptr ;
121
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
163
122
try {
123
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
164
124
p = ::operator new (size, alignment);
125
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
165
126
} catch (...) {
166
127
}
128
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
167
129
return p;
168
- # endif
169
130
}
170
131
171
- _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void *
172
- operator new [](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
132
+ _LIBCPP_WEAK void * operator new [](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
173
133
return ::operator new (size, alignment);
174
134
}
175
135
176
136
_LIBCPP_WEAK void * operator new [](size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept {
177
- # ifdef _LIBCPP_HAS_NO_EXCEPTIONS
178
- # if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
179
- _LIBCPP_ASSERT_SHIM (
180
- !std::__is_function_overridden (static_cast <void * (*)(std::size_t , std::align_val_t )>(&operator new [])),
181
- " libc++ was configured with exceptions disabled and `operator new[](size_t, align_val_t)` has been overridden, "
182
- " but `operator new[](size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "
183
- " `operator new[](size_t, align_val_t, nothrow_t)` must call `operator new[](size_t, align_val_t)`, which will "
184
- " terminate in case it fails to allocate, making it impossible for `operator new[](size_t, align_val_t, "
185
- " nothrow_t)` to fulfill its contract (since it should return nullptr upon failure). Please make sure you "
186
- " override "
187
- " `operator new[](size_t, align_val_t, nothrow_t)` as well." );
188
- # endif
189
-
190
- return operator_new_aligned_impl (size, alignment);
191
- # else
192
137
void * p = nullptr ;
138
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
193
139
try {
140
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
194
141
p = ::operator new [](size, alignment);
142
+ # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
195
143
} catch (...) {
196
144
}
145
+ # endif // _LIBCPP_HAS_NO_EXCEPTIONS
197
146
return p;
198
- # endif
199
147
}
200
148
201
149
_LIBCPP_WEAK void operator delete (void * ptr, std::align_val_t ) noexcept { std::__libcpp_aligned_free (ptr); }
0 commit comments