Sat, 04 Jan 2025 13:40:34 +0100
refine docs for util_allocator.h - issue #548
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
29 | #ifndef UCX_TEST_UTIL_ALLOCATOR_H |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
30 | #define UCX_TEST_UTIL_ALLOCATOR_H |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include "cx/allocator.h" |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
34 | #ifdef __cplusplus |
988
15b3ca7ee33f
make ucx C++ compatible again (and add tests for it) - fixes #486
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
35 | extern "C" { |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
36 | #endif |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | |
1086
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
38 | /** |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
39 | * An allocator that verifies allocations / deallocations during testing. |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
40 | * |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
41 | * @par Typical Use |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
42 | * @code |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
43 | * CxTestingAllocator talloc; |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
44 | * cx_testing_allocator_init(&talloc); |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
45 | * CxAllocator *alloc = &talloc.base; |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
46 | * CX_TEST_DO { |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
47 | * // do your tests and use alloc as allocator for all API calls |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
48 | * // ... |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
49 | * // optional: check that the allocator was used |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
50 | * CX_TEST_ASSERT(cx_testing_allocator_used(&talloc)); |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
51 | * // verify |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
52 | * CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
53 | * } |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
54 | * cx_testing_allocator_destroy(&talloc); |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
55 | * @endcode |
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
56 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
57 | typedef struct CxTestingAllocator { |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
58 | CxAllocator base; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | * Total number of all allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | * A realloc() does only count when the memory has to be moved. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
63 | unsigned alloc_total; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | * Number of failed allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
67 | unsigned alloc_failed; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | * Total number of freed pointers. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | * A reallocation also counts as a free when the memory has to be moved. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
72 | unsigned free_total; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | * Number of failed free invocations. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | * A free() is considered failed, if it has not been performed on tracked memory. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
77 | unsigned free_failed; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
78 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
79 | * The number of currently tracked memory blocks. |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
80 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
81 | size_t tracked_count; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
82 | /** |
1086
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
83 | * The capaciyty of the @c tracked array. |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
84 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
85 | size_t tracked_capacity; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | /** |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
87 | * The set of tracked memory blocks. |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
89 | void **tracked; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
90 | } CxTestingAllocator; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
91 | |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
93 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
94 | * Initializes a new testing allocator. |
1086
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
95 | * @see cx_testing_allocator_destroy() |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
96 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
97 | void cx_testing_allocator_init(CxTestingAllocator *alloc); |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
98 | |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
99 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
100 | * Destroys a testing allocator. |
1086
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
101 | * @see cx_testing_allocator_init() |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
102 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
103 | void cx_testing_allocator_destroy(CxTestingAllocator *alloc); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
105 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
106 | * Verifies that this allocator has been used. |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
107 | * |
1086
4c6ea8a10acd
refine docs for util_allocator.h - issue #548
Mike Becker <universe@uap-core.de>
parents:
988
diff
changeset
|
108 | * @return true iff any allocation was attempted using this allocator |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
109 | */ |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
770
diff
changeset
|
110 | bool cx_testing_allocator_used(const CxTestingAllocator *alloc); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
111 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
112 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
113 | * Verifies that all allocated memory blocks are freed and no free occurred twice. |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
114 | * |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
115 | * @return true iff all tracked allocations / deallocations were valid |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
116 | */ |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
770
diff
changeset
|
117 | bool cx_testing_allocator_verify(const CxTestingAllocator *alloc); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
119 | #ifdef __cplusplus |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
120 | } // extern "C" |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
121 | #endif |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
122 | |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
123 | #endif // UCX_TEST_UTIL_ALLOCATOR_H |