Thu, 25 Jan 2024 22:01:12 +0100
add cx_array_add() + fix type of cx_array_default_reallocator
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "util_allocator.h"
30 #include "cx/test.h"
32 static void cx_testing_allocator_track(CxTestingAllocator *alloc, void *ptr) {
33 for (size_t i = 0; i < alloc->tracked_count; i++) {
34 if (alloc->tracked[i] == ptr) return; // is already tracked
35 }
37 if (alloc->tracked_count == alloc->tracked_capacity) {
38 size_t newcapa = alloc->tracked_capacity + 64;
39 void *newarr = realloc(alloc->tracked, newcapa * sizeof(void *));
40 if (newarr == NULL) abort();
41 alloc->tracked = newarr;
42 alloc->tracked_capacity = newcapa;
43 }
45 alloc->tracked[alloc->tracked_count] = ptr;
46 alloc->tracked_count++;
47 }
49 static bool cx_testing_allocator_untrack(CxTestingAllocator *alloc, void *ptr) {
50 for (size_t i = 0; i < alloc->tracked_count; i++) {
51 if (alloc->tracked[i] == ptr) {
52 size_t last = alloc->tracked_count - 1;
53 if (i < last) {
54 alloc->tracked[i] = alloc->tracked[last];
55 }
56 alloc->tracked_count--;
57 return true;
58 }
59 }
60 return false;
61 }
63 static void *cx_malloc_testing(void *d, size_t n) {
64 CxTestingAllocator *data = d;
65 void *ptr = malloc(n);
66 data->alloc_total++;
67 if (ptr == NULL) {
68 data->alloc_failed++;
69 } else {
70 cx_testing_allocator_track(data, ptr);
71 }
72 return ptr;
73 }
75 static void *cx_realloc_testing(void *d, void *mem, size_t n) {
76 CxTestingAllocator *data = d;
77 void *ptr = realloc(mem, n);
78 if (ptr == mem) {
79 return ptr;
80 } else {
81 data->alloc_total++;
82 if (ptr == NULL) {
83 data->alloc_failed++;
84 } else {
85 data->free_total++;
86 #if !defined(__clang__) && __GNUC__ > 11
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wuse-after-free"
89 #endif
90 if (!cx_testing_allocator_untrack(data, mem)) {
91 data->free_failed++;
92 }
93 #if !defined(__clang__) && __GNUC__ > 11
94 #pragma GCC diagnostic pop
95 #endif
96 cx_testing_allocator_track(data, ptr);
97 }
98 return ptr;
99 }
100 }
102 static void *cx_calloc_testing(void *d, size_t nelem, size_t n) {
103 CxTestingAllocator *data = d;
104 void *ptr = calloc(nelem, n);
105 data->alloc_total++;
106 if (ptr == NULL) {
107 data->alloc_failed++;
108 } else {
109 cx_testing_allocator_track(data, ptr);
110 }
111 return ptr;
112 }
114 static void cx_free_testing(void *d, void *mem) {
115 CxTestingAllocator *data = d;
116 data->free_total++;
117 if (cx_testing_allocator_untrack(data, mem)) {
118 free(mem);
119 } else {
120 data->free_failed++;
121 // do not even attempt to free mem, because it is likely to segfault
122 }
123 }
125 cx_allocator_class cx_testing_allocator_class = {
126 cx_malloc_testing,
127 cx_realloc_testing,
128 cx_calloc_testing,
129 cx_free_testing
130 };
133 void cx_testing_allocator_init(CxTestingAllocator *alloc) {
134 alloc->base.cl = &cx_testing_allocator_class;
135 alloc->base.data = alloc;
136 alloc->alloc_failed = 0;
137 alloc->alloc_total = 0;
138 alloc->free_failed = 0;
139 alloc->free_total = 0;
140 size_t initial_capa = 16;
141 alloc->tracked_capacity = initial_capa;
142 alloc->tracked_count = 0;
143 alloc->tracked = calloc(sizeof(void *), initial_capa);
144 }
146 void cx_testing_allocator_destroy(CxTestingAllocator *alloc) {
147 free(alloc->tracked);
148 }
150 bool cx_testing_allocator_used(CxTestingAllocator const *alloc) {
151 return alloc->alloc_total > 0;
152 }
154 bool cx_testing_allocator_verify(CxTestingAllocator const *alloc) {
155 return alloc->tracked_count == 0 && alloc->alloc_failed == 0 && alloc->free_failed == 0
156 && alloc->alloc_total == alloc->free_total;
157 }
159 // SELF-TEST
161 CX_TEST(test_util_allocator_expect_free) {
162 CxTestingAllocator talloc;
163 cx_testing_allocator_init(&talloc);
164 CxAllocator *alloc = &talloc.base;
165 CX_TEST_DO {
166 CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc),
167 "Fresh testing allocator fails to verify.");
168 CX_TEST_ASSERTM(!cx_testing_allocator_used(&talloc),
169 "Fresh testing allocator already used.");
170 void *ptr = cxMalloc(alloc, 16);
171 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
172 "Testing allocator verifies with unfreed memory.");
173 CX_TEST_ASSERT(cx_testing_allocator_used(&talloc));
174 CX_TEST_ASSERT(ptr != NULL);
176 cxFree(alloc, ptr);
177 CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc),
178 "Testing allocator fails to verify after everything freed.");
179 }
180 cx_testing_allocator_destroy(&talloc);
181 }
183 CX_TEST(test_util_allocator_detect_double_free) {
184 CxTestingAllocator talloc;
185 cx_testing_allocator_init(&talloc);
186 CxAllocator *alloc = &talloc.base;
187 CX_TEST_DO {
188 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
189 void *ptr = cxMalloc(alloc, 16);
190 CX_TEST_ASSERT(ptr != NULL);
191 cxFree(alloc, ptr);
192 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
193 cxFree(alloc, ptr);
194 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
195 "Testing allocator does not detect double-free.");
196 }
197 cx_testing_allocator_destroy(&talloc);
198 }
200 CX_TEST(test_util_allocator_free_untracked) {
201 CxTestingAllocator talloc;
202 cx_testing_allocator_init(&talloc);
203 CxAllocator *alloc = &talloc.base;
204 void *ptr = malloc(16);
205 CX_TEST_DO {
206 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
207 cxFree(alloc, ptr);
208 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
209 "Testing allocator does not detect free of untracked memory.");
210 }
211 free(ptr);
212 cx_testing_allocator_destroy(&talloc);
213 }
215 CX_TEST(test_util_allocator_full_lifecycle_with_realloc) {
216 CxTestingAllocator talloc;
217 cx_testing_allocator_init(&talloc);
218 CxAllocator *alloc = &talloc.base;
219 CX_TEST_DO {
220 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
221 void *ptr = cxMalloc(alloc, 16);
222 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
223 CX_TEST_ASSERT(ptr != NULL);
224 CX_TEST_ASSERT(talloc.tracked_count == 1);
225 ptr = cxRealloc(alloc, ptr, 256);
226 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
227 CX_TEST_ASSERT(ptr != NULL);
228 CX_TEST_ASSERT(talloc.tracked_count == 1);
229 cxFree(alloc, ptr);
230 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
231 }
232 cx_testing_allocator_destroy(&talloc);
233 }
235 CX_TEST(test_util_allocator_calloc_initializes) {
236 CxTestingAllocator talloc;
237 cx_testing_allocator_init(&talloc);
238 CxAllocator *alloc = &talloc.base;
239 CX_TEST_DO {
240 const char zeros[16] = {0};
241 void *ptr = cxCalloc(alloc, 16, 1);
242 CX_TEST_ASSERT(memcmp(ptr, zeros, 16) == 0);
243 cxFree(alloc, ptr);
244 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
245 }
246 cx_testing_allocator_destroy(&talloc);
247 }
249 CxTestSuite *cx_test_suite_testing_allocator(void) {
250 CxTestSuite *suite = cx_test_suite_new("testing allocator self-test");
252 cx_test_register(suite, test_util_allocator_expect_free);
253 cx_test_register(suite, test_util_allocator_detect_double_free);
254 cx_test_register(suite, test_util_allocator_free_untracked);
255 cx_test_register(suite, test_util_allocator_full_lifecycle_with_realloc);
256 cx_test_register(suite, test_util_allocator_calloc_initializes);
258 return suite;
259 }