-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Allocator::max_size support in basic_memory_buffer #1960
Conversation
Thanks for the PR but I don't think it's necessary because |
If "size <= max_size && new_capacity > max_size" then exception thrown from allocate() will not be correctly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks reasonable. Please add a unit test and address the inline comments.
include/fmt/format.h
Outdated
if (size > max_size) { | ||
throw std::length_error("fmt::basic_memory_buffer::grow"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this to reduce the number of checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
include/fmt/format.h
Outdated
if (new_capacity > max_size) new_capacity = max_size; | ||
if (size > new_capacity) new_capacity = size; // size is never greater max_size. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only need to check new_capacity
if it is not greater than size
. The rest allocate
will take care of. Something like:
if (size > new_capacity) new_capacity = size;
else if (new_capacity > max_size) new_capacity = (std::max)(size, max_size);
50a5e4f
to
8f7a638
Compare
Commit updated. |
test/mock-allocator.h
Outdated
template <typename Allocator, size_t MaxSize> | ||
class allocator_max_size: public Allocator { | ||
public: | ||
typedef typename Allocator::value_type value_type; | ||
size_t max_size() const FMT_NOEXCEPT { | ||
return MaxSize; | ||
} | ||
value_type* allocate(size_t n) { | ||
if (n > max_size()) { | ||
throw std::length_error("size > max_size"); | ||
} | ||
return std::allocator_traits<Allocator>::allocate( | ||
*static_cast<Allocator *>(this), n); | ||
} | ||
void deallocate(value_type* p, size_t n) { | ||
std::allocator_traits<Allocator>::deallocate( | ||
*static_cast<Allocator *>(this), p, n); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this to the format-test.cc
since it's only used there and not a mock.
8f7a638
to
1250382
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more nits, otherwise LGTM
test/format-test.cc
Outdated
template <typename Allocator, size_t MaxSize> | ||
class allocator_max_size: public Allocator { | ||
public: | ||
typedef typename Allocator::value_type value_type; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and below: typedef -> using
test/format-test.cc
Outdated
} catch (const std::exception &) { | ||
throws_on_resize = true; | ||
} | ||
EXPECT_EQ(throws_on_resize, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EXPECT_TRUE
test/format-test.cc
Outdated
} catch (const std::exception &) { | ||
throws_on_resize = true; | ||
} | ||
EXPECT_EQ(throws_on_resize, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EXPECT_TRUE
1250382
to
d290fa5
Compare
Thank you! |
The allocator may have a limit on the size of the allocated memory.
I agree that my contributions are licensed under the {fmt} license, and agree to future changes to the licensing.