Coverage Report

Created: 2026-10-19 14:15

src/zserio/pmr/MemoryResource.h
Line
Count
Source
1
#ifndef ZSERIO_PMR_I_MEMORY_RESOURCE_H_INC
2
#define ZSERIO_PMR_I_MEMORY_RESOURCE_H_INC
3
4
#ifdef ZSERIO_MEMORY_RESOURCE_TRACING
5
    #include <iostream>
6
#endif
7
8
#include "zserio/Types.h"
9
10
namespace zserio
11
{
12
namespace pmr
13
{
14
15
/**
16
 * Abstract base class for memory resources which are to be used by polymorphic allocators defined by zserio.
17
 */
18
class MemoryResource
19
{
20
public:
21
    // this empty constructor is necessary for gcc 9.3.0 bug which causes test coverage failure
22
    /**
23
     * Constructor.
24
     */
25
25
    MemoryResource() = default;
26
27
    /**
28
     * Destructor.
29
     */
30
26
    virtual ~MemoryResource() = default;
31
32
    /**
33
     * Copying and moving is disallowed!
34
     * \{
35
     */
36
    MemoryResource(const MemoryResource& other) = delete;
37
    MemoryResource(MemoryResource&& other) = delete;
38
39
    MemoryResource& operator=(const MemoryResource& other) = delete;
40
    MemoryResource& operator=(MemoryResource&& other) = delete;
41
    /** \} */
42
43
    /**
44
     * Allocates storage with a size of at least bytes bytes, aligned to the specified alignment.
45
     *
46
     * \param bytes Minimum number of bytes to allocate.
47
     * \param alignment Requested alignment.
48
     *
49
     * \return Pointer to the allocated storage.
50
     */
51
    void* allocate(size_t bytes, size_t alignment = alignof(max_align_t))
52
197
    {
53
#ifdef ZSERIO_MEMORY_RESOURCE_TRACING
54
        void* const ptr = doAllocate(bytes, alignment);
55
        std::cout << "MemoryResource::allocate bytes=" << bytes << " alignment=" << alignment << " -> "
56
                  << std::hex << ptr << std::dec << std::endl;
57
        return ptr;
58
#else
59
197
        return doAllocate(bytes, alignment);
60
197
#endif
61
197
    }
62
63
    /**
64
     * Deallocates the storage pointed to by p.
65
     *
66
     * Arguments shall match to prior call to allocate!
67
     *
68
     * \param storage Pointer to the storage to deallocate.
69
     * \param bytes Number of bytes to deallocate.
70
     * \param alignment Requested alignment.
71
     */
72
    void deallocate(void* storage, size_t bytes, size_t alignment = alignof(max_align_t))
73
197
    {
74
#ifdef ZSERIO_MEMORY_RESOURCE_TRACING
75
        std::cout << "MemoryResource::deallocate p=" << std::hex << storage << std::dec << " bytes=" << bytes
76
                  << " alignment=" << alignment << std::endl;
77
#endif
78
197
        doDeallocate(storage, bytes, alignment);
79
197
    }
80
81
    /**
82
     * Compares *this for equality with other.
83
     *
84
     * Two memory resources  compare equal if and only if memory allocated from one memory resource
85
     * can be deallocated from the other and vice versa.
86
     *
87
     * \param other Other memory resource to compare.
88
     *
89
     * \return True when the two resources are equal, false otherwise.
90
     */
91
    bool isEqual(const MemoryResource& other) const noexcept
92
13
    {
93
#ifdef ZSERIO_MEMORY_RESOURCE_TRACING
94
        std::cout << "MemoryResource::isEqual other=" << std::hex << &other << std::dec << std::endl;
95
#endif
96
13
        return doIsEqual(other);
97
13
    }
98
99
private:
100
    /**
101
     * Allocates storage with a size of at least bytes bytes, aligned to the specified alignment.
102
     *
103
     * \param bytes Minimum number of bytes to allocate.
104
     * \param alignment Requested alignment.
105
     *
106
     * \return Pointer to the allocated storage.
107
     */
108
    virtual void* doAllocate(size_t bytes, size_t alignment) = 0;
109
110
    /**
111
     * Deallocates the storage pointed to by storage.
112
     *
113
     * Arguments shall match to prior call to doAllocate!
114
     *
115
     * \param storage Pointer to the storage to deallocate.
116
     * \param bytes Number of bytes to deallocate.
117
     * \param alignment Requested alignment.
118
     */
119
    virtual void doDeallocate(void* storage, size_t bytes, size_t alignment) = 0;
120
121
    /**
122
     * Compares *this for equality with other.
123
     *
124
     * Two memory resources  compare equal if and only if memory allocated from one memory resource
125
     * can be deallocated from the other and vice versa.
126
     *
127
     * \param other Other memory resource to compare.
128
     *
129
     * \return True when the two resources are equal, false otherwise.
130
     */
131
    virtual bool doIsEqual(const MemoryResource& other) const noexcept = 0;
132
};
133
134
/**
135
 * Compares the memory resources lhs and rhs for equality.
136
 *
137
 * \param lhs First memory resource to compare.
138
 * \param rhs Second memory resource to compare.
139
 *
140
 * \return True when the two resources are equal, false otherwise.
141
 */
142
inline bool operator==(const MemoryResource& lhs, const MemoryResource& rhs)
143
74
{
144
74
    return &lhs == &rhs || 
lhs.isEqual(rhs)8
;
145
74
}
146
147
/**
148
 * Compares the memory resources lhs and rhs for non-equality.
149
 *
150
 * \param lhs First memory resource to compare.
151
 * \param rhs Second memory resource to compare.
152
 *
153
 * \return True when the two resources are NOT equal, false otherwise.
154
 */
155
inline bool operator!=(const MemoryResource& lhs, const MemoryResource& rhs)
156
3
{
157
3
    return !(lhs == rhs);
158
3
}
159
160
/**
161
 * Returns default memory resource, which is the resource set by previous call to
162
 * setDefaultResource, or zserio::pmr::NewDeleteResource if no call to setDefaultResource
163
 * was done.
164
 *
165
 * \warning This function does not synchronize with setDefaultResource.
166
 *
167
 * \return Default memory resource.
168
 */
169
MemoryResource* getDefaultResource() noexcept;
170
171
/**
172
 * If resource is not null, sets the default memory resource pointer to resource,
173
 * otherwise, sets the default memory resource pointer to zserio::pmr::NewDeleteResource.
174
 * All subsequent calls to getDefaultResource() returns resource set by this function.
175
 *
176
 * \warning This function does not synchronize with getDefaultResource or other calls to setDefaultResource.
177
 * It is undefined behavior to call setDefaultResource together with getDefaultResource concurrently.
178
 *
179
 * \param resource Resource to be set as default, or nullptr to use zserio::pmr::NewDeleteResource.
180
 *
181
 * \return Previous default resource.
182
 */
183
MemoryResource* setDefaultResource(MemoryResource* resource) noexcept;
184
185
} // namespace pmr
186
} // namespace zserio
187
188
#endif // ZSERIO_PMR_I_MEMORY_RESOURCE_H_INC