Coverage Report

Created: 2026-10-19 14:15

test/zserio/TrackingAllocator.h
Line
Count
Source
1
#ifndef ZSERIO_TRACKING_ALLOCATOR_H_INC
2
#define ZSERIO_TRACKING_ALLOCATOR_H_INC
3
4
#include <memory>
5
#include <set>
6
7
#include "gtest/gtest.h"
8
#include "zserio/Types.h"
9
10
namespace zserio
11
{
12
13
namespace detail
14
{
15
16
class AllocationTracker
17
{
18
public:
19
234
    AllocationTracker() = default;
20
    ~AllocationTracker()
21
234
    {
22
234
        EXPECT_TRUE(m_allocations.empty());
23
234
    }
24
25
    AllocationTracker(const AllocationTracker&) = default;
26
    AllocationTracker& operator=(const AllocationTracker&) = default;
27
28
    AllocationTracker(AllocationTracker&&) = default;
29
    AllocationTracker& operator=(AllocationTracker&&) = default;
30
31
    void allocated(void* ptr)
32
217
    {
33
217
        m_allocations.insert(ptr);
34
217
    }
35
36
    void deallocated(void* ptr)
37
217
    {
38
217
        EXPECT_TRUE(m_allocations.count(ptr) > 0);
39
217
        m_allocations.erase(ptr);
40
217
    }
41
42
    size_t numAllocs() const
43
253
    {
44
253
        return m_allocations.size();
45
253
    }
46
47
private:
48
    std::set<void*> m_allocations;
49
};
50
51
} // namespace detail
52
53
/**
54
 * Stateful allocator that tracks the allocations. Used in unit tests.
55
 * Every copy of an allocator must be able to deallocate what any of the other copies
56
 * allocated. This allocator simulates this behavior, all copies shares the same tracker,
57
 * so it can check if the allocators are used correctly (ie. no allocations leaks,
58
 * every allocation is deallocated by the same allocator or its copy).
59
 */
60
template <typename T>
61
class TrackingAllocatorImpl
62
{
63
public:
64
    using value_type = typename std::allocator<T>::value_type;
65
    using pointer = typename std::allocator_traits<std::allocator<T>>::pointer;
66
67
    TrackingAllocatorImpl() :
68
            m_tracker(std::make_shared<detail::AllocationTracker>())
69
234
    {}
70
1.59k
    ~TrackingAllocatorImpl() = default;
71
72
1.03k
    TrackingAllocatorImpl(const TrackingAllocatorImpl&) = default;
73
49
    TrackingAllocatorImpl& operator=(const TrackingAllocatorImpl&) = default;
74
75
    TrackingAllocatorImpl(TrackingAllocatorImpl&&) = delete;
76
    TrackingAllocatorImpl& operator=(TrackingAllocatorImpl&&) = delete;
77
78
    template <typename Other>
79
    TrackingAllocatorImpl(const TrackingAllocatorImpl<Other>& other) :
80
            m_allocator(other.m_allocator),
81
            m_tracker(other.m_tracker)
82
331
    {}
83
84
    value_type* allocate(size_t size)
85
217
    {
86
217
        const auto ptr = m_allocator.allocate(size);
87
217
        m_tracker->allocated(ptr);
88
217
        return ptr;
89
217
    }
90
91
    void deallocate(value_type* memory, size_t size) noexcept
92
217
    {
93
217
        m_tracker->deallocated(memory);
94
217
        m_allocator.deallocate(memory, size);
95
217
    }
96
97
    bool operator==(const TrackingAllocatorImpl& other) const
98
101
    {
99
101
        return std::tie(m_tracker, m_allocator) == std::tie(other.m_tracker, other.m_allocator);
100
101
    }
101
102
    bool operator!=(const TrackingAllocatorImpl& other) const
103
7
    {
104
7
        return !(*this == other);
105
7
    }
106
107
    size_t numAllocs() const
108
253
    {
109
253
        return m_tracker->numAllocs();
110
253
    }
111
112
    template <typename U>
113
    friend class TrackingAllocatorImpl;
114
115
private:
116
    std::allocator<T> m_allocator;
117
    std::shared_ptr<detail::AllocationTracker> m_tracker;
118
};
119
120
/**
121
 * Propagating allocator. Ie. container copy/move propagates also
122
 * the allocator.
123
 */
124
template <typename T>
125
class TrackingAllocator : public TrackingAllocatorImpl<T>
126
{
127
public:
128
    using propagate_on_container_copy_assignment = std::true_type;
129
    using propagate_on_container_move_assignment = std::true_type;
130
131
    using TrackingAllocatorImpl<T>::TrackingAllocatorImpl;
132
133
    TrackingAllocator select_on_container_copy_construction() const
134
30
    {
135
30
        return *this;
136
30
    }
137
};
138
139
/**
140
 * Non-propagating allocator. Ie. container copy/move does not propagate
141
 * the allocator.
142
 */
143
template <typename T>
144
class TrackingAllocatorNonProp : public TrackingAllocatorImpl<T>
145
{
146
public:
147
    using propagate_on_container_copy_assignment = std::false_type;
148
    using propagate_on_container_move_assignment = std::false_type;
149
150
    using TrackingAllocatorImpl<T>::TrackingAllocatorImpl;
151
152
    TrackingAllocatorNonProp select_on_container_copy_construction() const
153
27
    {
154
27
        return TrackingAllocatorNonProp();
155
27
    }
156
};
157
158
} // namespace zserio
159
160
#endif // ZSERIO_TRACKING_ALLOCATOR_H_INC