Skip to content

Commit d71721f

Browse files
committed
Slightly simplify max_size and add new tests for vector
1 parent 99354f9 commit d71721f

File tree

4 files changed

+194
-11
lines changed

4 files changed

+194
-11
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,8 @@ template <class _Allocator>
511511
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
512512
vector<bool, _Allocator>::max_size() const _NOEXCEPT {
513513
size_type __amax = __storage_traits::max_size(__alloc_);
514-
size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
515-
if (__nmax / __bits_per_word <= __amax)
516-
return __nmax;
517-
return __internal_cap_to_external(__amax);
514+
size_type __nmax = numeric_limits<difference_type>::max();
515+
return __nmax / __bits_per_word <= __amax ? __nmax : __internal_cap_to_external(__amax);
518516
}
519517

520518
// Precondition: __new_size > capacity()
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <vector>
10+
11+
// size_type max_size() const;
12+
13+
#include <cassert>
14+
#include <climits>
15+
#include <cstdint>
16+
#include <limits>
17+
#include <memory>
18+
#include <type_traits>
19+
#include <vector>
20+
21+
#include "min_allocator.h"
22+
#include "sized_allocator.h"
23+
#include "test_allocator.h"
24+
#include "test_macros.h"
25+
26+
template <typename Alloc>
27+
TEST_CONSTEXPR_CXX20 void test(const std::vector<bool, Alloc>& v) {
28+
using Vector = std::vector<bool, Alloc>;
29+
using size_type = typename Vector::size_type;
30+
using difference_type = typename Vector::difference_type;
31+
using storage_type = typename Vector::__storage_type;
32+
using storage_alloc = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>;
33+
using storage_traits = typename std::allocator_traits<Alloc>::template rebind_traits<storage_type>;
34+
const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max());
35+
const size_type max_alloc = storage_traits::max_size(storage_alloc(v.get_allocator()));
36+
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
37+
const size_type max_size = max_dist / bits_per_word < max_alloc ? max_dist : max_alloc * bits_per_word;
38+
assert(v.max_size() <= max_dist);
39+
assert(v.max_size() / bits_per_word <= max_alloc); // max_alloc * bits_per_word may overflow
40+
LIBCPP_ASSERT(v.max_size() == max_size);
41+
}
42+
43+
TEST_CONSTEXPR_CXX20 bool tests() {
44+
// Test with limited_allocator where v.max_size() is determined by allocator::max_size()
45+
{
46+
using Alloc = limited_allocator<bool, 10>;
47+
using Vector = std::vector<bool, Alloc>;
48+
using storage_type = Vector::__storage_type;
49+
Vector v;
50+
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
51+
assert(v.max_size() <= 10 * bits_per_word);
52+
LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
53+
}
54+
{
55+
using Alloc = limited_allocator<double, 10>;
56+
using Vector = std::vector<bool, Alloc>;
57+
using storage_type = Vector::__storage_type;
58+
Vector v;
59+
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
60+
assert(v.max_size() <= 10 * bits_per_word);
61+
LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
62+
}
63+
64+
// Test with various allocators and diffrent size_type
65+
{
66+
test(std::vector<bool>());
67+
test(std::vector<bool, std::allocator<int> >());
68+
test(std::vector<bool, min_allocator<bool> >());
69+
test(std::vector<bool, test_allocator<bool> >(test_allocator<bool>(1)));
70+
test(std::vector<bool, other_allocator<bool> >(other_allocator<bool>(5)));
71+
test(std::vector<bool, sized_allocator<bool, std::uint8_t, std::int8_t> >());
72+
test(std::vector<bool, sized_allocator<bool, std::uint16_t, std::int16_t> >());
73+
test(std::vector<bool, sized_allocator<bool, std::uint32_t, std::int32_t> >());
74+
test(std::vector<bool, sized_allocator<bool, std::uint64_t, std::int64_t> >());
75+
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1)> >());
76+
}
77+
78+
// Test cases to identify incorrect implementations that unconditionally return:
79+
// std::min<size_type>(__nmax, __internal_cap_to_external(__amax))
80+
// This can cause overflow in __internal_cap_to_external and lead to incorrect results.
81+
{
82+
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 61>>());
83+
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 63>>());
84+
}
85+
86+
return true;
87+
}
88+
89+
int main(int, char**) {
90+
tests();
91+
92+
#if TEST_STD_VER >= 20
93+
static_assert(tests());
94+
#endif
95+
96+
return 0;
97+
}

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,30 @@
1111
// size_type max_size() const;
1212

1313
#include <cassert>
14+
#include <cstdint>
1415
#include <limits>
1516
#include <type_traits>
1617
#include <vector>
1718

19+
#include "min_allocator.h"
20+
#include "sized_allocator.h"
1821
#include "test_allocator.h"
1922
#include "test_macros.h"
2023

24+
template <typename T, typename Alloc>
25+
TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) {
26+
using Vector = std::vector<T, Alloc>;
27+
using alloc_traits = typename Vector::__alloc_traits;
28+
using size_type = typename Vector::size_type;
29+
using difference_type = typename Vector::difference_type;
30+
const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max());
31+
const size_type max_alloc = alloc_traits::max_size(v.get_allocator());
32+
assert(v.max_size() <= max_dist);
33+
assert(v.max_size() <= max_alloc);
34+
LIBCPP_ASSERT(v.max_size() == std::min(max_dist, max_alloc));
35+
}
2136

22-
TEST_CONSTEXPR_CXX20 bool test() {
37+
TEST_CONSTEXPR_CXX20 bool tests() {
2338
{
2439
typedef limited_allocator<int, 10> A;
2540
typedef std::vector<int, A> C;
@@ -30,29 +45,44 @@ TEST_CONSTEXPR_CXX20 bool test() {
3045
{
3146
typedef limited_allocator<int, (std::size_t)-1> A;
3247
typedef std::vector<int, A> C;
33-
const C::size_type max_dist =
34-
static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
48+
const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
3549
C c;
3650
assert(c.max_size() <= max_dist);
3751
LIBCPP_ASSERT(c.max_size() == max_dist);
3852
}
3953
{
4054
typedef std::vector<char> C;
41-
const C::size_type max_dist =
42-
static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
55+
const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
4356
C c;
4457
assert(c.max_size() <= max_dist);
4558
assert(c.max_size() <= alloc_max_size(c.get_allocator()));
59+
LIBCPP_ASSERT(c.max_size() == std::min(max_dist, alloc_max_size(c.get_allocator())));
60+
}
61+
62+
// Test with various allocators and diffrent size_type
63+
{
64+
test(std::vector<int>());
65+
test(std::vector<short, std::allocator<short> >());
66+
test(std::vector<unsigned, min_allocator<unsigned> >());
67+
test(std::vector<char, test_allocator<char> >(test_allocator<char>(1)));
68+
test(std::vector<std::size_t, other_allocator<std::size_t> >(other_allocator<std::size_t>(5)));
69+
test(std::vector<int, sized_allocator<int, std::uint8_t, std::int8_t> >());
70+
test(std::vector<int, sized_allocator<int, std::uint16_t, std::int16_t> >());
71+
test(std::vector<int, sized_allocator<int, std::uint32_t, std::int32_t> >());
72+
test(std::vector<int, sized_allocator<int, std::uint64_t, std::int64_t> >());
73+
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1)> >());
74+
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 2> >());
75+
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 4> >());
4676
}
4777

4878
return true;
4979
}
5080

5181
int main(int, char**) {
52-
test();
82+
tests();
5383

5484
#if TEST_STD_VER > 17
55-
static_assert(test());
85+
static_assert(tests());
5686
#endif
5787

5888
return 0;

libcxx/test/support/sized_allocator.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef TEST_SUPPORT_SIZED_ALLOCATOR_H
10+
#define TEST_SUPPORT_SIZED_ALLOCATOR_H
11+
12+
#include <cstddef>
13+
#include <limits>
14+
#include <memory>
15+
#include <new>
16+
17+
#include "test_macros.h"
18+
19+
template <typename T, typename SIZE_TYPE = std::size_t, typename DIFF_TYPE = std::ptrdiff_t>
20+
class sized_allocator {
21+
template <typename U, typename Sz, typename Diff>
22+
friend class sized_allocator;
23+
24+
public:
25+
using value_type = T;
26+
using size_type = SIZE_TYPE;
27+
using difference_type = DIFF_TYPE;
28+
using propagate_on_container_swap = std::true_type;
29+
30+
TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}
31+
32+
template <typename U, typename Sz, typename Diff>
33+
TEST_CONSTEXPR_CXX20 sized_allocator(const sized_allocator<U, Sz, Diff>& a) TEST_NOEXCEPT : data_(a.data_) {}
34+
35+
TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {
36+
if (n > max_size())
37+
TEST_THROW(std::bad_array_new_length());
38+
return std::allocator<T>().allocate(n);
39+
}
40+
41+
TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
42+
43+
TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {
44+
return std::numeric_limits<size_type>::max() / sizeof(value_type);
45+
}
46+
47+
private:
48+
int data_;
49+
50+
TEST_CONSTEXPR friend bool operator==(const sized_allocator& a, const sized_allocator& b) {
51+
return a.data_ == b.data_;
52+
}
53+
TEST_CONSTEXPR friend bool operator!=(const sized_allocator& a, const sized_allocator& b) {
54+
return a.data_ != b.data_;
55+
}
56+
};
57+
58+
#endif

0 commit comments

Comments
 (0)