Sun, 26 Sep 2021 14:41:16 +0200
add utility to verify allocations
test/CMakeLists.txt | file | annotate | diff | comparison | revisions | |
test/test_list.c | file | annotate | diff | comparison | revisions | |
test/util_allocator.c | file | annotate | diff | comparison | revisions | |
test/util_allocator.h | file | annotate | diff | comparison | revisions |
1.1 --- a/test/CMakeLists.txt Sun Sep 26 14:34:49 2021 +0200 1.2 +++ b/test/CMakeLists.txt Sun Sep 26 14:41:16 2021 +0200 1.3 @@ -13,7 +13,7 @@ 1.4 ) 1.5 1.6 foreach(test ${TESTS}) 1.7 - add_executable(${test} ${test}.c) 1.8 + add_executable(${test} util_allocator.c ${test}.c) 1.9 target_link_libraries(${test} PRIVATE ucx_static ${CUNIT_LIBRARY}) 1.10 target_include_directories(${test} PRIVATE ${CUNIT_INCLUDE_DIR}) 1.11 add_test(NAME ${test} COMMAND ${test} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
2.1 --- a/test/test_list.c Sun Sep 26 14:34:49 2021 +0200 2.2 +++ b/test/test_list.c Sun Sep 26 14:41:16 2021 +0200 2.3 @@ -28,6 +28,7 @@ 2.4 2.5 #include "cx/linked_list.h" 2.6 #include "test_config.h" 2.7 +#include "util_allocator.h" 2.8 2.9 int cmp_int(int const *l, int const *r) { 2.10 int left = *l, right = *r; 2.11 @@ -35,27 +36,32 @@ 2.12 } 2.13 2.14 void test_linked_list_create() { 2.15 - CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int)); 2.16 + cxTestingAllocatorReset(); 2.17 + 2.18 + CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); 2.19 2.20 CU_ASSERT_EQUAL(list->data.size, 0) 2.21 CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1) 2.22 - CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator) 2.23 + CU_ASSERT_PTR_EQUAL(list->data.allocator, cxTestingAllocator) 2.24 CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int)) 2.25 CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int) 2.26 2.27 struct node { 2.28 - void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc; 2.29 + void *begin; 2.30 + void *end; 2.31 + ptrdiff_t ploc; 2.32 + ptrdiff_t nloc; 2.33 }; 2.34 2.35 - struct node* actual = (struct node*) list->data.listdata; 2.36 + struct node *actual = (struct node *) list->data.listdata; 2.37 CU_ASSERT_PTR_NULL(actual->begin) 2.38 CU_ASSERT_PTR_NULL(actual->end) 2.39 CU_ASSERT_EQUAL(0, actual->ploc) 2.40 - CU_ASSERT_EQUAL(sizeof(void*), actual->nloc) 2.41 + CU_ASSERT_EQUAL(sizeof(void *), actual->nloc) 2.42 2.43 cxLinkedListDestroy(list); 2.44 2.45 - // TODO: use allocator that keeps track of the freed memory 2.46 + CU_ASSERT_TRUE(cxTestingAllocatorVerify()) 2.47 } 2.48 2.49 int main() {
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/util_allocator.c Sun Sep 26 14:41:16 2021 +0200 3.3 @@ -0,0 +1,126 @@ 3.4 +/* 3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 + * 3.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 3.8 + * 3.9 + * Redistribution and use in source and binary forms, with or without 3.10 + * modification, are permitted provided that the following conditions are met: 3.11 + * 3.12 + * 1. Redistributions of source code must retain the above copyright 3.13 + * notice, this list of conditions and the following disclaimer. 3.14 + * 3.15 + * 2. Redistributions in binary form must reproduce the above copyright 3.16 + * notice, this list of conditions and the following disclaimer in the 3.17 + * documentation and/or other materials provided with the distribution. 3.18 + * 3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 + * POSSIBILITY OF SUCH DAMAGE. 3.30 + */ 3.31 + 3.32 +#include "util_allocator.h" 3.33 +#include <string.h> 3.34 + 3.35 +void cx_testing_allocator_add(cx_testing_allocator_s *data, void *ptr) { 3.36 + data->tracked[data->live] = ptr; 3.37 + data->live++; 3.38 +} 3.39 + 3.40 +int cx_testing_allocator_remove(cx_testing_allocator_s *data, void *ptr) { 3.41 + for (int i = 0; i < data->live; i++) { 3.42 + if (data->tracked[i] == ptr) { 3.43 + data->tracked[i] = data->tracked[data->live - 1]; 3.44 + data->live--; 3.45 + return 0; 3.46 + } 3.47 + } 3.48 + return 1; 3.49 +} 3.50 + 3.51 +void *cx_malloc_testing(void *d, size_t n) { 3.52 + cx_testing_allocator_s *data = d; 3.53 + void *ptr = malloc(n); 3.54 + data->alloc_total++; 3.55 + if (ptr == NULL) { 3.56 + data->alloc_failed++; 3.57 + } else { 3.58 + cx_testing_allocator_add(data, ptr); 3.59 + } 3.60 + return ptr; 3.61 +} 3.62 + 3.63 +void *cx_realloc_testing(void *d, void *mem, size_t n) { 3.64 + cx_testing_allocator_s *data = d; 3.65 + void *ptr = realloc(mem, n); 3.66 + if (ptr == mem) { 3.67 + return ptr; 3.68 + } else { 3.69 + data->alloc_total++; 3.70 + if (ptr == NULL) { 3.71 + data->alloc_failed++; 3.72 + } else { 3.73 + data->free_total++; 3.74 + if (cx_testing_allocator_remove(data, mem)) { 3.75 + data->free_failed++; 3.76 + } 3.77 + cx_testing_allocator_add(data, ptr); 3.78 + } 3.79 + return ptr; 3.80 + } 3.81 +} 3.82 + 3.83 +void *cx_calloc_testing(void *d, size_t nelem, size_t n) { 3.84 + cx_testing_allocator_s *data = d; 3.85 + void *ptr = calloc(nelem, n); 3.86 + data->alloc_total++; 3.87 + if (ptr == NULL) { 3.88 + data->alloc_failed++; 3.89 + } else { 3.90 + cx_testing_allocator_add(data, ptr); 3.91 + } 3.92 + return ptr; 3.93 +} 3.94 + 3.95 +void cx_free_testing(void *d, void *mem) { 3.96 + cx_testing_allocator_s *data = d; 3.97 + data->free_total++; 3.98 + if (cx_testing_allocator_remove(data, mem)) { 3.99 + data->free_failed++; 3.100 + // do not even attempt to free mem, because it is likely to segfault 3.101 + } else { 3.102 + free(mem); 3.103 + } 3.104 +} 3.105 + 3.106 +cx_allocator_class cx_testing_allocator_class = { 3.107 + cx_malloc_testing, 3.108 + cx_realloc_testing, 3.109 + cx_calloc_testing, 3.110 + cx_free_testing 3.111 +}; 3.112 + 3.113 +cx_testing_allocator_s cx_testing_allocator_data; 3.114 + 3.115 +struct cx_allocator_s cx_testing_allocator = { 3.116 + &cx_testing_allocator_class, 3.117 + &cx_testing_allocator_data 3.118 +}; 3.119 +CxAllocator cxTestingAllocator = &cx_testing_allocator; 3.120 + 3.121 +void cxTestingAllocatorReset(void) { 3.122 + memset(&cx_testing_allocator_data, 0, sizeof(cx_testing_allocator_s)); 3.123 +} 3.124 + 3.125 +int cxTestingAllocatorVerify(void) { 3.126 + return cx_testing_allocator_data.live == 0 3.127 + && cx_testing_allocator_data.alloc_failed == 0 && cx_testing_allocator_data.free_failed == 0 3.128 + && cx_testing_allocator_data.alloc_total == cx_testing_allocator_data.free_total; 3.129 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/util_allocator.h Sun Sep 26 14:41:16 2021 +0200 4.3 @@ -0,0 +1,90 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + */ 4.31 + 4.32 +#ifndef UCX_UTIL_ALLOCATOR_H 4.33 +#define UCX_UTIL_ALLOCATOR_H 4.34 + 4.35 +#include "cx/allocator.h" 4.36 + 4.37 +#ifdef __cplusplus 4.38 +extern "C" { 4.39 +#endif 4.40 + 4.41 +#define CX_TESTING_ALLOCATOR_MAX_LIVE 1024 4.42 + 4.43 +typedef struct { 4.44 + /** 4.45 + * Total number of all allocations (malloc, calloc, realloc). 4.46 + * A realloc() does only count when the memory has to be moved. 4.47 + */ 4.48 + int alloc_total; 4.49 + /** 4.50 + * Number of failed allocations (malloc, calloc, realloc). 4.51 + */ 4.52 + int alloc_failed; 4.53 + /** 4.54 + * Total number of freed pointers. 4.55 + * A reallocation also counts as a free when the memory has to be moved. 4.56 + */ 4.57 + int free_total; 4.58 + /** 4.59 + * Number of failed free invocations. 4.60 + * A free() is considered failed, if it has not been performed on tracked memory. 4.61 + */ 4.62 + int free_failed; 4.63 + /** 4.64 + * Number of memory blocks that are currently live (and tracked). 4.65 + * The maximum number of tracked blocks is defined in #CX_TESTING_ALLOCATOR_MAX_LIVE. 4.66 + */ 4.67 + int live; 4.68 + /** 4.69 + * The array of tracked memory blocks. 4.70 + */ 4.71 + void *tracked[CX_TESTING_ALLOCATOR_MAX_LIVE]; 4.72 +} cx_testing_allocator_s; 4.73 + 4.74 +extern CxAllocator cxTestingAllocator; 4.75 + 4.76 +/** 4.77 + * Resets the testing allocator information. 4.78 + * This function SHOULD be called prior to any use of this allocator. 4.79 + */ 4.80 +void cxTestingAllocatorReset(void); 4.81 + 4.82 +/** 4.83 + * Checks whether all allocated memory is properly freed and no failed (de)allocations happened. 4.84 + * 4.85 + * @return true on success, false if there was any problem 4.86 + */ 4.87 +int cxTestingAllocatorVerify(void); 4.88 + 4.89 +#ifdef __cplusplus 4.90 +}; /* extern "C" */ 4.91 +#endif 4.92 + 4.93 +#endif /* UCX_UTIL_ALLOCATOR_H */