#201 - add mempool implementation

Wed, 03 Aug 2022 17:27:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 03 Aug 2022 17:27:55 +0200
changeset 571
f83583a0bbac
parent 570
7edce1b5a798
child 572
f0f99dd06d9f

#201 - add mempool implementation

src/CMakeLists.txt file | annotate | diff | comparison | revisions
src/basic_mempool.c file | annotate | diff | comparison | revisions
src/cx/basic_mempool.h file | annotate | diff | comparison | revisions
src/cx/mempool.h file | annotate | diff | comparison | revisions
test/CMakeLists.txt file | annotate | diff | comparison | revisions
test/test_basic_mempool.cpp file | annotate | diff | comparison | revisions
test/util_allocator.cpp file | annotate | diff | comparison | revisions
test/util_allocator.h file | annotate | diff | comparison | revisions
--- a/src/CMakeLists.txt	Wed Aug 03 15:44:46 2022 +0200
+++ b/src/CMakeLists.txt	Wed Aug 03 17:27:55 2022 +0200
@@ -7,6 +7,7 @@
         buffer.c
         hash_key.c
         hash_map.c
+        basic_mempool.c
 )
 set(headers
         cx/common.h
@@ -20,6 +21,8 @@
         cx/map.h
         cx/hash_key.h
         cx/hash_map.h
+        cx/mempool.h
+        cx/basic_mempool.h
         )
 
 add_library(ucx SHARED ${sources})
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/basic_mempool.c	Wed Aug 03 17:27:55 2022 +0200
@@ -0,0 +1,242 @@
+/*
+ * 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 <stdint.h>
+#include <string.h>
+
+#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 = cxMalloc(pool->allocator,
+                                            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 *) cxRealloc(pool->allocator, 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));
+            }
+            cxFree(pool->allocator, 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));
+            }
+            cxFree(pool->allocator, 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,
+        CxAllocator *allocator
+) {
+    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;
+    pool->allocator = allocator;
+
+    return (CxMempool *) pool;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cx/basic_mempool.h	Wed Aug 03 17:27:55 2022 +0200
@@ -0,0 +1,97 @@
+/*
+ * 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;
+
+    /** The underlying allocator. */
+    CxAllocator *allocator;
+
+    /** 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
+ * @param allocator the underlying allocator
+ * @return the created memory pool or \c NULL if allocation failed
+ */
+__attribute__((__warn_unused_result__))
+CxMempool *cxBasicMempoolCreate(
+        size_t capacity,
+        CxAllocator *allocator
+);
+
+
+/**
+ * Creates a basic array-based memory pool.
+ *
+ * The pool will use the default standard library allocator.
+ *
+ * @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 *cxBasicMempoolCreateSimple(size_t capacity) {
+    return cxBasicMempoolCreate(capacity, cxDefaultAllocator);
+}
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif // UCX_BASIC_MEMPOOL_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cx/mempool.h	Wed Aug 03 17:27:55 2022 +0200
@@ -0,0 +1,121 @@
+/*
+ * 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 mempool.h
+ * \brief Interface for memory pool implementations.
+ * \author Mike Becker
+ * \author Olaf Wintermann
+ * \version 3.0
+ * \copyright 2-Clause BSD License
+ */
+
+#ifndef UCX_MEMPOOL_H
+#define UCX_MEMPOOL_H
+
+#include "allocator.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Memory pool class type.
+ */
+typedef struct cx_mempool_class_s cx_mempool_class;
+
+/**
+ * The basic structure of a memory pool.
+ * Should be the first member of an actual memory pool implementation.
+ */
+struct cx_mempool_s {
+    /**
+     * The pool class definition.
+     */
+    cx_mempool_class *cl;
+    /**
+     * The provided allocator.
+     */
+    CxAllocator const *allocator;
+};
+
+/**
+ * Common type for all memory pool implementations.
+ */
+typedef struct cx_mempool_s CxMempool;
+
+/**
+ * The class definition for a memory pool.
+ */
+struct cx_mempool_class_s {
+    /** Member function for destroying the pool. */
+    __attribute__((__nonnull__))
+    void (*destroy)(CxMempool *pool);
+
+    /** Member function for setting a destructor. */
+    __attribute__((__nonnull__))
+    void (*set_destructor)(
+            CxMempool *pool,
+            void *memory,
+            cx_destructor_func fnc
+    );
+};
+
+
+/**
+ * Destroys a memory pool including their contents.
+ *
+ * @param pool the memory pool to destroy
+ */
+__attribute__((__nonnull__))
+static inline void cxMempoolDestroy(CxMempool *pool) {
+    pool->cl->destroy(pool);
+}
+
+/**
+ * Sets a destructor function for an allocated memory object.
+ *
+ * If the memory is not managed by the pool, the behavior is undefined.
+ *
+ * @param pool the pool
+ * @param memory the objected allocated in the pool
+ * @param fnc the destructor function
+ */
+__attribute__((__nonnull__))
+static inline void cxMempoolSetDestructor(
+        CxMempool *pool,
+        void *memory,
+        cx_destructor_func fnc
+) {
+    pool->cl->set_destructor(pool, memory, fnc);
+}
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif // UCX_MEMPOOL_H
--- a/test/CMakeLists.txt	Wed Aug 03 15:44:46 2022 +0200
+++ b/test/CMakeLists.txt	Wed Aug 03 17:27:55 2022 +0200
@@ -19,6 +19,7 @@
         test_list.cpp
         test_tree.cpp
         test_map.cpp
+        test_basic_mempool.cpp
         selftest.cpp
         util_allocator.cpp
         )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test_basic_mempool.cpp	Wed Aug 03 17:27:55 2022 +0200
@@ -0,0 +1,162 @@
+/*
+ * 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 <gtest/gtest.h>
+
+class CxBasicMempool : public ::testing::Test {
+protected:
+    CxTestingAllocator testingAllocator;
+    CxMempool *pool = nullptr;
+
+    void TearDown() override {
+        if (pool != nullptr) {
+            cxMempoolDestroy(pool);
+        }
+        EXPECT_TRUE(testingAllocator.verify());
+    }
+};
+
+TEST_F(CxBasicMempool, Create) {
+    pool = cxBasicMempoolCreateSimple(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<cx_basic_mempool_s *>(pool);
+    EXPECT_EQ(basic_pool->allocator, cxDefaultAllocator);
+    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, &testingAllocator);
+    auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
+    EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
+    EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
+    EXPECT_EQ(testingAllocator.alloc_total, 2);
+    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(testingAllocator.alloc_total, 4);
+    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(testingAllocator.alloc_total, 6);
+    EXPECT_EQ(basic_pool->ndata, 6);
+    EXPECT_GE(basic_pool->size, 6);
+    EXPECT_TRUE(testingAllocator.used());
+}
+
+TEST_F(CxBasicMempool, calloc) {
+    pool = cxBasicMempoolCreate(4, &testingAllocator);
+
+    auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int));
+    ASSERT_NE(test, nullptr);
+    EXPECT_EQ(test[0], 0);
+    EXPECT_EQ(test[1], 0);
+    EXPECT_TRUE(testingAllocator.used());
+}
+
+static unsigned test_destructor_called = 0;
+
+static void test_destructor([[maybe_unused]] void *mem) {
+    test_destructor_called++;
+}
+
+TEST_F(CxBasicMempool, destructor) {
+    pool = cxBasicMempoolCreate(4, &testingAllocator);
+    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, &testingAllocator);
+    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, &testingAllocator);
+    auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(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
--- a/test/util_allocator.cpp	Wed Aug 03 15:44:46 2022 +0200
+++ b/test/util_allocator.cpp	Wed Aug 03 17:27:55 2022 +0200
@@ -95,6 +95,10 @@
     data = this;
 }
 
+bool CxTestingAllocator::used() const {
+    return alloc_total > 0;
+}
+
 bool CxTestingAllocator::verify() const {
     return tracked.empty() && alloc_failed == 0 && free_failed == 0 && alloc_total == free_total;
 }
--- a/test/util_allocator.h	Wed Aug 03 15:44:46 2022 +0200
+++ b/test/util_allocator.h	Wed Aug 03 17:27:55 2022 +0200
@@ -64,11 +64,18 @@
     CxTestingAllocator();
 
     /**
+     * Verifies that this allocator has been used.
+     *
+     * @return true if any allocation was attempted using this allocator
+     */
+    [[nodiscard]] bool used() const;
+
+    /**
      * Verifies that all allocated memory blocks are freed and no free occurred twice.
      *
      * @return true iff all tracked allocations / deallocations were valid
      */
-    bool verify() const;
+    [[nodiscard]] bool verify() const;
 };
 
 #endif /* UCX_UTIL_ALLOCATOR_H */

mercurial