Skip to content

Commit 8ee3a56

Browse files
committed
Move <new> include to the implementation
1 parent 25df8cc commit 8ee3a56

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

include/fmt/format-inl.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# include <climits>
1515
# include <cmath>
1616
# include <exception>
17+
# include <new> // std::bad_alloc
1718
#endif
1819

1920
#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE)
@@ -78,11 +79,16 @@ template <typename Locale> auto locale_ref::get() const -> Locale {
7879

7980
namespace detail {
8081

82+
FMT_FUNC auto allocate(size_t size) -> void* {
83+
void* p = malloc(size);
84+
if (!p) FMT_THROW(std::bad_alloc());
85+
return p;
86+
}
87+
8188
FMT_FUNC void format_error_code(detail::buffer<char>& out, int error_code,
8289
string_view message) noexcept {
83-
// Report error code making sure that the output fits into
84-
// inline_buffer_size to avoid dynamic memory allocation and potential
85-
// bad_alloc.
90+
// Report error code making sure that the output fits into inline_buffer_size
91+
// to avoid dynamic memory allocation and potential bad_alloc.
8692
out.try_resize(0);
8793
static const char SEP[] = ": ";
8894
static const char ERROR_STR[] = "error ";

include/fmt/format.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
# include <cstddef> // std::byte
5555
# include <cstdint> // uint32_t
5656
# include <limits> // std::numeric_limits
57-
# include <new> // std::bad_alloc
5857
# if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
5958
// Workaround for pre gcc 5 libstdc++.
6059
# include <memory> // std::allocator_traits
@@ -736,6 +735,8 @@ using fast_float_t = conditional_t<sizeof(T) == sizeof(double), double, float>;
736735
template <typename T>
737736
using is_double_double = bool_constant<std::numeric_limits<T>::digits == 106>;
738737

738+
FMT_API auto allocate(size_t size) -> void*;
739+
739740
// An allocator that uses malloc/free to allow removing dependency on the C++
740741
// standard library runtime. std::decay is used for back_inserter to be found by
741742
// ADL when applied to memory_buffer.
@@ -744,9 +745,7 @@ template <typename T> struct allocator : private std::decay<void> {
744745

745746
auto allocate(size_t n) -> T* {
746747
FMT_ASSERT(n <= max_value<size_t>() / sizeof(T), "");
747-
T* p = static_cast<T*>(malloc(n * sizeof(T)));
748-
if (!p) FMT_THROW(std::bad_alloc());
749-
return p;
748+
return static_cast<T*>(detail::allocate(n * sizeof(T)));
750749
}
751750

752751
void deallocate(T* p, size_t) { free(p); }

0 commit comments

Comments
 (0)