test/util_allocator.c

Sun, 26 Sep 2021 14:41:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 14:41:16 +0200
changeset 422
afd87df80b13
child 494
6ce8cfa10a96
permissions
-rw-r--r--

add utility to verify allocations

universe@422 1 /*
universe@422 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@422 3 *
universe@422 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@422 5 *
universe@422 6 * Redistribution and use in source and binary forms, with or without
universe@422 7 * modification, are permitted provided that the following conditions are met:
universe@422 8 *
universe@422 9 * 1. Redistributions of source code must retain the above copyright
universe@422 10 * notice, this list of conditions and the following disclaimer.
universe@422 11 *
universe@422 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@422 13 * notice, this list of conditions and the following disclaimer in the
universe@422 14 * documentation and/or other materials provided with the distribution.
universe@422 15 *
universe@422 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@422 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@422 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@422 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@422 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@422 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@422 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@422 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@422 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@422 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@422 26 * POSSIBILITY OF SUCH DAMAGE.
universe@422 27 */
universe@422 28
universe@422 29 #include "util_allocator.h"
universe@422 30 #include <string.h>
universe@422 31
universe@422 32 void cx_testing_allocator_add(cx_testing_allocator_s *data, void *ptr) {
universe@422 33 data->tracked[data->live] = ptr;
universe@422 34 data->live++;
universe@422 35 }
universe@422 36
universe@422 37 int cx_testing_allocator_remove(cx_testing_allocator_s *data, void *ptr) {
universe@422 38 for (int i = 0; i < data->live; i++) {
universe@422 39 if (data->tracked[i] == ptr) {
universe@422 40 data->tracked[i] = data->tracked[data->live - 1];
universe@422 41 data->live--;
universe@422 42 return 0;
universe@422 43 }
universe@422 44 }
universe@422 45 return 1;
universe@422 46 }
universe@422 47
universe@422 48 void *cx_malloc_testing(void *d, size_t n) {
universe@422 49 cx_testing_allocator_s *data = d;
universe@422 50 void *ptr = malloc(n);
universe@422 51 data->alloc_total++;
universe@422 52 if (ptr == NULL) {
universe@422 53 data->alloc_failed++;
universe@422 54 } else {
universe@422 55 cx_testing_allocator_add(data, ptr);
universe@422 56 }
universe@422 57 return ptr;
universe@422 58 }
universe@422 59
universe@422 60 void *cx_realloc_testing(void *d, void *mem, size_t n) {
universe@422 61 cx_testing_allocator_s *data = d;
universe@422 62 void *ptr = realloc(mem, n);
universe@422 63 if (ptr == mem) {
universe@422 64 return ptr;
universe@422 65 } else {
universe@422 66 data->alloc_total++;
universe@422 67 if (ptr == NULL) {
universe@422 68 data->alloc_failed++;
universe@422 69 } else {
universe@422 70 data->free_total++;
universe@422 71 if (cx_testing_allocator_remove(data, mem)) {
universe@422 72 data->free_failed++;
universe@422 73 }
universe@422 74 cx_testing_allocator_add(data, ptr);
universe@422 75 }
universe@422 76 return ptr;
universe@422 77 }
universe@422 78 }
universe@422 79
universe@422 80 void *cx_calloc_testing(void *d, size_t nelem, size_t n) {
universe@422 81 cx_testing_allocator_s *data = d;
universe@422 82 void *ptr = calloc(nelem, n);
universe@422 83 data->alloc_total++;
universe@422 84 if (ptr == NULL) {
universe@422 85 data->alloc_failed++;
universe@422 86 } else {
universe@422 87 cx_testing_allocator_add(data, ptr);
universe@422 88 }
universe@422 89 return ptr;
universe@422 90 }
universe@422 91
universe@422 92 void cx_free_testing(void *d, void *mem) {
universe@422 93 cx_testing_allocator_s *data = d;
universe@422 94 data->free_total++;
universe@422 95 if (cx_testing_allocator_remove(data, mem)) {
universe@422 96 data->free_failed++;
universe@422 97 // do not even attempt to free mem, because it is likely to segfault
universe@422 98 } else {
universe@422 99 free(mem);
universe@422 100 }
universe@422 101 }
universe@422 102
universe@422 103 cx_allocator_class cx_testing_allocator_class = {
universe@422 104 cx_malloc_testing,
universe@422 105 cx_realloc_testing,
universe@422 106 cx_calloc_testing,
universe@422 107 cx_free_testing
universe@422 108 };
universe@422 109
universe@422 110 cx_testing_allocator_s cx_testing_allocator_data;
universe@422 111
universe@422 112 struct cx_allocator_s cx_testing_allocator = {
universe@422 113 &cx_testing_allocator_class,
universe@422 114 &cx_testing_allocator_data
universe@422 115 };
universe@422 116 CxAllocator cxTestingAllocator = &cx_testing_allocator;
universe@422 117
universe@422 118 void cxTestingAllocatorReset(void) {
universe@422 119 memset(&cx_testing_allocator_data, 0, sizeof(cx_testing_allocator_s));
universe@422 120 }
universe@422 121
universe@422 122 int cxTestingAllocatorVerify(void) {
universe@422 123 return cx_testing_allocator_data.live == 0
universe@422 124 && cx_testing_allocator_data.alloc_failed == 0 && cx_testing_allocator_data.free_failed == 0
universe@422 125 && cx_testing_allocator_data.alloc_total == cx_testing_allocator_data.free_total;
universe@422 126 }

mercurial