Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::allocator_traits #3804

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@
#include <cstring> // std::memcpy
#include <initializer_list> // std::initializer_list
#include <limits> // std::numeric_limits
#include <stdexcept> // std::runtime_error
#include <string> // std::string
#include <system_error> // std::system_error
#if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe __GLIBCXX__ < 20150422 instead of _GLIBCXX_USE_DUAL_ABI? https://stackoverflow.com/questions/37118114/value-of-glibcxx-for-each-libstdc-release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing dates won't work:
#2349 (comment)

// Workaround for pre gcc 5 libstdc++.
# include <memory> // std::allocator_traits
#endif
#include <stdexcept> // std::runtime_error
#include <string> // std::string
#include <system_error> // std::system_error

#include "base.h"

Expand Down Expand Up @@ -508,14 +512,6 @@ FMT_INLINE void assume(bool condition) {
#endif
}

template <typename Allocator, typename Enable = void> struct allocator_size {
using type = size_t;
};
template <typename Allocator>
struct allocator_size<Allocator, void_t<typename Allocator::size_type>> {
using type = typename Allocator::size_type;
};

template <typename Char, typename InputIt>
auto copy_str(InputIt begin, InputIt end, appender out) -> appender {
get_container(out).append(begin, end);
Expand Down Expand Up @@ -914,9 +910,8 @@ class basic_memory_buffer : public detail::buffer<T> {
static FMT_CONSTEXPR20 void grow(detail::buffer<T>& buf, size_t size) {
detail::abort_fuzzing_if(size > 5000);
auto& self = static_cast<basic_memory_buffer&>(buf);
constexpr size_t max_size =
detail::max_value<typename detail::allocator_size<Allocator>::type>() /
sizeof(T);
const size_t max_size =
std::allocator_traits<Allocator>::max_size(self.alloc_);
size_t old_capacity = buf.capacity();
size_t new_capacity = old_capacity + old_capacity / 2;
if (size > new_capacity)
Expand Down
35 changes: 21 additions & 14 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,30 +413,37 @@ TEST(memory_buffer_test, exception_in_deallocate) {
EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size));
}

class smol_allocator : public std::allocator<char> {
template <typename Allocator, size_t MaxSize>
class max_size_allocator : public Allocator {
public:
using size_type = unsigned char;

auto allocate(size_t n) -> value_type* {
if (n > fmt::detail::max_value<size_type>())
using typename Allocator::value_type;
size_t max_size() const noexcept { return MaxSize; }
value_type* allocate(size_t n) {
if (n > max_size()) {
throw std::length_error("size > max_size");
return std::allocator<char>::allocate(n);
}
return std::allocator_traits<Allocator>::allocate(
*static_cast<Allocator*>(this), n);
}
void deallocate(value_type* p, size_t n) {
std::allocator<char>::deallocate(p, n);
std::allocator_traits<Allocator>::deallocate(*static_cast<Allocator*>(this),
p, n);
}
};

TEST(memory_buffer_test, max_size_allocator) {
basic_memory_buffer<char, 10, smol_allocator> buffer;
buffer.resize(200);
// new_capacity = 200 + 200/2 = 300 > 256
buffer.resize(255); // Shouldn't throw.
// 160 = 128 + 32
using test_allocator = max_size_allocator<std::allocator<char>, 160>;
basic_memory_buffer<char, 10, test_allocator> buffer;
buffer.resize(128);
// new_capacity = 128 + 128/2 = 192 > 160
buffer.resize(160); // Shouldn't throw.
}

TEST(memory_buffer_test, max_size_allocator_overflow) {
basic_memory_buffer<char, 10, smol_allocator> buffer;
EXPECT_THROW(buffer.resize(256), std::exception);
using test_allocator = max_size_allocator<std::allocator<char>, 160>;
basic_memory_buffer<char, 10, test_allocator> buffer;
EXPECT_THROW(buffer.resize(161), std::exception);
}

TEST(format_test, exception_from_lib) {
Expand Down Expand Up @@ -2152,7 +2159,7 @@ TEST(format_int_test, format_int) {
EXPECT_EQ(fmt::format_int(42ul).str(), "42");
EXPECT_EQ(fmt::format_int(-42l).str(), "-42");
EXPECT_EQ(fmt::format_int(42ull).str(), "42");
EXPECT_EQ(fmt::format_int(-42ll).str(), "-42");\
EXPECT_EQ(fmt::format_int(-42ll).str(), "-42");
EXPECT_EQ(fmt::format_int(max_value<int64_t>()).str(),
std::to_string(max_value<int64_t>()));
}
Expand Down