# HG changeset patch # User Mike Becker # Date 1687975672 -7200 # Node ID d92a59f5d261d48b463c299551e154e2a8dfe7b9 # Parent 44986c0e2b05ed17cb44188225b86f2c9412c3b1 improve mempool implementation diff -r 44986c0e2b05 -r d92a59f5d261 docs/src/features.md --- a/docs/src/features.md Wed Jun 28 19:18:01 2023 +0200 +++ b/docs/src/features.md Wed Jun 28 20:07:52 2023 +0200 @@ -30,7 +30,7 @@ struct my_allocator_state { size_t total; size_t avail; - char[] mem; + char mem[]; }; static cx_allocator_class my_allocator_class = { @@ -91,10 +91,6 @@ *Header file:* [mempool.h](api/mempool_8h.html) -### Basic Memory Pool - -*Header file:* [basic_mempool.h](api/basic__mempool_8h.html) - ## Iterator *Header file:* [iterator.h](api/iterator_8h.html) diff -r 44986c0e2b05 -r d92a59f5d261 src/CMakeLists.txt --- a/src/CMakeLists.txt Wed Jun 28 19:18:01 2023 +0200 +++ b/src/CMakeLists.txt Wed Jun 28 20:07:52 2023 +0200 @@ -9,7 +9,7 @@ map.c hash_key.c hash_map.c - basic_mempool.c + mempool.c printf.c compare.c ) @@ -28,7 +28,6 @@ cx/hash_key.h cx/hash_map.h cx/mempool.h - cx/basic_mempool.h cx/printf.h cx/compare.h ) diff -r 44986c0e2b05 -r d92a59f5d261 src/basic_mempool.c --- a/src/basic_mempool.c Wed Jun 28 19:18:01 2023 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,235 +0,0 @@ -/* - * 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/basic_mempool.h" -#include "cx/utils.h" -#include - -#define of_chk_(n) if (SIZE_MAX - sizeof(cx_destructor_func) < (n)) return NULL - -/** Internal structure for denoting pooled memory. */ -typedef struct { - /** The destructor. */ - cx_destructor_func destructor; - /** - * Access to the first byte of the polled memory. - */ - char c; -} cx_basic_mempool_memory; - -static int cx_basic_mempool_chcap( - struct cx_basic_mempool_s *pool, - size_t newcap -) { - if (newcap < pool->ndata) { - return 1; - } - - size_t newcapsz; - if (cx_szmul(newcap, sizeof(void *), &newcapsz)) { - return 1; - } - - void **data = realloc(pool->data, newcapsz); - if (data) { - pool->data = data; - pool->size = newcap; - return 0; - } else { - return 1; - } -} - -void *cx_malloc_basic_mempool( - void *data, - size_t n -) { - of_chk_(n); - struct cx_basic_mempool_s *pool = data; - - if (pool->ndata >= pool->size) { - size_t newcap = pool->size * 2; - if (newcap < pool->size || cx_basic_mempool_chcap(pool, newcap)) { - return NULL; - } - } - - cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n); - if (mem == NULL) { - return NULL; - } - - mem->destructor = NULL; - pool->data[pool->ndata] = mem; - pool->ndata++; - - return &(mem->c); -} - -void *cx_calloc_basic_mempool( - void *data, - size_t nelem, - size_t elsize -) { - size_t msz; - if (cx_szmul(nelem, elsize, &msz)) { - return NULL; - } - void *ptr = cx_malloc_basic_mempool(data, msz); - if (ptr == NULL) { - return NULL; - } - memset(ptr, 0, nelem * elsize); - return ptr; -} - -void *cx_realloc_basic_mempool( - void *data, - void *ptr, - size_t n -) { - of_chk_(n); - struct cx_basic_mempool_s *pool = data; - - char *mem = ((char *) ptr) - sizeof(cx_destructor_func); - char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func)); - if (newm == NULL) { - return NULL; - } - if (mem != newm) { - cx_for_n(i, pool->ndata) { - if (pool->data[i] == mem) { - pool->data[i] = newm; - return newm + sizeof(cx_destructor_func); - } - } - abort(); - } else { - return newm + sizeof(cx_destructor_func); - } -} - -void cx_free_basic_mempool( - void *data, - void *ptr -) { - struct cx_basic_mempool_s *pool = data; - - cx_basic_mempool_memory *mem = (cx_basic_mempool_memory *) - ((char *) ptr - sizeof(cx_destructor_func)); - cx_for_n(i, pool->ndata) { - if (mem == pool->data[i]) { - if (mem->destructor != NULL) { - mem->destructor(&(mem->c)); - } - free(mem); - size_t last_index = pool->ndata - 1; - if (i != last_index) { - pool->data[i] = pool->data[last_index]; - pool->data[last_index] = NULL; - } - pool->ndata--; - return; - } - } - abort(); -} - -void cx_basic_mempool_destroy(CxMempool *p) { - struct cx_basic_mempool_s *pool = (struct cx_basic_mempool_s *) p; - cx_basic_mempool_memory *mem; - cx_for_n(i, pool->ndata) { - mem = (cx_basic_mempool_memory *) pool->data[i]; - if (mem) { - if (mem->destructor) { - mem->destructor(&(mem->c)); - } - free(mem); - } - } - free(pool->data); - free((void *) p->allocator); - free(pool); -} - -void cx_basic_mempool_set_destr( - __attribute__((__unused__)) CxMempool *pool, - void *ptr, - cx_destructor_func func -) { - *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; -} - -static cx_allocator_class cx_basic_mempool_allocator_class = { - cx_malloc_basic_mempool, - cx_realloc_basic_mempool, - cx_calloc_basic_mempool, - cx_free_basic_mempool -}; - -static cx_mempool_class cx_basic_mempool_class = { - cx_basic_mempool_destroy, - cx_basic_mempool_set_destr, -}; - -CxMempool *cxBasicMempoolCreate(size_t capacity) { - size_t poolsize; - if (cx_szmul(capacity, sizeof(void *), &poolsize)) { - return NULL; - } - - struct cx_basic_mempool_s *pool = - malloc(sizeof(struct cx_basic_mempool_s)); - if (pool == NULL) { - return NULL; - } - - - CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); - if (!provided_allocator) { - free(pool); - return NULL; - } - provided_allocator->cl = &cx_basic_mempool_allocator_class; - provided_allocator->data = pool; - - pool->base.cl = &cx_basic_mempool_class; - pool->base.allocator = provided_allocator; - - pool->data = malloc(poolsize); - if (pool->data == NULL) { - free(provided_allocator); - free(pool); - return NULL; - } - - pool->ndata = 0; - pool->size = capacity; - - return (CxMempool *) pool; -} diff -r 44986c0e2b05 -r d92a59f5d261 src/cx/basic_mempool.h --- a/src/cx/basic_mempool.h Wed Jun 28 19:18:01 2023 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * 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. - */ -/** - * \file basic_mempool.h - * \brief Implementation of a basic memory pool. - * \author Mike Becker - * \author Olaf Wintermann - * \version 3.0 - * \copyright 2-Clause BSD License - */ - -#ifndef UCX_BASIC_MEMPOOL_H -#define UCX_BASIC_MEMPOOL_H - -#include "mempool.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Basic array-based memory pool. - */ -struct cx_basic_mempool_s { - /** Inherit base structure members. */ - CxMempool base; - - /** List of pointers to pooled memory. */ - void **data; - - /** Number of pooled memory items. */ - size_t ndata; - - /** Memory pool size. */ - size_t size; -}; - -/** - * Creates a basic array-based memory pool. - * - * @param capacity the initial capacity of the pool - * @return the created memory pool or \c NULL if allocation failed - */ -__attribute__((__warn_unused_result__)) -CxMempool *cxBasicMempoolCreate(size_t capacity); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif // UCX_BASIC_MEMPOOL_H diff -r 44986c0e2b05 -r d92a59f5d261 src/cx/mempool.h --- a/src/cx/mempool.h Wed Jun 28 19:18:01 2023 +0200 +++ b/src/cx/mempool.h Wed Jun 28 20:07:52 2023 +0200 @@ -44,24 +44,31 @@ extern "C" { #endif -/** - * Memory pool class type. - */ -typedef struct cx_mempool_class_s cx_mempool_class; +/** Internal structure for pooled memory. */ +struct cx_mempool_memory_s; /** * The basic structure of a memory pool. * Should be the first member of an actual memory pool implementation. */ struct cx_mempool_s { + /** The provided allocator. */ + CxAllocator const *allocator; + /** - * The pool class definition. + * A destructor that shall be automatically registered for newly allocated memory. + * This destructor MUST NOT free the memory. */ - cx_mempool_class *cl; - /** - * The provided allocator. - */ - CxAllocator const *allocator; + cx_destructor_func auto_destr; + + /** Array of pooled memory. */ + struct cx_mempool_memory_s **data; + + /** Number of pooled memory items. */ + size_t size; + + /** Memory pool capacity. */ + size_t capacity; }; /** @@ -70,22 +77,27 @@ typedef struct cx_mempool_s CxMempool; /** - * The class definition for a memory pool. + * Creates an array-based memory pool with a shared destructor function. + * + * This destructor MUST NOT free the memory. + * + * @param capacity the initial capacity of the pool + * @param destr the destructor function to use for allocated memory + * @return the created memory pool or \c NULL if allocation failed */ -struct cx_mempool_class_s { - /** Member function for destroying the pool. */ - __attribute__((__nonnull__)) - void (*destroy)(CxMempool *pool); +__attribute__((__warn_unused_result__)) +CxMempool *cxMempoolCreate(size_t capacity, cx_destructor_func destr); - /** Member function for setting a destructor. */ - __attribute__((__nonnull__)) - void (*set_destructor)( - CxMempool *pool, - void *memory, - cx_destructor_func fnc - ); -}; - +/** + * Creates a basic array-based memory pool. + * + * @param capacity the initial capacity of the pool + * @return the created memory pool or \c NULL if allocation failed + */ +__attribute__((__warn_unused_result__)) +static inline CxMempool *cxBasicMempoolCreate(size_t capacity) { + return cxMempoolCreate(capacity, NULL); +} /** * Destroys a memory pool including their contents. @@ -93,27 +105,42 @@ * @param pool the memory pool to destroy */ __attribute__((__nonnull__)) -static inline void cxMempoolDestroy(CxMempool *pool) { - pool->cl->destroy(pool); -} +void cxMempoolDestroy(CxMempool *pool); /** - * Sets a destructor function for an allocated memory object. + * Sets the destructor function for a specific allocated memory object. * - * If the memory is not managed by the pool, the behavior is undefined. + * If the memory is not managed by a UCX memory pool, the behavior is undefined. + * The destructor MUST NOT free the memory. * - * @param pool the pool - * @param memory the objected allocated in the pool + * @param memory the object allocated in the pool * @param fnc the destructor function */ __attribute__((__nonnull__)) -static inline void cxMempoolSetDestructor( - CxMempool *pool, +void cxMempoolSetDestructor( void *memory, cx_destructor_func fnc -) { - pool->cl->set_destructor(pool, memory, fnc); -} +); + +/** + * Registers foreign memory with this pool. + * + * The destructor, in contrast to memory allocated by the pool, MUST free the memory. + * + * A small portion of memory will be allocated to register the information in the pool. + * If that allocation fails, this function will return non-zero. + * + * @param pool the pool + * @param memory the object allocated in the pool + * @param destr the destructor function + * @return zero on success, non-zero on failure + */ +__attribute__((__nonnull__)) +int cxMempoolRegister( + CxMempool *pool, + void *memory, + cx_destructor_func destr +); #ifdef __cplusplus } // extern "C" diff -r 44986c0e2b05 -r d92a59f5d261 src/mempool.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mempool.c Wed Jun 28 20:07:52 2023 +0200 @@ -0,0 +1,232 @@ +/* + * 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 "cx/utils.h" +#include + +struct cx_mempool_memory_s { + /** The destructor. */ + cx_destructor_func destructor; + /** The actual memory. */ + char c[]; +}; + +static void *cx_mempool_malloc( + void *p, + size_t n +) { + struct cx_mempool_s *pool = p; + + if (pool->size >= pool->capacity) { + size_t newcap = pool->capacity - (pool->capacity % 16) + 16; + struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*)); + if (newdata == NULL) { + return NULL; + } + pool->data = newdata; + pool->capacity = newcap; + } + + struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n); + if (mem == NULL) { + return NULL; + } + + mem->destructor = pool->auto_destr; + pool->data[pool->size] = mem; + pool->size++; + + return mem->c; +} + +static void *cx_mempool_calloc( + void *p, + size_t nelem, + size_t elsize +) { + size_t msz; + if (cx_szmul(nelem, elsize, &msz)) { + return NULL; + } + void *ptr = cx_mempool_malloc(p, msz); + if (ptr == NULL) { + return NULL; + } + memset(ptr, 0, nelem * elsize); + return ptr; +} + +static void *cx_mempool_realloc( + void *p, + void *ptr, + size_t n +) { + struct cx_mempool_s *pool = p; + + struct cx_mempool_memory_s *mem, *newm; + mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func)); + newm = realloc(mem, n + sizeof(cx_destructor_func)); + + if (newm == NULL) { + return NULL; + } + if (mem != newm) { + cx_for_n(i, pool->size) { + if (pool->data[i] == mem) { + pool->data[i] = newm; + return ((char*)newm) + sizeof(cx_destructor_func); + } + } + abort(); + } else { + return ptr; + } +} + +static void cx_mempool_free( + void *p, + void *ptr +) { + struct cx_mempool_s *pool = p; + + struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *) + ((char *) ptr - sizeof(cx_destructor_func)); + + cx_for_n(i, pool->size) { + if (mem == pool->data[i]) { + if (mem->destructor) { + mem->destructor(mem->c); + } + free(mem); + size_t last_index = pool->size - 1; + if (i != last_index) { + pool->data[i] = pool->data[last_index]; + pool->data[last_index] = NULL; + } + pool->size--; + return; + } + } + abort(); +} + +void cxMempoolDestroy(CxMempool *pool) { + struct cx_mempool_memory_s *mem; + cx_for_n(i, pool->size) { + mem = pool->data[i]; + if (mem->destructor) { + mem->destructor(mem->c); + } + free(mem); + } + free(pool->data); + free((void*) pool->allocator); + free(pool); +} + +void cxMempoolSetDestructor( + void *ptr, + cx_destructor_func func +) { + *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; +} + +struct cx_mempool_foreign_mem_s { + cx_destructor_func destr; + void* mem; +}; + +static void cx_mempool_destr_foreign_mem(void* ptr) { + struct cx_mempool_foreign_mem_s *fm = ptr; + fm->destr(fm->mem); +} + +int cxMempoolRegister( + CxMempool *pool, + void *memory, + cx_destructor_func destr +) { + struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc( + pool, + sizeof(struct cx_mempool_foreign_mem_s) + ); + if (fm == NULL) return 1; + + fm->mem = memory; + fm->destr = destr; + *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem; + + return 0; +} + +static cx_allocator_class cx_mempool_allocator_class = { + cx_mempool_malloc, + cx_mempool_realloc, + cx_mempool_calloc, + cx_mempool_free +}; + +CxMempool *cxMempoolCreate( + size_t capacity, + cx_destructor_func destr +) { + size_t poolsize; + if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { + return NULL; + } + + struct cx_mempool_s *pool = + malloc(sizeof(struct cx_mempool_s)); + if (pool == NULL) { + return NULL; + } + + CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); + if (provided_allocator == NULL) { + free(pool); + return NULL; + } + provided_allocator->cl = &cx_mempool_allocator_class; + provided_allocator->data = pool; + + pool->allocator = provided_allocator; + + pool->data = malloc(poolsize); + if (pool->data == NULL) { + free(provided_allocator); + free(pool); + return NULL; + } + + pool->size = 0; + pool->capacity = capacity; + pool->auto_destr = destr; + + return (CxMempool *) pool; +} diff -r 44986c0e2b05 -r d92a59f5d261 tests/CMakeLists.txt --- a/tests/CMakeLists.txt Wed Jun 28 19:18:01 2023 +0200 +++ b/tests/CMakeLists.txt Wed Jun 28 20:07:52 2023 +0200 @@ -26,7 +26,7 @@ test_hash_key.cpp test_map.cpp test_map_generics.c - test_basic_mempool.cpp + test_mempool.cpp test_printf.cpp selftest.cpp util_allocator.cpp diff -r 44986c0e2b05 -r d92a59f5d261 tests/test_basic_mempool.cpp --- a/tests/test_basic_mempool.cpp Wed Jun 28 19:18:01 2023 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +0,0 @@ -/* - * 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/basic_mempool.h" -#include "util_allocator.h" -#include - -class CxBasicMempool : public ::testing::Test { -protected: - CxMempool *pool = nullptr; - - void TearDown() override { - if (pool != nullptr) { - cxMempoolDestroy(pool); - } - } -}; - -TEST_F(CxBasicMempool, Create) { - pool = cxBasicMempoolCreate(16); - ASSERT_NE(pool->allocator, nullptr); - ASSERT_NE(pool->cl, nullptr); - EXPECT_NE(pool->cl->destroy, 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); - - auto basic_pool = reinterpret_cast(pool); - EXPECT_EQ(basic_pool->size, 16); - EXPECT_EQ(basic_pool->ndata, 0); - EXPECT_NE(basic_pool->data, nullptr); -} - -TEST_F(CxBasicMempool, malloc) { - pool = cxBasicMempoolCreate(4); - auto basic_pool = reinterpret_cast(pool); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_EQ(basic_pool->ndata, 2); - EXPECT_EQ(basic_pool->size, 4); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_EQ(basic_pool->ndata, 4); - EXPECT_EQ(basic_pool->size, 4); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_EQ(basic_pool->ndata, 6); - EXPECT_GE(basic_pool->size, 6); -} - -TEST_F(CxBasicMempool, calloc) { - 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); -} - -static unsigned test_destructor_called = 0; - -static void test_destructor([[maybe_unused]] void *mem) { - test_destructor_called++; -} - -TEST_F(CxBasicMempool, destructor) { - pool = cxBasicMempoolCreate(4); - auto data = cxMalloc(pool->allocator, sizeof(int)); - *((int *) data) = 13; - cxMempoolSetDestructor(pool, 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(pool, data, test_destructor); - cxMempoolDestroy(pool); - pool = nullptr; - EXPECT_EQ(test_destructor_called, 2); -} - -TEST_F(CxBasicMempool, realloc) { - pool = cxBasicMempoolCreate(4); - auto data = cxMalloc(pool->allocator, sizeof(int)); - *((int *) data) = 13; - cxMempoolSetDestructor(pool, data, test_destructor); - - 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); -} - - -TEST_F(CxBasicMempool, free) { - pool = cxBasicMempoolCreate(4); - auto basic_pool = reinterpret_cast(pool); - - void *mem1; - void *mem2; - - mem1 = cxMalloc(pool->allocator, 16); - cxFree(pool->allocator, mem1); - EXPECT_EQ(basic_pool->ndata, 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(basic_pool->ndata, 5); - cxFree(pool->allocator, mem1); - EXPECT_EQ(basic_pool->ndata, 4); - cxFree(pool->allocator, mem2); - EXPECT_EQ(basic_pool->ndata, 3); -} \ No newline at end of file diff -r 44986c0e2b05 -r d92a59f5d261 tests/test_mempool.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_mempool.cpp Wed Jun 28 20:07:52 2023 +0200 @@ -0,0 +1,153 @@ +/* + * 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 + +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); +}