Skip to content

Commit 2e82109

Browse files
committed
[libc++][vector] Fixes shrink_to_fit.
This assures shrink_to_fit does not increase the allocated size. Partly addresses #95161
1 parent f90bac9 commit 2e82109

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

libcxx/include/vector

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,11 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<_Tp, _Allocator>::shrink_to_fit() _NOE
14431443
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
14441444
allocator_type& __a = this->__alloc();
14451445
__split_buffer<value_type, allocator_type&> __v(size(), size(), __a);
1446-
__swap_out_circular_buffer(__v);
1446+
// The Standard mandates shrink_to_fit() does not increase the capacity.
1447+
// With equal capacity keep the existing buffer. This avoids extra work
1448+
// due to swapping the elements.
1449+
if (__v.capacity() < capacity())
1450+
__swap_out_circular_buffer(__v);
14471451
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
14481452
} catch (...) {
14491453
}

libcxx/test/std/containers/sequences/vector/vector.capacity/shrink_to_fit.pass.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,50 @@ TEST_CONSTEXPR_CXX20 bool tests() {
7171
return true;
7272
}
7373

74+
#if TEST_STD_VER >= 23
75+
std::size_t min_bytes = 1000;
76+
77+
template <typename T>
78+
struct increasing_allocator {
79+
using value_type = T;
80+
increasing_allocator() = default;
81+
template <typename U>
82+
increasing_allocator(const increasing_allocator<U>&) noexcept {}
83+
std::allocation_result<T*> allocate_at_least(std::size_t n) {
84+
std::size_t allocation_amount = n * sizeof(T);
85+
if (allocation_amount < min_bytes)
86+
allocation_amount = min_bytes;
87+
min_bytes += 1000;
88+
return {static_cast<T*>(::operator new(allocation_amount)), allocation_amount};
89+
}
90+
T* allocate(std::size_t n) { return allocate_at_least(n).ptr; }
91+
void deallocate(T* p, std::size_t) noexcept { ::operator delete(static_cast<void*>(p)); }
92+
};
93+
94+
template <typename T, typename U>
95+
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
96+
return true;
97+
}
98+
99+
// https://github.com/llvm/llvm-project/issues/95161
100+
void test_increasing_allocator() {
101+
std::vector<int, increasing_allocator<int>> v;
102+
v.push_back(1);
103+
assert(is_contiguous_container_asan_correct(v));
104+
std::size_t capacity = v.capacity();
105+
v.shrink_to_fit();
106+
assert(v.capacity() == capacity);
107+
assert(v.size() == 1);
108+
assert(is_contiguous_container_asan_correct(v));
109+
}
110+
#endif // TEST_STD_VER >= 23
111+
74112
int main(int, char**)
75113
{
76114
tests();
115+
#if TEST_STD_VER >= 23
116+
test_increasing_allocator();
117+
#endif
77118
#if TEST_STD_VER > 17
78119
static_assert(tests());
79120
#endif

0 commit comments

Comments
 (0)