Sun, 24 Apr 2022 17:45:09 +0200
#170 add several more buffer tests
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 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
34 | #include <set> |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
36 | struct CxTestingAllocator : public CxAllocator { |
422
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 | * Total number of all allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | * 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
|
40 | */ |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
41 | unsigned alloc_total = 0; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | * Number of failed allocations (malloc, calloc, realloc). |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | */ |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
45 | unsigned alloc_failed = 0; |
422
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 | * Total number of freed pointers. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | * 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
|
49 | */ |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
50 | unsigned free_total = 0; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | /** |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | * Number of failed free invocations. |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | * 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
|
54 | */ |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
55 | unsigned free_failed = 0; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | /** |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
57 | * The set of tracked memory blocks. |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | */ |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
59 | std::set<void *> tracked; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
61 | /** |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
62 | * Constructs a new testing allocator. |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
63 | */ |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
64 | CxTestingAllocator(); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
66 | /** |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
67 | * Verifies that all allocated memory blocks are freed and no free occurred twice. |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
68 | * |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
69 | * @return true iff all tracked allocations / deallocations were valid |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
70 | */ |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
71 | bool verify() const; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
72 | }; |
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 | #endif /* UCX_UTIL_ALLOCATOR_H */ |