Wed, 28 Jun 2023 20:07:52 +0200
improve mempool implementation
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "cx/mempool.h" #include "util_allocator.h" #include <gtest/gtest.h> TEST(Mempool, Create) { auto pool = cxBasicMempoolCreate(16); ASSERT_EQ(pool->auto_destr, nullptr); ASSERT_NE(pool->allocator, nullptr); ASSERT_NE(pool->allocator->cl, nullptr); EXPECT_EQ(pool->allocator->data, pool); EXPECT_NE(pool->allocator->cl->malloc, nullptr); EXPECT_NE(pool->allocator->cl->calloc, nullptr); EXPECT_NE(pool->allocator->cl->realloc, nullptr); EXPECT_NE(pool->allocator->cl->free, nullptr); EXPECT_EQ(pool->capacity, 16); EXPECT_EQ(pool->size, 0); EXPECT_NE(pool->data, nullptr); cxMempoolDestroy(pool); } TEST(Mempool, malloc) { auto pool = cxBasicMempoolCreate(4); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_EQ(pool->size, 2); EXPECT_EQ(pool->capacity, 4); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_EQ(pool->size, 4); EXPECT_EQ(pool->capacity, 4); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); EXPECT_EQ(pool->size, 6); EXPECT_GE(pool->capacity, 6); cxMempoolDestroy(pool); } TEST(Mempool, calloc) { auto pool = cxBasicMempoolCreate(4); auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int)); ASSERT_NE(test, nullptr); EXPECT_EQ(test[0], 0); EXPECT_EQ(test[1], 0); cxMempoolDestroy(pool); } static unsigned test_destructor_called; static void test_destructor([[maybe_unused]] void *mem) { test_destructor_called++; } TEST(Mempool, realloc) { auto pool = cxMempoolCreate(4, test_destructor); ASSERT_EQ(pool->auto_destr, test_destructor); auto data = cxMalloc(pool->allocator, sizeof(int)); *((int *) data) = 13; void *rdata = data; unsigned n = 1; while (rdata == data) { n <<= 1; ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); } EXPECT_EQ(*((int *) rdata), 13); // test if destructor is still intact test_destructor_called = 0; cxFree(pool->allocator, rdata); EXPECT_EQ(test_destructor_called, 1); cxMempoolDestroy(pool); } TEST(Mempool, free) { auto pool = cxBasicMempoolCreate(4); void *mem1; void *mem2; mem1 = cxMalloc(pool->allocator, 16); cxFree(pool->allocator, mem1); EXPECT_EQ(pool->size, 0); cxMalloc(pool->allocator, 16); cxMalloc(pool->allocator, 16); mem1 = cxMalloc(pool->allocator, 16); cxMalloc(pool->allocator, 16); mem2 = cxMalloc(pool->allocator, 16); EXPECT_EQ(pool->size, 5); cxFree(pool->allocator, mem1); EXPECT_EQ(pool->size, 4); cxFree(pool->allocator, mem2); EXPECT_EQ(pool->size, 3); cxMempoolDestroy(pool); } TEST(Mempool, Destroy) { auto pool = cxBasicMempoolCreate(4); auto data = cxMalloc(pool->allocator, sizeof(int)); *((int *) data) = 13; cxMempoolSetDestructor(data, test_destructor); EXPECT_EQ(*((int *) data), 13); test_destructor_called = 0; cxFree(pool->allocator, data); EXPECT_EQ(test_destructor_called, 1); data = cxMalloc(pool->allocator, sizeof(int)); cxMempoolSetDestructor(data, test_destructor); cxMempoolDestroy(pool); EXPECT_EQ(test_destructor_called, 2); } TEST(Mempool, Register) { auto pool = cxBasicMempoolCreate(4); auto data = cxMalloc(pool->allocator, sizeof(int)); test_destructor_called = 0; cxMempoolSetDestructor(data, test_destructor); int donotfree = 0; cxMempoolRegister(pool, &donotfree, test_destructor); cxMempoolDestroy(pool); EXPECT_EQ(test_destructor_called, 2); }