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

[libc++][vector] Fixes shrink_to_fit. #97895

Merged
merged 2 commits into from
Jul 20, 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
6 changes: 5 additions & 1 deletion libcxx/include/vector
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOE
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
allocator_type& __a = this->__alloc();
__split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
mordante marked this conversation as resolved.
Show resolved Hide resolved
__swap_out_circular_buffer(__v);
// The Standard mandates shrink_to_fit() does not increase the capacity.
// With equal capacity keep the existing buffer. This avoids extra work
// due to swapping the elements.
if (__v.capacity() < capacity())
__swap_out_circular_buffer(__v);
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
} catch (...) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,56 @@ TEST_CONSTEXPR_CXX20 bool tests() {
return true;
}

#if TEST_STD_VER >= 23
template <typename T>
struct increasing_allocator {
using value_type = T;
std::size_t min_elements = 1000;
increasing_allocator() = default;

template <typename U>
constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}

constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
if (n < min_elements)
n = min_elements;
min_elements += 1000;
return std::allocator<T>{}.allocate_at_least(n);
}
constexpr T* allocate(std::size_t n) { return allocate_at_least(n).ptr; }
constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
};

template <typename T, typename U>
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
return true;
}

// https://github.com/llvm/llvm-project/issues/95161
constexpr bool test_increasing_allocator() {
std::vector<int, increasing_allocator<int>> v;
v.push_back(1);
assert(is_contiguous_container_asan_correct(v));
std::size_t capacity = v.capacity();
v.shrink_to_fit();
assert(v.capacity() <= capacity);
assert(v.size() == 1);
assert(is_contiguous_container_asan_correct(v));

return true;
}
#endif // TEST_STD_VER >= 23

int main(int, char**)
{
tests();
tests();
#if TEST_STD_VER > 17
static_assert(tests());
#endif
#if TEST_STD_VER >= 23
test_increasing_allocator();
static_assert(test_increasing_allocator());
#endif

return 0;
}
Loading