kokkos-execution
0.0.1
Toggle main menu visibility
Loading...
Searching...
No Matches
tracking_allocator.hpp
Go to the documentation of this file.
1
#ifndef KOKKOS_EXECUTION_TESTS_UTILS_TRACKING_ALLOCATOR_HPP
2
#define KOKKOS_EXECUTION_TESTS_UTILS_TRACKING_ALLOCATOR_HPP
3
4
#include <atomic>
5
#include <memory>
6
#include <utility>
7
8
namespace
Tests::Utils
{
9
15
template
<
typename
T>
16
struct
TrackingAllocator
{
17
using
value_type
= T;
18
19
std::atomic<size_t>*
count
;
20
21
T*
allocate
(std::size_t n) {
22
++(*count);
23
return
std::allocator<T>{}.allocate(n);
24
}
25
26
void
deallocate
(T* ptr, std::size_t size) {
27
std::allocator<T>{}.deallocate(ptr, size);
28
}
29
30
friend
constexpr
auto
operator<=>
(
const
TrackingAllocator
&,
const
TrackingAllocator
&)
noexcept
=
default
;
31
};
32
38
template
<
typename
Allocator,
typename
T>
39
auto
round_trip_allocate
(Allocator& allocator, T&& value) {
40
using
traits = std::allocator_traits<Allocator>;
41
42
static_assert
(std::same_as<typename traits::value_type, std::remove_cvref_t<T>>);
43
44
auto
* ptr = traits::allocate(allocator, 1);
45
46
traits::construct(allocator, ptr, std::forward<T>(value));
47
std::remove_cvref_t<T> result = *ptr;
// NOLINT(misc-const-correctness)
48
49
traits::destroy(allocator, ptr);
50
traits::deallocate(allocator, ptr, 1);
51
52
return
result;
53
}
54
55
}
// namespace Tests::Utils
56
57
#endif
// KOKKOS_EXECUTION_TESTS_UTILS_TRACKING_ALLOCATOR_HPP
Tests::Utils
Definition
atomic.hpp:59
Tests::Utils::round_trip_allocate
auto round_trip_allocate(Allocator &allocator, T &&value)
Definition
tracking_allocator.hpp:39
Tests::Utils::TrackingAllocator
A minimal tracking allocator.
Definition
tracking_allocator.hpp:16
Tests::Utils::TrackingAllocator::allocate
T * allocate(std::size_t n)
Definition
tracking_allocator.hpp:21
Tests::Utils::TrackingAllocator::deallocate
void deallocate(T *ptr, std::size_t size)
Definition
tracking_allocator.hpp:26
Tests::Utils::TrackingAllocator::operator<=>
friend constexpr auto operator<=>(const TrackingAllocator &, const TrackingAllocator &) noexcept=default
Tests::Utils::TrackingAllocator::count
std::atomic< size_t > * count
Definition
tracking_allocator.hpp:19
Tests::Utils::TrackingAllocator::value_type
T value_type
Definition
tracking_allocator.hpp:17
tests
utils
tracking_allocator.hpp
Generated on
for kokkos-execution by
1.18.0