Skip to content

Commit c27b6f6

Browse files
committed
Add comment for increasing_allocator
1 parent 55fce2a commit c27b6f6

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

libcxx/test/support/increasing_allocator.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,47 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef INCREASING_ALLOCATOR_H
10-
#define INCREASING_ALLOCATOR_H
9+
#ifndef TEST_SUPPORT_INCREASING_ALLOCATOR_H
10+
#define TEST_SUPPORT_INCREASING_ALLOCATOR_H
1111

1212
#include <cstddef>
1313
#include <memory>
1414

1515
#include "test_macros.h"
1616

17-
#if TEST_STD_VER >= 23
17+
// The increasing_allocator is a custom allocator that enforces an increasing minimum allocation size,
18+
// ensuring that it allocates an increasing amount of memory, possibly exceeding the requested amount.
19+
// This unique behavior is particularly useful for testing the shrink_to_fit functionality in std::vector,
20+
// vector<bool>, and std::basic_string, ensuring that shrink_to_fit does not increase the capacity of
21+
// the allocated memory.
22+
1823
template <typename T>
1924
struct increasing_allocator {
2025
using value_type = T;
2126
std::size_t min_elements = 1000;
2227
increasing_allocator() = default;
2328

2429
template <typename U>
25-
constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}
30+
TEST_CONSTEXPR_CXX20 increasing_allocator(const increasing_allocator<U>& other) TEST_NOEXCEPT
31+
: min_elements(other.min_elements) {}
2632

27-
constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
33+
#if TEST_STD_VER >= 23
34+
TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {
2835
if (n < min_elements)
2936
n = min_elements;
3037
min_elements += 1000;
3138
return std::allocator<T>{}.allocate_at_least(n);
3239
}
33-
constexpr T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
34-
constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
40+
#endif // TEST_STD_VER >= 23
41+
42+
TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
43+
44+
TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
3545
};
3646

3747
template <typename T, typename U>
38-
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
48+
TEST_CONSTEXPR_CXX20 bool operator==(increasing_allocator<T>, increasing_allocator<U>) TEST_NOEXCEPT {
3949
return true;
4050
}
41-
#endif // TEST_STD_VER >= 23
4251

43-
#endif // INCREASING_ALLOCATOR_H
52+
#endif // TEST_SUPPORT_INCREASING_ALLOCATOR_H

0 commit comments

Comments
 (0)