-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Fix throwing away smaller allocations in string::shrink_to_fit #115659
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
Merged
philnik777
merged 1 commit into
llvm:main
from
philnik777:shrink_to_fit_fix_allocate_at_least_overallocating
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
libcxx/test/libcxx/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 | ||
|
||
// <string> | ||
|
||
// void shrink_to_fit(); // constexpr since C++20 | ||
|
||
// Make sure we use an allocation returned by allocate_at_least if it is smaller than the current allocation | ||
// even if it contains more bytes than we requested | ||
|
||
#include <cassert> | ||
#include <string> | ||
|
||
template <typename T> | ||
struct oversizing_allocator { | ||
using value_type = T; | ||
oversizing_allocator() = default; | ||
template <typename U> | ||
oversizing_allocator(const oversizing_allocator<U>&) noexcept {} | ||
std::allocation_result<T*> allocate_at_least(std::size_t n) { | ||
++n; | ||
return {static_cast<T*>(::operator new(n * sizeof(T))), n}; | ||
} | ||
T* allocate(std::size_t n) { return allocate_at_least(n).ptr; } | ||
void deallocate(T* p, std::size_t) noexcept { ::operator delete(static_cast<void*>(p)); } | ||
}; | ||
|
||
template <typename T, typename U> | ||
bool operator==(oversizing_allocator<T>, oversizing_allocator<U>) { | ||
return true; | ||
} | ||
|
||
void test_oversizing_allocator() { | ||
std::basic_string<char, std::char_traits<char>, oversizing_allocator<char>> s{ | ||
"String does not fit in the internal buffer and is a bit longer"}; | ||
s = "String does not fit in the internal buffer"; | ||
std::size_t capacity = s.capacity(); | ||
std::size_t size = s.size(); | ||
s.shrink_to_fit(); | ||
assert(s.capacity() < capacity); | ||
assert(s.size() == size); | ||
} | ||
|
||
int main(int, char**) { | ||
test_oversizing_allocator(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn't this something we can check for all implementations? I don't think this test is libc++ specific?
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.
Implementations aren't required to do anything when
shrink_to_fit()
is called.void shrink_to_fit() {}
is a valid implementation: http://eel.is/c++draft/basic.string#string.capacity-16