test/util_allocator.h

Sun, 26 Sep 2021 14:41:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 14:41:16 +0200
changeset 422
afd87df80b13
child 494
6ce8cfa10a96
permissions
-rw-r--r--

add utility to verify allocations

     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_UTIL_ALLOCATOR_H
    30 #define UCX_UTIL_ALLOCATOR_H
    32 #include "cx/allocator.h"
    34 #ifdef __cplusplus
    35 extern "C" {
    36 #endif
    38 #define CX_TESTING_ALLOCATOR_MAX_LIVE 1024
    40 typedef struct {
    41     /**
    42      * Total number of all allocations (malloc, calloc, realloc).
    43      * A realloc() does only count when the memory has to be moved.
    44      */
    45     int alloc_total;
    46     /**
    47      * Number of failed allocations (malloc, calloc, realloc).
    48      */
    49     int alloc_failed;
    50     /**
    51      * Total number of freed pointers.
    52      * A reallocation also counts as a free when the memory has to be moved.
    53      */
    54     int free_total;
    55     /**
    56      * Number of failed free invocations.
    57      * A free() is considered failed, if it has not been performed on tracked memory.
    58      */
    59     int free_failed;
    60     /**
    61      * Number of memory blocks that are currently live (and tracked).
    62      * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE.
    63      */
    64     int live;
    65     /**
    66      * The array of tracked memory blocks.
    67      */
    68     void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE];
    69 } cx_testing_allocator_s;
    71 extern CxAllocator cxTestingAllocator;
    73 /**
    74  * Resets the testing allocator information.
    75  * This function SHOULD be called prior to any use of this allocator.
    76  */
    77 void cxTestingAllocatorReset(void);
    79 /**
    80  * Checks whether all allocated memory is properly freed and no failed (de)allocations happened.
    81  *
    82  * @return true on success, false if there was any problem
    83  */
    84 int cxTestingAllocatorVerify(void);
    86 #ifdef __cplusplus
    87 }; /* extern "C" */
    88 #endif
    90 #endif /* UCX_UTIL_ALLOCATOR_H */

mercurial