Refactor stateful_allocator to test helpers

This commit is contained in:
Christian Mazakas
2023-04-21 13:33:18 -07:00
parent 2cf72093b1
commit 1e92d9f545
2 changed files with 28 additions and 28 deletions

View File

@ -44,34 +44,6 @@ template <class T> struct soccc_allocator
bool operator!=(soccc_allocator const& rhs) const { return x_ != rhs.x_; }
};
template <class T> struct stateful_allocator
{
int x_ = -1;
using value_type = T;
stateful_allocator() = default;
stateful_allocator(stateful_allocator const&) = default;
stateful_allocator(stateful_allocator&&) = default;
stateful_allocator(int const x) : x_{x} {}
template <class U>
stateful_allocator(stateful_allocator<U> const& rhs) : x_{rhs.x_}
{
}
T* allocate(std::size_t n)
{
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t) { ::operator delete(p); }
bool operator==(stateful_allocator const& rhs) const { return x_ == rhs.x_; }
bool operator!=(stateful_allocator const& rhs) const { return x_ != rhs.x_; }
};
using hasher = stateful_hash;
using key_equal = stateful_key_equal;
using allocator_type = stateful_allocator<std::pair<raii const, raii> >;

View File

@ -103,6 +103,34 @@ struct stateful_key_equal
}
};
template <class T> struct stateful_allocator
{
int x_ = -1;
using value_type = T;
stateful_allocator() = default;
stateful_allocator(stateful_allocator const&) = default;
stateful_allocator(stateful_allocator&&) = default;
stateful_allocator(int const x) : x_{x} {}
template <class U>
stateful_allocator(stateful_allocator<U> const& rhs) : x_{rhs.x_}
{
}
T* allocate(std::size_t n)
{
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t) { ::operator delete(p); }
bool operator==(stateful_allocator const& rhs) const { return x_ == rhs.x_; }
bool operator!=(stateful_allocator const& rhs) const { return x_ != rhs.x_; }
};
struct raii
{
static std::atomic<std::uint32_t> default_constructor;