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