universe@422: /* universe@422: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@422: * universe@422: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@422: * universe@422: * Redistribution and use in source and binary forms, with or without universe@422: * modification, are permitted provided that the following conditions are met: universe@422: * universe@422: * 1. Redistributions of source code must retain the above copyright universe@422: * notice, this list of conditions and the following disclaimer. universe@422: * universe@422: * 2. Redistributions in binary form must reproduce the above copyright universe@422: * notice, this list of conditions and the following disclaimer in the universe@422: * documentation and/or other materials provided with the distribution. universe@422: * universe@422: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@422: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@422: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@422: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@422: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@422: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@422: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@422: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@422: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@422: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@422: * POSSIBILITY OF SUCH DAMAGE. universe@422: */ universe@422: universe@770: #ifndef UCX_TEST_UTIL_ALLOCATOR_H universe@770: #define UCX_TEST_UTIL_ALLOCATOR_H universe@422: universe@422: #include "cx/allocator.h" universe@422: universe@770: #ifdef __cplusplus universe@770: #extern "C" { universe@770: #endif universe@422: universe@770: typedef struct CxTestingAllocator { universe@770: CxAllocator base; universe@422: /** universe@422: * Total number of all allocations (malloc, calloc, realloc). universe@422: * A realloc() does only count when the memory has to be moved. universe@422: */ universe@770: unsigned alloc_total; universe@422: /** universe@422: * Number of failed allocations (malloc, calloc, realloc). universe@422: */ universe@770: unsigned alloc_failed; universe@422: /** universe@422: * Total number of freed pointers. universe@422: * A reallocation also counts as a free when the memory has to be moved. universe@422: */ universe@770: unsigned free_total; universe@422: /** universe@422: * Number of failed free invocations. universe@422: * A free() is considered failed, if it has not been performed on tracked memory. universe@422: */ universe@770: unsigned free_failed; universe@770: /** universe@770: * The number of currently tracked memory blocks. universe@770: */ universe@770: size_t tracked_count; universe@770: /** universe@770: * The capaciyty of the \c tracked array. universe@770: */ universe@770: size_t tracked_capacity; universe@422: /** universe@518: * The set of tracked memory blocks. universe@422: */ universe@770: void **tracked; universe@770: } CxTestingAllocator; universe@518: universe@422: universe@770: /** universe@770: * Initializes a new testing allocator. universe@770: */ universe@770: void cx_testing_allocator_init(CxTestingAllocator *alloc); universe@571: universe@770: /** universe@770: * Destroys a testing allocator. universe@770: */ universe@770: void cx_testing_allocator_destroy(CxTestingAllocator *alloc); universe@422: universe@770: /** universe@770: * Verifies that this allocator has been used. universe@770: * universe@770: * @return true if any allocation was attempted using this allocator universe@770: */ universe@770: bool cx_testing_allocator_used(CxTestingAllocator const *alloc); universe@770: universe@770: /** universe@770: * Verifies that all allocated memory blocks are freed and no free occurred twice. universe@770: * universe@770: * @return true iff all tracked allocations / deallocations were valid universe@770: */ universe@770: bool cx_testing_allocator_verify(CxTestingAllocator const *alloc); universe@770: universe@770: #ifdef __cplusplus universe@770: } // extern "C" universe@770: #endif universe@770: universe@770: #endif // UCX_TEST_UTIL_ALLOCATOR_H