Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

#include <string>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <type_traits>

#include "test_macros.h"

static int allocated_;
static std::ptrdiff_t allocated_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we make this a size_t instead?

Copy link
Contributor Author

@frederick-vs-ja frederick-vs-ja Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was intended to use a signed integer type to allow negative values.


template <class T, class Sz>
struct test_alloc {
Expand All @@ -40,13 +41,13 @@ struct test_alloc {
TEST_CONSTEXPR test_alloc(const test_alloc<U, Sz>&) TEST_NOEXCEPT {}

pointer allocate(size_type n, const void* = nullptr) {
allocated_ += n;
return std::allocator<value_type>().allocate(n);
allocated_ += static_cast<std::ptrdiff_t>(n);
return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));
}

void deallocate(pointer p, size_type s) {
allocated_ -= s;
std::allocator<value_type>().deallocate(p, s);
allocated_ -= static_cast<std::ptrdiff_t>(s);
std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));
}

template <class U>
Expand All @@ -64,14 +65,14 @@ struct test_alloc {

template <class Sz>
void test() {
using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
for (int i = 1; i < 1000; ++i) {
using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
{
Str s(i, 't');
assert(allocated_ == 0 || allocated_ >= i);
(void)s;
}
assert(allocated_ == 0);
}
assert(allocated_ == 0);
}

int main(int, char**) {
Expand Down
Loading