Skip to content

Commit 30775a7

Browse files
committed
Address ldionne's comments
1 parent d3e04e7 commit 30775a7

File tree

4 files changed

+27
-48
lines changed

4 files changed

+27
-48
lines changed

libcxx/include/__cxx03/string

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,9 +2483,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
24832483
__throw_length_error();
24842484
pointer __old_p = __get_pointer();
24852485
size_type __cap =
2486-
__old_cap < __ms / 2 - __alignment
2487-
? __recommend(std::max<size_type>(__old_cap + __delta_cap, 2 * __old_cap))
2488-
: __ms - 1;
2486+
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
24892487
__annotate_delete();
24902488
auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
24912489
pointer __p = __allocation.ptr;
@@ -2528,9 +2526,7 @@ _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Trait
25282526
__throw_length_error();
25292527
pointer __old_p = __get_pointer();
25302528
size_type __cap =
2531-
__old_cap < __ms / 2 - __alignment
2532-
? __recommend(std::max<size_type>(__old_cap + __delta_cap, 2 * __old_cap))
2533-
: __ms - 1;
2529+
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
25342530
__annotate_delete();
25352531
auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
25362532
pointer __p = __allocation.ptr;

libcxx/include/__cxx03/vector

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2328,7 +2328,7 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
23282328
const size_type __cap = capacity();
23292329
if (__cap >= __ms / 2)
23302330
return __ms;
2331-
return std::max<size_type>(2 * __cap, __align_it(__new_size));
2331+
return std::max(2 * __cap, __align_it(__new_size));
23322332
}
23332333

23342334
// Default constructs __n objects starting at __end_

libcxx/include/string

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
26292629
__throw_length_error();
26302630
pointer __old_p = __get_pointer();
26312631
size_type __cap =
2632-
__old_cap < __ms / 2 - __alignment ? __recommend(std::max<size_type>(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
2632+
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
26332633
__annotate_delete();
26342634
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
26352635
auto __allocation = std::__allocate_at_least(__alloc_, __cap + 1);

libcxx/test/std/containers/sequences/vector.bool/sized_allocator.pass.cpp

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
// <vector>
1010
// vector<bool>
1111

12-
// This test examines ambiguous calls to std::max in vector<bool>
12+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
13+
14+
// This test ensures that ambiguous calls to std::max in vector<bool> is fixed.
1315
// Fix https://github.com/llvm/llvm-project/issues/121713
1416

1517
#include <cassert>
@@ -20,93 +22,74 @@
2022
#include <new>
2123
#include <vector>
2224

25+
#include "sized_allocator.h"
2326
#include "test_macros.h"
2427

25-
template <typename T, typename SIZE_TYPE = std::size_t, typename DIFF_TYPE = std::ptrdiff_t>
26-
class sized_allocator {
27-
template <typename U, typename Sz, typename Diff>
28-
friend class sized_allocator;
29-
30-
public:
31-
using value_type = T;
32-
using size_type = SIZE_TYPE;
33-
using difference_type = DIFF_TYPE;
34-
using propagate_on_container_swap = std::true_type;
35-
36-
TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}
37-
38-
template <typename U, typename Sz, typename Diff>
39-
TEST_CONSTEXPR_CXX20 sized_allocator(const sized_allocator<U, Sz, Diff>& a) TEST_NOEXCEPT : data_(a.data_) {}
40-
41-
TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {
42-
if (n > max_size())
43-
TEST_THROW(std::bad_array_new_length());
44-
return std::allocator<T>().allocate(n);
45-
}
46-
47-
TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
48-
49-
TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {
50-
return std::numeric_limits<size_type>::max() / sizeof(value_type);
51-
}
52-
53-
private:
54-
int data_;
55-
56-
TEST_CONSTEXPR friend bool operator==(const sized_allocator& a, const sized_allocator& b) {
57-
return a.data_ == b.data_;
58-
}
59-
TEST_CONSTEXPR friend bool operator!=(const sized_allocator& a, const sized_allocator& b) {
60-
return a.data_ != b.data_;
61-
}
62-
};
63-
6428
TEST_CONSTEXPR_CXX20 bool tests() {
6529
// The following tests are typical ways to trigger reallocations where `std::max` is used to calculate the capacity.
30+
// The purpose of these tests is to ensure that the ambiguous internal calls to `std::max` have been fixed.
6631
{
6732
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
6833
std::vector<bool, Alloc> c(Alloc(1));
6934
c.resize(10);
35+
assert(c.size() == 10);
36+
assert(c.capacity() >= 10);
7037
}
7138
{
7239
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
7340
std::vector<bool, Alloc> c(Alloc(1));
7441
c.assign(10, true);
42+
assert(c.size() == 10);
43+
assert(c.capacity() >= 10);
7544
}
7645
{
7746
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
7847
std::vector<bool, Alloc> c(Alloc(1));
7948
c.insert(c.end(), true);
49+
assert(c.size() == 1);
50+
assert(c.capacity() >= 1);
8051
}
8152
{
8253
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
8354
std::vector<bool, Alloc> c(Alloc(1));
8455
c.insert(c.end(), 10, true);
56+
assert(c.size() == 10);
57+
assert(c.capacity() >= 10);
8558
}
8659
{
8760
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
8861
std::vector<bool, Alloc> c(Alloc(1));
8962
c.push_back(true);
63+
assert(c.size() == 1);
64+
assert(c.capacity() >= 1);
9065
}
9166
{
9267
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
9368
std::vector<bool, Alloc> c(Alloc(1));
9469
c.resize(10, true);
70+
assert(c.size() == 10);
71+
assert(c.capacity() >= 10);
9572
}
9673
{
9774
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
9875
std::vector<bool, Alloc> c(Alloc(1));
9976
c.resize(10);
77+
assert(c.size() == 10);
78+
assert(c.capacity() >= 10);
10079
}
10180
{
10281
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
10382
std::vector<bool, Alloc> c(Alloc(1));
10483
c.resize(10);
84+
assert(c.size() == 10);
85+
assert(c.capacity() >= 10);
10586
}
10687
{
10788
using Alloc = sized_allocator<bool, std::size_t, std::ptrdiff_t>;
10889
std::vector<bool, Alloc> c(Alloc(1));
10990
c.resize(10);
91+
assert(c.size() == 10);
92+
assert(c.capacity() >= 10);
11093
}
11194

11295
return true;

0 commit comments

Comments
 (0)