Skip to content

Commit f362be5

Browse files
committed
[libc++][NFC] Reformat new.cpp and stdlib_new_delete.cpp
This makes it a lot easier to make wide ranging changes like I am about to do in https://llvm.org/D150610.
1 parent 3927b9a commit f362be5

File tree

3 files changed

+216
-383
lines changed

3 files changed

+216
-383
lines changed

libcxx/src/new.cpp

Lines changed: 123 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -20,239 +20,149 @@
2020
// in this shared library, so that they can be overridden by programs
2121
// that define non-weak copies of the functions.
2222

23-
_LIBCPP_WEAK
24-
void *
25-
operator new(std::size_t size) _THROW_BAD_ALLOC
26-
{
27-
if (size == 0)
28-
size = 1;
29-
void* p;
30-
while ((p = std::malloc(size)) == nullptr)
31-
{
32-
// If malloc fails and there is a new_handler,
33-
// call it to try free up memory.
34-
std::new_handler nh = std::get_new_handler();
35-
if (nh)
36-
nh();
37-
else
38-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
39-
throw std::bad_alloc();
40-
#else
41-
break;
42-
#endif
23+
_LIBCPP_WEAK void* operator new(std::size_t size) _THROW_BAD_ALLOC {
24+
if (size == 0)
25+
size = 1;
26+
void* p;
27+
while ((p = std::malloc(size)) == nullptr) {
28+
// If malloc fails and there is a new_handler,
29+
// call it to try free up memory.
30+
std::new_handler nh = std::get_new_handler();
31+
if (nh)
32+
nh();
33+
else
34+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
35+
throw std::bad_alloc();
36+
# else
37+
break;
38+
# endif
39+
}
40+
return p;
41+
}
42+
43+
_LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) noexcept {
44+
void* p = nullptr;
45+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
46+
try {
47+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
48+
p = ::operator new(size);
49+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
50+
} catch (...) {
51+
}
52+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
53+
return p;
54+
}
55+
56+
_LIBCPP_WEAK void* operator new[](size_t size) _THROW_BAD_ALLOC { return ::operator new(size); }
57+
58+
_LIBCPP_WEAK void* operator new[](size_t size, const std::nothrow_t&) noexcept {
59+
void* p = nullptr;
60+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
61+
try {
62+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
63+
p = ::operator new[](size);
64+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
65+
} catch (...) {
66+
}
67+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
68+
return p;
69+
}
70+
71+
_LIBCPP_WEAK void operator delete(void* ptr) noexcept { std::free(ptr); }
72+
73+
_LIBCPP_WEAK void operator delete(void* ptr, const std::nothrow_t&) noexcept { ::operator delete(ptr); }
74+
75+
_LIBCPP_WEAK void operator delete(void* ptr, size_t) noexcept { ::operator delete(ptr); }
76+
77+
_LIBCPP_WEAK void operator delete[](void* ptr) noexcept { ::operator delete(ptr); }
78+
79+
_LIBCPP_WEAK void operator delete[](void* ptr, const std::nothrow_t&) noexcept { ::operator delete[](ptr); }
80+
81+
_LIBCPP_WEAK void operator delete[](void* ptr, size_t) noexcept { ::operator delete[](ptr); }
82+
83+
# if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
84+
85+
_LIBCPP_WEAK void* operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
86+
if (size == 0)
87+
size = 1;
88+
if (static_cast<size_t>(alignment) < sizeof(void*))
89+
alignment = std::align_val_t(sizeof(void*));
90+
91+
// Try allocating memory. If allocation fails and there is a new_handler,
92+
// call it to try free up memory, and try again until it succeeds, or until
93+
// the new_handler decides to terminate.
94+
//
95+
// If allocation fails and there is no new_handler, we throw bad_alloc
96+
// (or return nullptr if exceptions are disabled).
97+
void* p;
98+
while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr) {
99+
std::new_handler nh = std::get_new_handler();
100+
if (nh)
101+
nh();
102+
else {
103+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
104+
throw std::bad_alloc();
105+
# else
106+
break;
107+
# endif
43108
}
44-
return p;
109+
}
110+
return p;
45111
}
46112

47-
_LIBCPP_WEAK
48-
void*
49-
operator new(size_t size, const std::nothrow_t&) noexcept
50-
{
51-
void* p = nullptr;
52-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
53-
try
54-
{
55-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
56-
p = ::operator new(size);
57-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
58-
}
59-
catch (...)
60-
{
61-
}
62-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
63-
return p;
64-
}
65-
66-
_LIBCPP_WEAK
67-
void*
68-
operator new[](size_t size) _THROW_BAD_ALLOC
69-
{
70-
return ::operator new(size);
71-
}
72-
73-
_LIBCPP_WEAK
74-
void*
75-
operator new[](size_t size, const std::nothrow_t&) noexcept
76-
{
77-
void* p = nullptr;
78-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
79-
try
80-
{
81-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
82-
p = ::operator new[](size);
83-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
84-
}
85-
catch (...)
86-
{
87-
}
88-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
89-
return p;
90-
}
91-
92-
_LIBCPP_WEAK
93-
void
94-
operator delete(void* ptr) noexcept
95-
{
96-
std::free(ptr);
97-
}
98-
99-
_LIBCPP_WEAK
100-
void
101-
operator delete(void* ptr, const std::nothrow_t&) noexcept
102-
{
103-
::operator delete(ptr);
104-
}
105-
106-
_LIBCPP_WEAK
107-
void
108-
operator delete(void* ptr, size_t) noexcept
109-
{
110-
::operator delete(ptr);
111-
}
112-
113-
_LIBCPP_WEAK
114-
void
115-
operator delete[] (void* ptr) noexcept
116-
{
117-
::operator delete(ptr);
113+
_LIBCPP_WEAK void* operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept {
114+
void* p = nullptr;
115+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
116+
try {
117+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
118+
p = ::operator new(size, alignment);
119+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
120+
} catch (...) {
121+
}
122+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
123+
return p;
118124
}
119125

120-
_LIBCPP_WEAK
121-
void
122-
operator delete[] (void* ptr, const std::nothrow_t&) noexcept
123-
{
124-
::operator delete[](ptr);
126+
_LIBCPP_WEAK void* operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
127+
return ::operator new(size, alignment);
125128
}
126129

127-
_LIBCPP_WEAK
128-
void
129-
operator delete[] (void* ptr, size_t) noexcept
130-
{
131-
::operator delete[](ptr);
130+
_LIBCPP_WEAK void* operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept {
131+
void* p = nullptr;
132+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
133+
try {
134+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
135+
p = ::operator new[](size, alignment);
136+
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
137+
} catch (...) {
138+
}
139+
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
140+
return p;
132141
}
133142

134-
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
135-
136-
_LIBCPP_WEAK
137-
void *
138-
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
139-
{
140-
if (size == 0)
141-
size = 1;
142-
if (static_cast<size_t>(alignment) < sizeof(void*))
143-
alignment = std::align_val_t(sizeof(void*));
144-
145-
// Try allocating memory. If allocation fails and there is a new_handler,
146-
// call it to try free up memory, and try again until it succeeds, or until
147-
// the new_handler decides to terminate.
148-
//
149-
// If allocation fails and there is no new_handler, we throw bad_alloc
150-
// (or return nullptr if exceptions are disabled).
151-
void* p;
152-
while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr)
153-
{
154-
std::new_handler nh = std::get_new_handler();
155-
if (nh)
156-
nh();
157-
else {
158-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
159-
throw std::bad_alloc();
160-
#else
161-
break;
162-
#endif
163-
}
164-
}
165-
return p;
166-
}
167-
168-
_LIBCPP_WEAK
169-
void*
170-
operator new(size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
171-
{
172-
void* p = nullptr;
173-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
174-
try
175-
{
176-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
177-
p = ::operator new(size, alignment);
178-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
179-
}
180-
catch (...)
181-
{
182-
}
183-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
184-
return p;
185-
}
186-
187-
_LIBCPP_WEAK
188-
void*
189-
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
190-
{
191-
return ::operator new(size, alignment);
192-
}
193-
194-
_LIBCPP_WEAK
195-
void*
196-
operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept
197-
{
198-
void* p = nullptr;
199-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
200-
try
201-
{
202-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
203-
p = ::operator new[](size, alignment);
204-
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
205-
}
206-
catch (...)
207-
{
208-
}
209-
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
210-
return p;
211-
}
212-
213-
_LIBCPP_WEAK
214-
void
215-
operator delete(void* ptr, std::align_val_t) noexcept
216-
{
217-
std::__libcpp_aligned_free(ptr);
218-
}
143+
_LIBCPP_WEAK void operator delete(void* ptr, std::align_val_t) noexcept { std::__libcpp_aligned_free(ptr); }
219144

220-
_LIBCPP_WEAK
221-
void
222-
operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
223-
{
224-
::operator delete(ptr, alignment);
145+
_LIBCPP_WEAK void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept {
146+
::operator delete(ptr, alignment);
225147
}
226148

227-
_LIBCPP_WEAK
228-
void
229-
operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept
230-
{
231-
::operator delete(ptr, alignment);
149+
_LIBCPP_WEAK void operator delete(void* ptr, size_t, std::align_val_t alignment) noexcept {
150+
::operator delete(ptr, alignment);
232151
}
233152

234-
_LIBCPP_WEAK
235-
void
236-
operator delete[] (void* ptr, std::align_val_t alignment) noexcept
237-
{
238-
::operator delete(ptr, alignment);
153+
_LIBCPP_WEAK void operator delete[](void* ptr, std::align_val_t alignment) noexcept {
154+
::operator delete(ptr, alignment);
239155
}
240156

241-
_LIBCPP_WEAK
242-
void
243-
operator delete[] (void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept
244-
{
245-
::operator delete[](ptr, alignment);
157+
_LIBCPP_WEAK void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept {
158+
::operator delete[](ptr, alignment);
246159
}
247160

248-
_LIBCPP_WEAK
249-
void
250-
operator delete[] (void* ptr, size_t, std::align_val_t alignment) noexcept
251-
{
252-
::operator delete[](ptr, alignment);
161+
_LIBCPP_WEAK void operator delete[](void* ptr, size_t, std::align_val_t alignment) noexcept {
162+
::operator delete[](ptr, alignment);
253163
}
254164

255-
#endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
165+
# endif // !_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
256166
// ------------------ END COPY ------------------
257167

258168
#endif // !__GLIBCXX__ && !_LIBCPP_ABI_VCRUNTIME

libcxx/utils/data/ignore_format.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,6 @@ libcxx/src/locale.cpp
525525
libcxx/src/memory.cpp
526526
libcxx/src/mutex.cpp
527527
libcxx/src/mutex_destructor.cpp
528-
libcxx/src/new.cpp
529528
libcxx/src/optional.cpp
530529
libcxx/src/pstl/libdispatch.cpp
531530
libcxx/src/random.cpp

0 commit comments

Comments
 (0)