Skip to content

Commit 994c324

Browse files
committed
Address ldionne's comments
1 parent 3c84df6 commit 994c324

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
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
@@ -2623,7 +2623,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
26232623
__throw_length_error();
26242624
pointer __old_p = __get_pointer();
26252625
size_type __cap =
2626-
__old_cap < __ms / 2 - __alignment ? __recommend(std::max<size_type>(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
2626+
__old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms;
26272627
__annotate_delete();
26282628
auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
26292629
auto __allocation = std::__allocate_at_least(__alloc_, __cap + 1);

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

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,93 +20,74 @@
2020
#include <new>
2121
#include <vector>
2222

23+
#include "sized_allocator.h"
2324
#include "test_macros.h"
2425

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-
6426
TEST_CONSTEXPR_CXX20 bool tests() {
6527
// The following tests are typical ways to trigger reallocations where `std::max` is used to calculate the capacity.
28+
// The purpose of these tests is to ensure that the ambiguous internal calls to `std::max` have been fixed.
6629
{
6730
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
6831
std::vector<bool, Alloc> c(Alloc(1));
6932
c.resize(10);
33+
assert(c.size() == 10);
34+
assert(c.capacity() >= 10);
7035
}
7136
{
7237
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
7338
std::vector<bool, Alloc> c(Alloc(1));
7439
c.assign(10, true);
40+
assert(c.size() == 10);
41+
assert(c.capacity() >= 10);
7542
}
7643
{
7744
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
7845
std::vector<bool, Alloc> c(Alloc(1));
7946
c.insert(c.end(), true);
47+
assert(c.size() == 1);
48+
assert(c.capacity() >= 1);
8049
}
8150
{
8251
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
8352
std::vector<bool, Alloc> c(Alloc(1));
8453
c.insert(c.end(), 10, true);
54+
assert(c.size() == 10);
55+
assert(c.capacity() >= 10);
8556
}
8657
{
8758
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
8859
std::vector<bool, Alloc> c(Alloc(1));
8960
c.push_back(true);
61+
assert(c.size() == 1);
62+
assert(c.capacity() >= 1);
9063
}
9164
{
9265
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
9366
std::vector<bool, Alloc> c(Alloc(1));
9467
c.resize(10, true);
68+
assert(c.size() == 10);
69+
assert(c.capacity() >= 10);
9570
}
9671
{
9772
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
9873
std::vector<bool, Alloc> c(Alloc(1));
9974
c.resize(10);
75+
assert(c.size() == 10);
76+
assert(c.capacity() >= 10);
10077
}
10178
{
10279
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
10380
std::vector<bool, Alloc> c(Alloc(1));
10481
c.resize(10);
82+
assert(c.size() == 10);
83+
assert(c.capacity() >= 10);
10584
}
10685
{
10786
using Alloc = sized_allocator<bool, std::size_t, std::ptrdiff_t>;
10887
std::vector<bool, Alloc> c(Alloc(1));
10988
c.resize(10);
89+
assert(c.size() == 10);
90+
assert(c.capacity() >= 10);
11091
}
11192

11293
return true;

0 commit comments

Comments
 (0)