diff -r b53e0e003d7e -r ed710122af44 tests/util_allocator.h --- a/tests/util_allocator.h Wed Dec 27 14:54:04 2023 +0100 +++ b/tests/util_allocator.h Wed Dec 27 16:04:38 2023 +0100 @@ -26,56 +26,77 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#ifndef UCX_UTIL_ALLOCATOR_H -#define UCX_UTIL_ALLOCATOR_H +#ifndef UCX_TEST_UTIL_ALLOCATOR_H +#define UCX_TEST_UTIL_ALLOCATOR_H #include "cx/allocator.h" -#include +#ifdef __cplusplus +#extern "C" { +#endif -struct CxTestingAllocator : public CxAllocator { +typedef struct CxTestingAllocator { + CxAllocator base; /** * Total number of all allocations (malloc, calloc, realloc). * A realloc() does only count when the memory has to be moved. */ - unsigned alloc_total = 0; + unsigned alloc_total; /** * Number of failed allocations (malloc, calloc, realloc). */ - unsigned alloc_failed = 0; + unsigned alloc_failed; /** * Total number of freed pointers. * A reallocation also counts as a free when the memory has to be moved. */ - unsigned free_total = 0; + unsigned free_total; /** * Number of failed free invocations. * A free() is considered failed, if it has not been performed on tracked memory. */ - unsigned free_failed = 0; + unsigned free_failed; + /** + * The number of currently tracked memory blocks. + */ + size_t tracked_count; + /** + * The capaciyty of the \c tracked array. + */ + size_t tracked_capacity; /** * The set of tracked memory blocks. */ - std::set tracked; + void **tracked; +} CxTestingAllocator; - /** - * Constructs a new testing allocator. - */ - CxTestingAllocator(); - /** - * Verifies that this allocator has been used. - * - * @return true if any allocation was attempted using this allocator - */ - [[nodiscard]] bool used() const; +/** + * Initializes a new testing allocator. + */ +void cx_testing_allocator_init(CxTestingAllocator *alloc); - /** - * Verifies that all allocated memory blocks are freed and no free occurred twice. - * - * @return true iff all tracked allocations / deallocations were valid - */ - [[nodiscard]] bool verify() const; -}; +/** + * Destroys a testing allocator. + */ +void cx_testing_allocator_destroy(CxTestingAllocator *alloc); -#endif // UCX_UTIL_ALLOCATOR_H +/** + * Verifies that this allocator has been used. + * + * @return true if any allocation was attempted using this allocator + */ +bool cx_testing_allocator_used(CxTestingAllocator const *alloc); + +/** + * Verifies that all allocated memory blocks are freed and no free occurred twice. + * + * @return true iff all tracked allocations / deallocations were valid + */ +bool cx_testing_allocator_verify(CxTestingAllocator const *alloc); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // UCX_TEST_UTIL_ALLOCATOR_H