[libcxx-commits] [libcxxabi] [libc++][NFC] Refactor the core logic of operator new into helper functions (PR #69407)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 17 19:26:24 PDT 2023
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff beffc821e8290136a1d8b359cc83487c359b48ca 57827e7b8aad619a8cd773c6e9e2741d3e7bebd2 -- libcxxabi/src/stdlib_new_delete.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libcxxabi/src/stdlib_new_delete.cpp b/libcxxabi/src/stdlib_new_delete.cpp
index 7946d07a59b7..9882fb4fa82f 100644
--- a/libcxxabi/src/stdlib_new_delete.cpp
+++ b/libcxxabi/src/stdlib_new_delete.cpp
@@ -31,31 +31,29 @@
// that define non-weak copies of the functions.
static void* operator_new_impl(std::size_t size) noexcept {
- if (size == 0)
- size = 1;
- void* p;
- while ((p = std::malloc(size)) == nullptr) {
- // If malloc fails and there is a new_handler,
- // call it to try free up memory.
- std::new_handler nh = std::get_new_handler();
- if (nh)
- nh();
- else
- break;
- }
- return p;
+ if (size == 0)
+ size = 1;
+ void* p;
+ while ((p = std::malloc(size)) == nullptr) {
+ // If malloc fails and there is a new_handler,
+ // call it to try free up memory.
+ std::new_handler nh = std::get_new_handler();
+ if (nh)
+ nh();
+ else
+ break;
+ }
+ return p;
}
_LIBCPP_WEAK
-void *
-operator new(std::size_t size) _THROW_BAD_ALLOC
-{
- void* p = operator_new_impl(size);
+void* operator new(std::size_t size) _THROW_BAD_ALLOC {
+ void* p = operator_new_impl(size);
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- if (p == nullptr)
- throw std::bad_alloc();
+ if (p == nullptr)
+ throw std::bad_alloc();
#endif
- return p;
+ return p;
}
_LIBCPP_WEAK
@@ -148,35 +146,33 @@ operator delete[] (void* ptr, size_t) noexcept
#if !defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignment) noexcept {
- if (size == 0)
- size = 1;
- if (static_cast<size_t>(alignment) < sizeof(void*))
- alignment = std::align_val_t(sizeof(void*));
+ if (size == 0)
+ size = 1;
+ if (static_cast<size_t>(alignment) < sizeof(void*))
+ alignment = std::align_val_t(sizeof(void*));
- // Try allocating memory. If allocation fails and there is a new_handler,
- // call it to try free up memory, and try again until it succeeds, or until
- // the new_handler decides to terminate.
- void* p;
- while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr) {
- std::new_handler nh = std::get_new_handler();
- if (nh)
- nh();
- else
- break;
- }
- return p;
+ // Try allocating memory. If allocation fails and there is a new_handler,
+ // call it to try free up memory, and try again until it succeeds, or until
+ // the new_handler decides to terminate.
+ void* p;
+ while ((p = std::__libcpp_aligned_alloc(static_cast<std::size_t>(alignment), size)) == nullptr) {
+ std::new_handler nh = std::get_new_handler();
+ if (nh)
+ nh();
+ else
+ break;
+ }
+ return p;
}
_LIBCPP_WEAK
-void *
-operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC
-{
- void* p = operator_new_aligned_impl(size, alignment);
-#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
- if (p == nullptr)
- throw std::bad_alloc();
-#endif
- return p;
+void* operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
+ void* p = operator_new_aligned_impl(size, alignment);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ if (p == nullptr)
+ throw std::bad_alloc();
+# endif
+ return p;
}
_LIBCPP_WEAK
``````````
</details>
https://github.com/llvm/llvm-project/pull/69407
More information about the libcxx-commits
mailing list