Skip to content

Commit

Permalink
Provide only a const version and run required clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
winner245 committed Dec 7, 2024
1 parent 043557a commit 28f747d
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions libcxx/test/support/test_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,45 @@ TEST_CONSTEXPR_CXX20 inline typename std::allocator_traits<Alloc>::size_type all
}

struct test_allocator_statistics {
int time_to_throw = 0;
int throw_after = INT_MAX;
int time_to_throw = 0;
int throw_after = INT_MAX;
int count = 0; // the number of active instances
int alloc_count = 0; // the number of allocations not deallocating
int allocated_size = 0; // the size of allocated elements
int construct_count = 0; // the number of times that ::construct was called
int destroy_count = 0; // the number of times that ::destroy was called
int copied = 0;
int moved = 0;
int converted = 0;
int destroy_count = 0; // the number of times that ::destroy was called
int copied = 0;
int moved = 0;
int converted = 0;

TEST_CONSTEXPR_CXX14 void clear() {
assert(count == 0 && "clearing leaking allocator data?");
count = 0;
time_to_throw = 0;
alloc_count = 0;
count = 0;
time_to_throw = 0;
alloc_count = 0;
allocated_size = 0;
construct_count = 0;
destroy_count = 0;
throw_after = INT_MAX;
destroy_count = 0;
throw_after = INT_MAX;
clear_ctor_counters();
}

TEST_CONSTEXPR_CXX14 void clear_ctor_counters() {
copied = 0;
moved = 0;
copied = 0;
moved = 0;
converted = 0;
}
};

struct test_alloc_base {
TEST_CONSTEXPR static const int destructed_value = -1;
TEST_CONSTEXPR static const int moved_value = INT_MAX;
TEST_CONSTEXPR static const int moved_value = INT_MAX;
};

template <class T>
class test_allocator {
int data_ = 0; // participates in equality
int id_ = 0; // unique identifier, doesn't participate in equality
int data_ = 0; // participates in equality
int id_ = 0; // unique identifier, doesn't participate in equality
test_allocator_statistics* stats_ = nullptr;

template <class U>
Expand Down Expand Up @@ -95,21 +95,26 @@ class test_allocator {
TEST_CONSTEXPR explicit test_allocator(int data) TEST_NOEXCEPT : data_(data) {}

TEST_CONSTEXPR_CXX14 explicit test_allocator(int data, test_allocator_statistics* stats) TEST_NOEXCEPT
: data_(data), stats_(stats) {
: data_(data),
stats_(stats) {
if (stats != nullptr)
++stats_->count;
}

TEST_CONSTEXPR explicit test_allocator(int data, int id) TEST_NOEXCEPT : data_(data), id_(id) {}

TEST_CONSTEXPR_CXX14 explicit test_allocator(int data, int id, test_allocator_statistics* stats) TEST_NOEXCEPT
: data_(data), id_(id), stats_(stats) {
: data_(data),
id_(id),
stats_(stats) {
if (stats_ != nullptr)
++stats_->count;
}

TEST_CONSTEXPR_CXX14 test_allocator(const test_allocator& a) TEST_NOEXCEPT
: data_(a.data_), id_(a.id_), stats_(a.stats_) {
: data_(a.data_),
id_(a.id_),
stats_(a.stats_) {
assert(a.data_ != test_alloc_base::destructed_value && a.id_ != test_alloc_base::destructed_value &&
"copying from destroyed allocator");
if (stats_ != nullptr) {
Expand All @@ -130,7 +135,9 @@ class test_allocator {

template <class U>
TEST_CONSTEXPR_CXX14 test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
: data_(a.data_), id_(a.id_), stats_(a.stats_) {
: data_(a.data_),
id_(a.id_),
stats_(a.stats_) {
if (stats_ != nullptr) {
++stats_->count;
++stats_->converted;
Expand All @@ -143,7 +150,7 @@ class test_allocator {
if (stats_ != nullptr)
--stats_->count;
data_ = test_alloc_base::destructed_value;
id_ = test_alloc_base::destructed_value;
id_ = test_alloc_base::destructed_value;
}

TEST_CONSTEXPR pointer address(reference x) const { return &x; }
Expand Down Expand Up @@ -197,8 +204,8 @@ class test_allocator {

template <>
class test_allocator<void> {
int data_ = 0;
int id_ = 0;
int data_ = 0;
int id_ = 0;
test_allocator_statistics* stats_ = nullptr;

template <class U>
Expand All @@ -223,27 +230,30 @@ class test_allocator<void> {
TEST_CONSTEXPR explicit test_allocator(int data) TEST_NOEXCEPT : data_(data) {}

TEST_CONSTEXPR explicit test_allocator(int data, test_allocator_statistics* stats) TEST_NOEXCEPT
: data_(data), stats_(stats)
{}
: data_(data),
stats_(stats) {}

TEST_CONSTEXPR explicit test_allocator(int data, int id) : data_(data), id_(id) {}

TEST_CONSTEXPR_CXX14 explicit test_allocator(int data, int id, test_allocator_statistics* stats) TEST_NOEXCEPT
: data_(data), id_(id), stats_(stats)
{}
: data_(data),
id_(id),
stats_(stats) {}

TEST_CONSTEXPR_CXX14 explicit test_allocator(const test_allocator& a) TEST_NOEXCEPT
: data_(a.data_), id_(a.id_), stats_(a.stats_)
{}
: data_(a.data_),
id_(a.id_),
stats_(a.stats_) {}

template <class U>
TEST_CONSTEXPR_CXX14 test_allocator(const test_allocator<U>& a) TEST_NOEXCEPT
: data_(a.data_), id_(a.id_), stats_(a.stats_)
{}
: data_(a.data_),
id_(a.id_),
stats_(a.stats_) {}

TEST_CONSTEXPR_CXX20 ~test_allocator() TEST_NOEXCEPT {
data_ = test_alloc_base::destructed_value;
id_ = test_alloc_base::destructed_value;
id_ = test_alloc_base::destructed_value;
}

TEST_CONSTEXPR int get_id() const { return id_; }
Expand Down Expand Up @@ -310,9 +320,9 @@ struct Tag_X {
TEST_CONSTEXPR Tag_X(Ctor_Tag, Args&&...) {}

// not DefaultConstructible, CopyConstructible or MoveConstructible.
Tag_X() = delete;
Tag_X() = delete;
Tag_X(const Tag_X&) = delete;
Tag_X(Tag_X&&) = delete;
Tag_X(Tag_X&&) = delete;

// CopyAssignable.
TEST_CONSTEXPR_CXX14 Tag_X& operator=(const Tag_X&) { return *this; };
Expand All @@ -329,7 +339,7 @@ struct Tag_X {
template <typename T>
class TaggingAllocator {
public:
using value_type = T;
using value_type = T;
TaggingAllocator() = default;

template <typename U>
Expand All @@ -356,13 +366,13 @@ class TaggingAllocator {
template <std::size_t MaxAllocs>
struct limited_alloc_handle {
std::size_t outstanding_ = 0;
void* last_alloc_ = nullptr;
void* last_alloc_ = nullptr;

template <class T>
TEST_CONSTEXPR_CXX20 T* allocate(std::size_t N) {
if (N + outstanding_ > MaxAllocs)
TEST_THROW(std::bad_alloc());
auto alloc = std::allocator<T>().allocate(N);
auto alloc = std::allocator<T>().allocate(N);
last_alloc_ = alloc;
outstanding_ += N;
return alloc;
Expand Down Expand Up @@ -467,9 +477,6 @@ class limited_allocator {
TEST_CONSTEXPR_CXX20 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
TEST_CONSTEXPR_CXX20 void deallocate(pointer p, size_type n) { handle_->template deallocate<T>(p, n); }
TEST_CONSTEXPR size_type max_size() const { return N; }

// In C++11, constexpr non-static member functions are implicitly const, but this is no longer the case since C++14.
TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); }
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); }
};

Expand Down

0 comments on commit 28f747d

Please sign in to comment.