Tue, 26 Nov 2024 21:53:06 +0100
cxBufferFree() now calls cxBufferDestroy()
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 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
38 | typedef struct CxTestingAllocator { |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
39 | CxAllocator base; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | * Total number of all allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | * 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
|
43 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
44 | unsigned alloc_total; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | * Number of failed allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
48 | unsigned alloc_failed; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | * Total number of freed pointers. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | * 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
|
52 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
53 | unsigned free_total; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | * Number of failed free invocations. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | * 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
|
57 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
58 | unsigned free_failed; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
59 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
60 | * 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
|
61 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
62 | size_t tracked_count; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
63 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
64 | * The capaciyty of the \c tracked array. |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
65 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
66 | size_t tracked_capacity; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | /** |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
68 | * The set of tracked memory blocks. |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | */ |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
70 | void **tracked; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
71 | } CxTestingAllocator; |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
72 | |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
74 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
75 | * Initializes a new testing allocator. |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
76 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
77 | 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
|
78 | |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
79 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
80 | * Destroys a testing allocator. |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
81 | */ |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
82 | void cx_testing_allocator_destroy(CxTestingAllocator *alloc); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | |
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 | * 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
|
86 | * |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
87 | * @return true if any allocation was attempted using this allocator |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
88 | */ |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
770
diff
changeset
|
89 | bool cx_testing_allocator_used(const CxTestingAllocator *alloc); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
90 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
91 | /** |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
92 | * 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
|
93 | * |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
94 | * @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
|
95 | */ |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
770
diff
changeset
|
96 | bool cx_testing_allocator_verify(const CxTestingAllocator *alloc); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | |
770
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
98 | #ifdef __cplusplus |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
99 | } // extern "C" |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
100 | #endif |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
101 | |
ed710122af44
migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
102 | #endif // UCX_TEST_UTIL_ALLOCATOR_H |