tests/util_allocator.h

changeset 770
ed710122af44
parent 653
e081643aae2a
     1.1 --- a/tests/util_allocator.h	Wed Dec 27 14:54:04 2023 +0100
     1.2 +++ b/tests/util_allocator.h	Wed Dec 27 16:04:38 2023 +0100
     1.3 @@ -26,56 +26,77 @@
     1.4   * POSSIBILITY OF SUCH DAMAGE.
     1.5   */
     1.6  
     1.7 -#ifndef UCX_UTIL_ALLOCATOR_H
     1.8 -#define UCX_UTIL_ALLOCATOR_H
     1.9 +#ifndef UCX_TEST_UTIL_ALLOCATOR_H
    1.10 +#define UCX_TEST_UTIL_ALLOCATOR_H
    1.11  
    1.12  #include "cx/allocator.h"
    1.13  
    1.14 -#include <set>
    1.15 +#ifdef __cplusplus
    1.16 +#extern "C" {
    1.17 +#endif
    1.18  
    1.19 -struct CxTestingAllocator : public CxAllocator {
    1.20 +typedef struct CxTestingAllocator {
    1.21 +    CxAllocator base;
    1.22      /**
    1.23       * Total number of all allocations (malloc, calloc, realloc).
    1.24       * A realloc() does only count when the memory has to be moved.
    1.25       */
    1.26 -    unsigned alloc_total = 0;
    1.27 +    unsigned alloc_total;
    1.28      /**
    1.29       * Number of failed allocations (malloc, calloc, realloc).
    1.30       */
    1.31 -    unsigned alloc_failed = 0;
    1.32 +    unsigned alloc_failed;
    1.33      /**
    1.34       * Total number of freed pointers.
    1.35       * A reallocation also counts as a free when the memory has to be moved.
    1.36       */
    1.37 -    unsigned free_total = 0;
    1.38 +    unsigned free_total;
    1.39      /**
    1.40       * Number of failed free invocations.
    1.41       * A free() is considered failed, if it has not been performed on tracked memory.
    1.42       */
    1.43 -    unsigned free_failed = 0;
    1.44 +    unsigned free_failed;
    1.45 +    /**
    1.46 +     * The number of currently tracked memory blocks.
    1.47 +     */
    1.48 +    size_t tracked_count;
    1.49 +    /**
    1.50 +     * The capaciyty of the \c tracked array.
    1.51 +     */
    1.52 +    size_t tracked_capacity;
    1.53      /**
    1.54       * The set of tracked memory blocks.
    1.55       */
    1.56 -    std::set<void *> tracked;
    1.57 +    void **tracked;
    1.58 +} CxTestingAllocator;
    1.59  
    1.60 -    /**
    1.61 -     * Constructs a new testing allocator.
    1.62 -     */
    1.63 -    CxTestingAllocator();
    1.64  
    1.65 -    /**
    1.66 -     * Verifies that this allocator has been used.
    1.67 -     *
    1.68 -     * @return true if any allocation was attempted using this allocator
    1.69 -     */
    1.70 -    [[nodiscard]] bool used() const;
    1.71 +/**
    1.72 + * Initializes a new testing allocator.
    1.73 + */
    1.74 +void cx_testing_allocator_init(CxTestingAllocator *alloc);
    1.75  
    1.76 -    /**
    1.77 -     * Verifies that all allocated memory blocks are freed and no free occurred twice.
    1.78 -     *
    1.79 -     * @return true iff all tracked allocations / deallocations were valid
    1.80 -     */
    1.81 -    [[nodiscard]] bool verify() const;
    1.82 -};
    1.83 +/**
    1.84 + * Destroys a testing allocator.
    1.85 + */
    1.86 +void cx_testing_allocator_destroy(CxTestingAllocator *alloc);
    1.87  
    1.88 -#endif // UCX_UTIL_ALLOCATOR_H
    1.89 +/**
    1.90 + * Verifies that this allocator has been used.
    1.91 + *
    1.92 + * @return true if any allocation was attempted using this allocator
    1.93 + */
    1.94 +bool cx_testing_allocator_used(CxTestingAllocator const *alloc);
    1.95 +
    1.96 +/**
    1.97 + * Verifies that all allocated memory blocks are freed and no free occurred twice.
    1.98 + *
    1.99 + * @return true iff all tracked allocations / deallocations were valid
   1.100 + */
   1.101 +bool cx_testing_allocator_verify(CxTestingAllocator const *alloc);
   1.102 +
   1.103 +#ifdef __cplusplus
   1.104 +} // extern "C"
   1.105 +#endif
   1.106 +
   1.107 +#endif // UCX_TEST_UTIL_ALLOCATOR_H

mercurial