#201 - remove dangerous allocator config

Mon, 08 Aug 2022 17:12:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Aug 2022 17:12:00 +0200
changeset 572
f0f99dd06d9f
parent 571
f83583a0bbac
child 573
3f3a0d19db58

#201 - remove dangerous allocator config

There is no plausible use case, except using the testing
allocator in the test case, and having the possibility to
specify any allocator (including another mempool) causes
more harm than good.

src/basic_mempool.c file | annotate | diff | comparison | revisions
src/cx/basic_mempool.h file | annotate | diff | comparison | revisions
test/test_basic_mempool.cpp file | annotate | diff | comparison | revisions
--- a/src/basic_mempool.c	Wed Aug 03 17:27:55 2022 +0200
+++ b/src/basic_mempool.c	Mon Aug 08 17:12:00 2022 +0200
@@ -80,8 +80,7 @@
         }
     }
 
-    cx_basic_mempool_memory *mem = cxMalloc(pool->allocator,
-                                            sizeof(cx_destructor_func) + n);
+    cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n);
     if (mem == NULL) {
         return NULL;
     }
@@ -119,8 +118,7 @@
     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));
+    char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func));
     if (newm == NULL) {
         return NULL;
     }
@@ -150,7 +148,7 @@
             if (mem->destructor != NULL) {
                 mem->destructor(&(mem->c));
             }
-            cxFree(pool->allocator, mem);
+            free(mem);
             size_t last_index = pool->ndata - 1;
             if (i != last_index) {
                 pool->data[i] = pool->data[last_index];
@@ -172,7 +170,7 @@
             if (mem->destructor) {
                 mem->destructor(&(mem->c));
             }
-            cxFree(pool->allocator, mem);
+            free(mem);
         }
     }
     free(pool->data);
@@ -200,10 +198,7 @@
         cx_basic_mempool_set_destr,
 };
 
-CxMempool *cxBasicMempoolCreate(
-        size_t capacity,
-        CxAllocator *allocator
-) {
+CxMempool *cxBasicMempoolCreate(size_t capacity) {
     size_t poolsize;
     if (cx_szmul(capacity, sizeof(void *), &poolsize)) {
         return NULL;
@@ -236,7 +231,6 @@
 
     pool->ndata = 0;
     pool->size = capacity;
-    pool->allocator = allocator;
 
     return (CxMempool *) pool;
 }
--- a/src/cx/basic_mempool.h	Wed Aug 03 17:27:55 2022 +0200
+++ b/src/cx/basic_mempool.h	Mon Aug 08 17:12:00 2022 +0200
@@ -50,9 +50,6 @@
     /** Inherit base structure members. */
     CxMempool base;
 
-    /** The underlying allocator. */
-    CxAllocator *allocator;
-
     /** List of pointers to pooled memory. */
     void **data;
 
@@ -67,28 +64,10 @@
  * 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);
-}
+CxMempool *cxBasicMempoolCreate(size_t capacity);
 
 #ifdef __cplusplus
 } // extern "C"
--- a/test/test_basic_mempool.cpp	Wed Aug 03 17:27:55 2022 +0200
+++ b/test/test_basic_mempool.cpp	Mon Aug 08 17:12:00 2022 +0200
@@ -32,19 +32,17 @@
 
 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);
+    pool = cxBasicMempoolCreate(16);
     ASSERT_NE(pool->allocator, nullptr);
     ASSERT_NE(pool->cl, nullptr);
     EXPECT_NE(pool->cl->destroy, nullptr);
@@ -56,41 +54,35 @@
     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);
+    pool = cxBasicMempoolCreate(4);
     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);
+    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);
-    EXPECT_TRUE(testingAllocator.used());
 }
 
 static unsigned test_destructor_called = 0;
@@ -100,7 +92,7 @@
 }
 
 TEST_F(CxBasicMempool, destructor) {
-    pool = cxBasicMempoolCreate(4, &testingAllocator);
+    pool = cxBasicMempoolCreate(4);
     auto data = cxMalloc(pool->allocator, sizeof(int));
     *((int *) data) = 13;
     cxMempoolSetDestructor(pool, data, test_destructor);
@@ -116,7 +108,7 @@
 }
 
 TEST_F(CxBasicMempool, realloc) {
-    pool = cxBasicMempoolCreate(4, &testingAllocator);
+    pool = cxBasicMempoolCreate(4);
     auto data = cxMalloc(pool->allocator, sizeof(int));
     *((int *) data) = 13;
     cxMempoolSetDestructor(pool, data, test_destructor);
@@ -138,7 +130,7 @@
 
 
 TEST_F(CxBasicMempool, free) {
-    pool = cxBasicMempoolCreate(4, &testingAllocator);
+    pool = cxBasicMempoolCreate(4);
     auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
 
     void *mem1;

mercurial