Mon, 20 Dec 2021 11:58:36 +0100
add more nonnull attributes
This also changes the contract for last/first in the sense that these
functions now also require a valid pointer.
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 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #ifndef UCX_UTIL_ALLOCATOR_H |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #define UCX_UTIL_ALLOCATOR_H |
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 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | #ifdef __cplusplus |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | extern "C" { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | #endif |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | #define CX_TESTING_ALLOCATOR_MAX_LIVE 1024 |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | typedef struct { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | * Total number of all allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | * 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
|
44 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | int alloc_total; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | * Number of failed allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | int alloc_failed; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | * Total number of freed pointers. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | * 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
|
53 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | int free_total; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | * Number of failed free invocations. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | * 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
|
58 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | int free_failed; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | * Number of memory blocks that are currently live (and tracked). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | int live; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | * The array of tracked memory blocks. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE]; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | } cx_testing_allocator_s; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | extern CxAllocator cxTestingAllocator; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | |
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 | * Resets the testing allocator information. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | * This function SHOULD be called prior to any use of this allocator. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | void cxTestingAllocatorReset(void); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | * Checks whether all allocated memory is properly freed and no failed (de)allocations happened. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | * |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | * @return true on success, false if there was any problem |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | int cxTestingAllocatorVerify(void); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | #ifdef __cplusplus |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | }; /* extern "C" */ |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | #endif |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | #endif /* UCX_UTIL_ALLOCATOR_H */ |