24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #ifndef UCX_UTIL_ALLOCATOR_H |
29 #ifndef UCX_TEST_UTIL_ALLOCATOR_H |
30 #define UCX_UTIL_ALLOCATOR_H |
30 #define UCX_TEST_UTIL_ALLOCATOR_H |
31 |
31 |
32 #include "cx/allocator.h" |
32 #include "cx/allocator.h" |
33 |
33 |
34 #include <set> |
34 #ifdef __cplusplus |
|
35 #extern "C" { |
|
36 #endif |
35 |
37 |
36 struct CxTestingAllocator : public CxAllocator { |
38 typedef struct CxTestingAllocator { |
|
39 CxAllocator base; |
37 /** |
40 /** |
38 * Total number of all allocations (malloc, calloc, realloc). |
41 * Total number of all allocations (malloc, calloc, realloc). |
39 * A realloc() does only count when the memory has to be moved. |
42 * A realloc() does only count when the memory has to be moved. |
40 */ |
43 */ |
41 unsigned alloc_total = 0; |
44 unsigned alloc_total; |
42 /** |
45 /** |
43 * Number of failed allocations (malloc, calloc, realloc). |
46 * Number of failed allocations (malloc, calloc, realloc). |
44 */ |
47 */ |
45 unsigned alloc_failed = 0; |
48 unsigned alloc_failed; |
46 /** |
49 /** |
47 * Total number of freed pointers. |
50 * Total number of freed pointers. |
48 * A reallocation also counts as a free when the memory has to be moved. |
51 * A reallocation also counts as a free when the memory has to be moved. |
49 */ |
52 */ |
50 unsigned free_total = 0; |
53 unsigned free_total; |
51 /** |
54 /** |
52 * Number of failed free invocations. |
55 * Number of failed free invocations. |
53 * A free() is considered failed, if it has not been performed on tracked memory. |
56 * A free() is considered failed, if it has not been performed on tracked memory. |
54 */ |
57 */ |
55 unsigned free_failed = 0; |
58 unsigned free_failed; |
|
59 /** |
|
60 * The number of currently tracked memory blocks. |
|
61 */ |
|
62 size_t tracked_count; |
|
63 /** |
|
64 * The capaciyty of the \c tracked array. |
|
65 */ |
|
66 size_t tracked_capacity; |
56 /** |
67 /** |
57 * The set of tracked memory blocks. |
68 * The set of tracked memory blocks. |
58 */ |
69 */ |
59 std::set<void *> tracked; |
70 void **tracked; |
|
71 } CxTestingAllocator; |
60 |
72 |
61 /** |
|
62 * Constructs a new testing allocator. |
|
63 */ |
|
64 CxTestingAllocator(); |
|
65 |
73 |
66 /** |
74 /** |
67 * Verifies that this allocator has been used. |
75 * Initializes a new testing allocator. |
68 * |
76 */ |
69 * @return true if any allocation was attempted using this allocator |
77 void cx_testing_allocator_init(CxTestingAllocator *alloc); |
70 */ |
|
71 [[nodiscard]] bool used() const; |
|
72 |
78 |
73 /** |
79 /** |
74 * Verifies that all allocated memory blocks are freed and no free occurred twice. |
80 * Destroys a testing allocator. |
75 * |
81 */ |
76 * @return true iff all tracked allocations / deallocations were valid |
82 void cx_testing_allocator_destroy(CxTestingAllocator *alloc); |
77 */ |
|
78 [[nodiscard]] bool verify() const; |
|
79 }; |
|
80 |
83 |
81 #endif // UCX_UTIL_ALLOCATOR_H |
84 /** |
|
85 * Verifies that this allocator has been used. |
|
86 * |
|
87 * @return true if any allocation was attempted using this allocator |
|
88 */ |
|
89 bool cx_testing_allocator_used(CxTestingAllocator const *alloc); |
|
90 |
|
91 /** |
|
92 * Verifies that all allocated memory blocks are freed and no free occurred twice. |
|
93 * |
|
94 * @return true iff all tracked allocations / deallocations were valid |
|
95 */ |
|
96 bool cx_testing_allocator_verify(CxTestingAllocator const *alloc); |
|
97 |
|
98 #ifdef __cplusplus |
|
99 } // extern "C" |
|
100 #endif |
|
101 |
|
102 #endif // UCX_TEST_UTIL_ALLOCATOR_H |