test/util_allocator.h

changeset 518
74d0372f5c6f
parent 500
eb9e7bd40a8e
child 571
f83583a0bbac
     1.1 --- a/test/util_allocator.h	Sat Apr 16 18:02:10 2022 +0200
     1.2 +++ b/test/util_allocator.h	Sat Apr 16 20:17:01 2022 +0200
     1.3 @@ -31,60 +31,44 @@
     1.4  
     1.5  #include "cx/allocator.h"
     1.6  
     1.7 -#ifdef __cplusplus
     1.8 -extern "C" {
     1.9 -#endif
    1.10 +#include <set>
    1.11  
    1.12 -#define CX_TESTING_ALLOCATOR_MAX_LIVE 1024
    1.13 -
    1.14 -typedef struct {
    1.15 +struct CxTestingAllocator : public CxAllocator {
    1.16      /**
    1.17       * Total number of all allocations (malloc, calloc, realloc).
    1.18       * A realloc() does only count when the memory has to be moved.
    1.19       */
    1.20 -    int alloc_total;
    1.21 +    unsigned alloc_total = 0;
    1.22      /**
    1.23       * Number of failed allocations (malloc, calloc, realloc).
    1.24       */
    1.25 -    int alloc_failed;
    1.26 +    unsigned alloc_failed = 0;
    1.27      /**
    1.28       * Total number of freed pointers.
    1.29       * A reallocation also counts as a free when the memory has to be moved.
    1.30       */
    1.31 -    int free_total;
    1.32 +    unsigned free_total = 0;
    1.33      /**
    1.34       * Number of failed free invocations.
    1.35       * A free() is considered failed, if it has not been performed on tracked memory.
    1.36       */
    1.37 -    int free_failed;
    1.38 +    unsigned free_failed = 0;
    1.39      /**
    1.40 -     * Number of memory blocks that are currently live (and tracked).
    1.41 -     * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE.
    1.42 +     * The set of tracked memory blocks.
    1.43       */
    1.44 -    int live;
    1.45 +    std::set<void *> tracked;
    1.46 +
    1.47      /**
    1.48 -     * The array of tracked memory blocks.
    1.49 +     * Constructs a new testing allocator.
    1.50       */
    1.51 -    void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE];
    1.52 -} cx_testing_allocator_s;
    1.53 +    CxTestingAllocator();
    1.54  
    1.55 -extern CxAllocator *cxTestingAllocator;
    1.56 -
    1.57 -/**
    1.58 - * Resets the testing allocator information.
    1.59 - * This function SHOULD be called prior to any use of this allocator.
    1.60 - */
    1.61 -void cxTestingAllocatorReset(void);
    1.62 -
    1.63 -/**
    1.64 - * Checks whether all allocated memory is properly freed and no failed (de)allocations happened.
    1.65 - *
    1.66 - * @return true on success, false if there was any problem
    1.67 - */
    1.68 -bool cxTestingAllocatorVerify(void);
    1.69 -
    1.70 -#ifdef __cplusplus
    1.71 -}; /* extern "C" */
    1.72 -#endif
    1.73 +    /**
    1.74 +     * Verifies that all allocated memory blocks are freed and no free occurred twice.
    1.75 +     *
    1.76 +     * @return true iff all tracked allocations / deallocations were valid
    1.77 +     */
    1.78 +    bool verify() const;
    1.79 +};
    1.80  
    1.81  #endif /* UCX_UTIL_ALLOCATOR_H */

mercurial