diff --git a/include/fmt/format.h b/include/fmt/format.h index adad650c..302e5193 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -46,6 +46,7 @@ # include // std::memcpy # include // std::initializer_list # include // std::numeric_limits +# include // std::bad_alloc # if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI) // Workaround for pre gcc 5 libstdc++. # include // std::allocator_traits @@ -789,6 +790,22 @@ template struct is_locale : std::false_type {}; template struct is_locale> : std::true_type {}; + +// An allocator that uses malloc/free to allow removing dependency on the C++ +// standard libary runtime. +template struct allocator { + using value_type = T; + + T* allocate(size_t n) { + FMT_ASSERT(n <= max_value() / sizeof(T), ""); + T* p = static_cast(malloc(n * sizeof(T))); + if (!p) FMT_THROW(std::bad_alloc()); + return p; + } + + void deallocate(T* p, size_t) { free(p); } +}; + } // namespace detail FMT_BEGIN_EXPORT @@ -811,7 +828,7 @@ enum { inline_buffer_size = 500 }; * converted to `std::string` with `to_string(out)`. */ template > + typename Allocator = detail::allocator> class basic_memory_buffer : public detail::buffer { private: T store_[SIZE];