kokkos-execution 0.0.1
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
8namespace Tests::Utils {
9
15template <typename T>
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
38template <typename Allocator, typename T>
39auto 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
auto round_trip_allocate(Allocator &allocator, T &&value)
A minimal tracking allocator.
void deallocate(T *ptr, std::size_t size)
friend constexpr auto operator<=>(const TrackingAllocator &, const TrackingAllocator &) noexcept=default