Move <new> include to the implementation

This commit is contained in:
Victor Zverovich 2026-01-25 18:01:56 -08:00
parent 25df8ccd96
commit 8ee3a56d12
2 changed files with 12 additions and 7 deletions

View File

@ -14,6 +14,7 @@
# include <climits>
# include <cmath>
# include <exception>
# include <new> // std::bad_alloc
#endif
#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE)
@ -78,11 +79,16 @@ template <typename Locale> auto locale_ref::get() const -> Locale {
namespace detail {
FMT_FUNC auto allocate(size_t size) -> void* {
void* p = malloc(size);
if (!p) FMT_THROW(std::bad_alloc());
return p;
}
FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
string_view message) noexcept {
// Report error code making sure that the output fits into
// inline_buffer_size to avoid dynamic memory allocation and potential
// bad_alloc.
// Report error code making sure that the output fits into inline_buffer_size
// to avoid dynamic memory allocation and potential bad_alloc.
out.try_resize(0);
static const char SEP[] = ": ";
static const char ERROR_STR[] = "error ";

View File

@ -54,7 +54,6 @@
# include <cstddef> // std::byte
# include <cstdint> // uint32_t
# include <limits> // std::numeric_limits
# include <new> // std::bad_alloc
# if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
// Workaround for pre gcc 5 libstdc++.
# include <memory> // std::allocator_traits
@ -736,6 +735,8 @@ using fast_float_t = conditional_t<sizeof(T) == sizeof(double), double, float>;
template <typename T>
using is_double_double = bool_constant<std::numeric_limits<T>::digits == 106>;
FMT_API auto allocate(size_t size) -> void*;
// An allocator that uses malloc/free to allow removing dependency on the C++
// standard library runtime. std::decay is used for back_inserter to be found by
// ADL when applied to memory_buffer.
@ -744,9 +745,7 @@ template <typename T> struct allocator : private std::decay<void> {
auto allocate(size_t n) -> T* {
FMT_ASSERT(n <= max_value<size_t>() / sizeof(T), "");
T* p = static_cast<T*>(malloc(n * sizeof(T)));
if (!p) FMT_THROW(std::bad_alloc());
return p;
return static_cast<T*>(detail::allocate(n * sizeof(T)));
}
void deallocate(T* p, size_t) { free(p); }