universe@422: /* universe@422: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@422: * universe@422: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@422: * universe@422: * Redistribution and use in source and binary forms, with or without universe@422: * modification, are permitted provided that the following conditions are met: universe@422: * universe@422: * 1. Redistributions of source code must retain the above copyright universe@422: * notice, this list of conditions and the following disclaimer. universe@422: * universe@422: * 2. Redistributions in binary form must reproduce the above copyright universe@422: * notice, this list of conditions and the following disclaimer in the universe@422: * documentation and/or other materials provided with the distribution. universe@422: * universe@422: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@422: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@422: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@422: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@422: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@422: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@422: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@422: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@422: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@422: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@422: * POSSIBILITY OF SUCH DAMAGE. universe@422: */ universe@422: universe@422: #include "util_allocator.h" universe@422: universe@422: void *cx_malloc_testing(void *d, size_t n) { universe@518: auto data = reinterpret_cast(d); universe@422: void *ptr = malloc(n); universe@422: data->alloc_total++; universe@518: if (ptr == nullptr) { universe@422: data->alloc_failed++; universe@422: } else { universe@518: data->tracked.insert(ptr); universe@422: } universe@422: return ptr; universe@422: } universe@422: universe@422: void *cx_realloc_testing(void *d, void *mem, size_t n) { universe@518: auto data = reinterpret_cast(d); universe@422: void *ptr = realloc(mem, n); universe@422: if (ptr == mem) { universe@422: return ptr; universe@422: } else { universe@422: data->alloc_total++; universe@518: if (ptr == nullptr) { universe@422: data->alloc_failed++; universe@422: } else { universe@422: data->free_total++; universe@518: if (data->tracked.erase(mem) == 0) { universe@422: data->free_failed++; universe@422: } universe@518: data->tracked.insert(ptr); universe@422: } universe@422: return ptr; universe@422: } universe@422: } universe@422: universe@422: void *cx_calloc_testing(void *d, size_t nelem, size_t n) { universe@518: auto data = reinterpret_cast(d); universe@422: void *ptr = calloc(nelem, n); universe@422: data->alloc_total++; universe@518: if (ptr == nullptr) { universe@422: data->alloc_failed++; universe@422: } else { universe@518: data->tracked.insert(ptr); universe@422: } universe@422: return ptr; universe@422: } universe@422: universe@422: void cx_free_testing(void *d, void *mem) { universe@518: auto data = reinterpret_cast(d); universe@422: data->free_total++; universe@518: if (data->tracked.erase(mem) == 0) { universe@422: data->free_failed++; universe@422: // do not even attempt to free mem, because it is likely to segfault universe@422: } else { universe@422: free(mem); universe@422: } universe@422: } universe@422: universe@422: cx_allocator_class cx_testing_allocator_class = { universe@422: cx_malloc_testing, universe@422: cx_realloc_testing, universe@422: cx_calloc_testing, universe@422: cx_free_testing universe@422: }; universe@422: universe@518: CxTestingAllocator::CxTestingAllocator() : CxAllocator() { universe@518: cl = &cx_testing_allocator_class; universe@518: data = this; universe@422: } universe@422: universe@571: bool CxTestingAllocator::used() const { universe@571: return alloc_total > 0; universe@571: } universe@571: universe@518: bool CxTestingAllocator::verify() const { universe@518: return tracked.empty() && alloc_failed == 0 && free_failed == 0 && alloc_total == free_total; universe@422: } universe@518: universe@518: // SELF-TEST universe@518: universe@518: #include universe@518: universe@518: TEST(TestingAllocator, ExpectFree) { universe@518: CxTestingAllocator allocator; universe@518: universe@518: ASSERT_TRUE(allocator.verify()); universe@642: EXPECT_FALSE(allocator.used()); universe@518: auto ptr = cxMalloc(&allocator, 16); universe@642: EXPECT_TRUE(allocator.used()); universe@518: ASSERT_NE(ptr, nullptr); universe@518: EXPECT_FALSE(allocator.verify()); universe@518: universe@518: cxFree(&allocator, ptr); universe@518: EXPECT_TRUE(allocator.verify()); universe@518: } universe@518: universe@518: TEST(TestingAllocator, DetectDoubleFree) { universe@518: CxTestingAllocator allocator; universe@518: universe@518: ASSERT_TRUE(allocator.verify()); universe@518: auto ptr = cxMalloc(&allocator, 16); universe@518: ASSERT_NE(ptr, nullptr); universe@518: universe@518: cxFree(&allocator, ptr); universe@518: EXPECT_TRUE(allocator.verify()); universe@518: ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr)); universe@518: EXPECT_FALSE(allocator.verify()); universe@518: } universe@518: universe@518: TEST(TestingAllocator, FreeUntracked) { universe@518: CxTestingAllocator allocator; universe@518: universe@518: auto ptr = malloc(16); universe@518: ASSERT_TRUE(allocator.verify()); universe@518: ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr)); universe@518: EXPECT_FALSE(allocator.verify()); universe@518: ASSERT_NO_FATAL_FAILURE(free(ptr)); universe@518: } universe@518: universe@518: TEST(TestingAllocator, FullLifecycleWithRealloc) { universe@518: CxTestingAllocator allocator; universe@518: ASSERT_TRUE(allocator.verify()); universe@518: auto ptr = cxMalloc(&allocator, 16); universe@518: ASSERT_NE(ptr, nullptr); universe@518: EXPECT_EQ(allocator.tracked.size(), 1); universe@518: ptr = cxRealloc(&allocator, ptr, 256); universe@518: ASSERT_NE(ptr, nullptr); universe@518: EXPECT_EQ(allocator.tracked.size(), 1); universe@518: cxFree(&allocator, ptr); universe@518: EXPECT_TRUE(allocator.verify()); universe@518: } universe@518: universe@518: TEST(TestingAllocator, CallocInitializes) { universe@518: CxTestingAllocator allocator; universe@642: const char zeros[16] = {0}; universe@518: auto ptr = cxCalloc(&allocator, 16, 1); universe@518: EXPECT_EQ(memcmp(ptr, zeros, 16), 0); universe@518: cxFree(&allocator, ptr); universe@518: EXPECT_TRUE(allocator.verify()); universe@518: }