diff -r b3baaf9b7e3c -r 74d0372f5c6f test/util_allocator.h --- a/test/util_allocator.h Sat Apr 16 18:02:10 2022 +0200 +++ b/test/util_allocator.h Sat Apr 16 20:17:01 2022 +0200 @@ -31,60 +31,44 @@ #include "cx/allocator.h" -#ifdef __cplusplus -extern "C" { -#endif +#include -#define CX_TESTING_ALLOCATOR_MAX_LIVE 1024 - -typedef struct { +struct CxTestingAllocator : public CxAllocator { /** * Total number of all allocations (malloc, calloc, realloc). * A realloc() does only count when the memory has to be moved. */ - int alloc_total; + unsigned alloc_total = 0; /** * Number of failed allocations (malloc, calloc, realloc). */ - int alloc_failed; + unsigned alloc_failed = 0; /** * Total number of freed pointers. * A reallocation also counts as a free when the memory has to be moved. */ - int free_total; + unsigned free_total = 0; /** * Number of failed free invocations. * A free() is considered failed, if it has not been performed on tracked memory. */ - int free_failed; + unsigned free_failed = 0; /** - * Number of memory blocks that are currently live (and tracked). - * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE. + * The set of tracked memory blocks. */ - int live; + std::set tracked; + /** - * The array of tracked memory blocks. + * Constructs a new testing allocator. */ - void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE]; -} cx_testing_allocator_s; + CxTestingAllocator(); -extern CxAllocator *cxTestingAllocator; - -/** - * Resets the testing allocator information. - * This function SHOULD be called prior to any use of this allocator. - */ -void cxTestingAllocatorReset(void); - -/** - * Checks whether all allocated memory is properly freed and no failed (de)allocations happened. - * - * @return true on success, false if there was any problem - */ -bool cxTestingAllocatorVerify(void); - -#ifdef __cplusplus -}; /* extern "C" */ -#endif + /** + * Verifies that all allocated memory blocks are freed and no free occurred twice. + * + * @return true iff all tracked allocations / deallocations were valid + */ + bool verify() const; +}; #endif /* UCX_UTIL_ALLOCATOR_H */