Wed, 28 Jun 2023 20:07:52 +0200
improve mempool implementation
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 | #include "util_allocator.h" |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | void *cx_malloc_testing(void *d, size_t n) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
32 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | void *ptr = malloc(n); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | data->alloc_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
35 | if (ptr == nullptr) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | data->alloc_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | } else { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
38 | data->tracked.insert(ptr); |
422
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 | return ptr; |
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 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | void *cx_realloc_testing(void *d, void *mem, size_t n) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
44 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | void *ptr = realloc(mem, n); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | if (ptr == mem) { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | return ptr; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | } else { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | data->alloc_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
50 | if (ptr == nullptr) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | data->alloc_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | } else { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | data->free_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
54 | if (data->tracked.erase(mem) == 0) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | data->free_failed++; |
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 | data->tracked.insert(ptr); |
422
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 | return ptr; |
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 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | void *cx_calloc_testing(void *d, size_t nelem, size_t n) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
64 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | void *ptr = calloc(nelem, n); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | data->alloc_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
67 | if (ptr == nullptr) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | data->alloc_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | } else { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
70 | data->tracked.insert(ptr); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | return ptr; |
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 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | void cx_free_testing(void *d, void *mem) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
76 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | data->free_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
78 | if (data->tracked.erase(mem) == 0) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | data->free_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | // do not even attempt to free mem, because it is likely to segfault |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | } else { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | free(mem); |
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 | } |
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 | cx_allocator_class cx_testing_allocator_class = { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | cx_malloc_testing, |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | cx_realloc_testing, |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | cx_calloc_testing, |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | cx_free_testing |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | }; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
93 | CxTestingAllocator::CxTestingAllocator() : CxAllocator() { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
94 | cl = &cx_testing_allocator_class; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
95 | data = this; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
96 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
97 | |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
98 | bool CxTestingAllocator::used() const { |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
99 | return alloc_total > 0; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
100 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
101 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
102 | bool CxTestingAllocator::verify() const { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
103 | return tracked.empty() && alloc_failed == 0 && free_failed == 0 && alloc_total == free_total; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
104 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
105 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
106 | // SELF-TEST |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
108 | #include <gtest/gtest.h> |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
109 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
110 | TEST(TestingAllocator, ExpectFree) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
111 | CxTestingAllocator allocator; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
113 | ASSERT_TRUE(allocator.verify()); |
642
98c90759f69e
minor fix for testing allocator self-test
Mike Becker <universe@uap-core.de>
parents:
571
diff
changeset
|
114 | EXPECT_FALSE(allocator.used()); |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
115 | auto ptr = cxMalloc(&allocator, 16); |
642
98c90759f69e
minor fix for testing allocator self-test
Mike Becker <universe@uap-core.de>
parents:
571
diff
changeset
|
116 | EXPECT_TRUE(allocator.used()); |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
117 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
118 | EXPECT_FALSE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
119 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
120 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
121 | EXPECT_TRUE(allocator.verify()); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
124 | TEST(TestingAllocator, DetectDoubleFree) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
125 | CxTestingAllocator allocator; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
126 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
127 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
128 | auto ptr = cxMalloc(&allocator, 16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
129 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
130 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
131 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
132 | EXPECT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
133 | ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr)); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
134 | EXPECT_FALSE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
135 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
136 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
137 | TEST(TestingAllocator, FreeUntracked) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
138 | CxTestingAllocator allocator; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
139 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
140 | auto ptr = malloc(16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
141 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
142 | ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr)); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
143 | EXPECT_FALSE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
144 | ASSERT_NO_FATAL_FAILURE(free(ptr)); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
145 | } |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
146 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
147 | TEST(TestingAllocator, FullLifecycleWithRealloc) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
148 | CxTestingAllocator allocator; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
149 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
150 | auto ptr = cxMalloc(&allocator, 16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
151 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
152 | EXPECT_EQ(allocator.tracked.size(), 1); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
153 | ptr = cxRealloc(&allocator, ptr, 256); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
154 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
155 | EXPECT_EQ(allocator.tracked.size(), 1); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
156 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
157 | EXPECT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
158 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
159 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
160 | TEST(TestingAllocator, CallocInitializes) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
161 | CxTestingAllocator allocator; |
642
98c90759f69e
minor fix for testing allocator self-test
Mike Becker <universe@uap-core.de>
parents:
571
diff
changeset
|
162 | const char zeros[16] = {0}; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
163 | auto ptr = cxCalloc(&allocator, 16, 1); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
164 | EXPECT_EQ(memcmp(ptr, zeros, 16), 0); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
165 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
166 | EXPECT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
167 | } |