tests/util_allocator.h

Wed, 27 Dec 2023 16:04:38 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Dec 2023 16:04:38 +0100
changeset 770
ed710122af44
parent 653
e081643aae2a
permissions
-rw-r--r--

migrates self-test for testing allocator - relates to #342

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #ifndef UCX_TEST_UTIL_ALLOCATOR_H
    30 #define UCX_TEST_UTIL_ALLOCATOR_H
    32 #include "cx/allocator.h"
    34 #ifdef __cplusplus
    35 #extern "C" {
    36 #endif
    38 typedef struct CxTestingAllocator {
    39     CxAllocator base;
    40     /**
    41      * Total number of all allocations (malloc, calloc, realloc).
    42      * A realloc() does only count when the memory has to be moved.
    43      */
    44     unsigned alloc_total;
    45     /**
    46      * Number of failed allocations (malloc, calloc, realloc).
    47      */
    48     unsigned alloc_failed;
    49     /**
    50      * Total number of freed pointers.
    51      * A reallocation also counts as a free when the memory has to be moved.
    52      */
    53     unsigned free_total;
    54     /**
    55      * Number of failed free invocations.
    56      * A free() is considered failed, if it has not been performed on tracked memory.
    57      */
    58     unsigned free_failed;
    59     /**
    60      * The number of currently tracked memory blocks.
    61      */
    62     size_t tracked_count;
    63     /**
    64      * The capaciyty of the \c tracked array.
    65      */
    66     size_t tracked_capacity;
    67     /**
    68      * The set of tracked memory blocks.
    69      */
    70     void **tracked;
    71 } CxTestingAllocator;
    74 /**
    75  * Initializes a new testing allocator.
    76  */
    77 void cx_testing_allocator_init(CxTestingAllocator *alloc);
    79 /**
    80  * Destroys a testing allocator.
    81  */
    82 void cx_testing_allocator_destroy(CxTestingAllocator *alloc);
    84 /**
    85  * Verifies that this allocator has been used.
    86  *
    87  * @return true if any allocation was attempted using this allocator
    88  */
    89 bool cx_testing_allocator_used(CxTestingAllocator const *alloc);
    91 /**
    92  * Verifies that all allocated memory blocks are freed and no free occurred twice.
    93  *
    94  * @return true iff all tracked allocations / deallocations were valid
    95  */
    96 bool cx_testing_allocator_verify(CxTestingAllocator const *alloc);
    98 #ifdef __cplusplus
    99 } // extern "C"
   100 #endif
   102 #endif // UCX_TEST_UTIL_ALLOCATOR_H

mercurial