test/util_allocator.c

changeset 422
afd87df80b13
child 494
6ce8cfa10a96
equal deleted inserted replaced
421:aa465fac4ef6 422:afd87df80b13
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 */
28
29 #include "util_allocator.h"
30 #include <string.h>
31
32 void cx_testing_allocator_add(cx_testing_allocator_s *data, void *ptr) {
33 data->tracked[data->live] = ptr;
34 data->live++;
35 }
36
37 int cx_testing_allocator_remove(cx_testing_allocator_s *data, void *ptr) {
38 for (int i = 0; i < data->live; i++) {
39 if (data->tracked[i] == ptr) {
40 data->tracked[i] = data->tracked[data->live - 1];
41 data->live--;
42 return 0;
43 }
44 }
45 return 1;
46 }
47
48 void *cx_malloc_testing(void *d, size_t n) {
49 cx_testing_allocator_s *data = d;
50 void *ptr = malloc(n);
51 data->alloc_total++;
52 if (ptr == NULL) {
53 data->alloc_failed++;
54 } else {
55 cx_testing_allocator_add(data, ptr);
56 }
57 return ptr;
58 }
59
60 void *cx_realloc_testing(void *d, void *mem, size_t n) {
61 cx_testing_allocator_s *data = d;
62 void *ptr = realloc(mem, n);
63 if (ptr == mem) {
64 return ptr;
65 } else {
66 data->alloc_total++;
67 if (ptr == NULL) {
68 data->alloc_failed++;
69 } else {
70 data->free_total++;
71 if (cx_testing_allocator_remove(data, mem)) {
72 data->free_failed++;
73 }
74 cx_testing_allocator_add(data, ptr);
75 }
76 return ptr;
77 }
78 }
79
80 void *cx_calloc_testing(void *d, size_t nelem, size_t n) {
81 cx_testing_allocator_s *data = d;
82 void *ptr = calloc(nelem, n);
83 data->alloc_total++;
84 if (ptr == NULL) {
85 data->alloc_failed++;
86 } else {
87 cx_testing_allocator_add(data, ptr);
88 }
89 return ptr;
90 }
91
92 void cx_free_testing(void *d, void *mem) {
93 cx_testing_allocator_s *data = d;
94 data->free_total++;
95 if (cx_testing_allocator_remove(data, mem)) {
96 data->free_failed++;
97 // do not even attempt to free mem, because it is likely to segfault
98 } else {
99 free(mem);
100 }
101 }
102
103 cx_allocator_class cx_testing_allocator_class = {
104 cx_malloc_testing,
105 cx_realloc_testing,
106 cx_calloc_testing,
107 cx_free_testing
108 };
109
110 cx_testing_allocator_s cx_testing_allocator_data;
111
112 struct cx_allocator_s cx_testing_allocator = {
113 &cx_testing_allocator_class,
114 &cx_testing_allocator_data
115 };
116 CxAllocator cxTestingAllocator = &cx_testing_allocator;
117
118 void cxTestingAllocatorReset(void) {
119 memset(&cx_testing_allocator_data, 0, sizeof(cx_testing_allocator_s));
120 }
121
122 int cxTestingAllocatorVerify(void) {
123 return cx_testing_allocator_data.live == 0
124 && cx_testing_allocator_data.alloc_failed == 0 && cx_testing_allocator_data.free_failed == 0
125 && cx_testing_allocator_data.alloc_total == cx_testing_allocator_data.free_total;
126 }

mercurial