#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
     1.1 --- a/src/basic_mempool.c	Wed Aug 03 17:27:55 2022 +0200
     1.2 +++ b/src/basic_mempool.c	Mon Aug 08 17:12:00 2022 +0200
     1.3 @@ -80,8 +80,7 @@
     1.4          }
     1.5      }
     1.6  
     1.7 -    cx_basic_mempool_memory *mem = cxMalloc(pool->allocator,
     1.8 -                                            sizeof(cx_destructor_func) + n);
     1.9 +    cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n);
    1.10      if (mem == NULL) {
    1.11          return NULL;
    1.12      }
    1.13 @@ -119,8 +118,7 @@
    1.14      struct cx_basic_mempool_s *pool = data;
    1.15  
    1.16      char *mem = ((char *) ptr) - sizeof(cx_destructor_func);
    1.17 -    char *newm = (char *) cxRealloc(pool->allocator, mem,
    1.18 -                                    n + sizeof(cx_destructor_func));
    1.19 +    char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func));
    1.20      if (newm == NULL) {
    1.21          return NULL;
    1.22      }
    1.23 @@ -150,7 +148,7 @@
    1.24              if (mem->destructor != NULL) {
    1.25                  mem->destructor(&(mem->c));
    1.26              }
    1.27 -            cxFree(pool->allocator, mem);
    1.28 +            free(mem);
    1.29              size_t last_index = pool->ndata - 1;
    1.30              if (i != last_index) {
    1.31                  pool->data[i] = pool->data[last_index];
    1.32 @@ -172,7 +170,7 @@
    1.33              if (mem->destructor) {
    1.34                  mem->destructor(&(mem->c));
    1.35              }
    1.36 -            cxFree(pool->allocator, mem);
    1.37 +            free(mem);
    1.38          }
    1.39      }
    1.40      free(pool->data);
    1.41 @@ -200,10 +198,7 @@
    1.42          cx_basic_mempool_set_destr,
    1.43  };
    1.44  
    1.45 -CxMempool *cxBasicMempoolCreate(
    1.46 -        size_t capacity,
    1.47 -        CxAllocator *allocator
    1.48 -) {
    1.49 +CxMempool *cxBasicMempoolCreate(size_t capacity) {
    1.50      size_t poolsize;
    1.51      if (cx_szmul(capacity, sizeof(void *), &poolsize)) {
    1.52          return NULL;
    1.53 @@ -236,7 +231,6 @@
    1.54  
    1.55      pool->ndata = 0;
    1.56      pool->size = capacity;
    1.57 -    pool->allocator = allocator;
    1.58  
    1.59      return (CxMempool *) pool;
    1.60  }
     2.1 --- a/src/cx/basic_mempool.h	Wed Aug 03 17:27:55 2022 +0200
     2.2 +++ b/src/cx/basic_mempool.h	Mon Aug 08 17:12:00 2022 +0200
     2.3 @@ -50,9 +50,6 @@
     2.4      /** Inherit base structure members. */
     2.5      CxMempool base;
     2.6  
     2.7 -    /** The underlying allocator. */
     2.8 -    CxAllocator *allocator;
     2.9 -
    2.10      /** List of pointers to pooled memory. */
    2.11      void **data;
    2.12  
    2.13 @@ -67,28 +64,10 @@
    2.14   * Creates a basic array-based memory pool.
    2.15   *
    2.16   * @param capacity the initial capacity of the pool
    2.17 - * @param allocator the underlying allocator
    2.18   * @return the created memory pool or \c NULL if allocation failed
    2.19   */
    2.20  __attribute__((__warn_unused_result__))
    2.21 -CxMempool *cxBasicMempoolCreate(
    2.22 -        size_t capacity,
    2.23 -        CxAllocator *allocator
    2.24 -);
    2.25 -
    2.26 -
    2.27 -/**
    2.28 - * Creates a basic array-based memory pool.
    2.29 - *
    2.30 - * The pool will use the default standard library allocator.
    2.31 - *
    2.32 - * @param capacity the initial capacity of the pool
    2.33 - * @return the created memory pool or \c NULL if allocation failed
    2.34 - */
    2.35 -__attribute__((__warn_unused_result__))
    2.36 -static inline CxMempool *cxBasicMempoolCreateSimple(size_t capacity) {
    2.37 -    return cxBasicMempoolCreate(capacity, cxDefaultAllocator);
    2.38 -}
    2.39 +CxMempool *cxBasicMempoolCreate(size_t capacity);
    2.40  
    2.41  #ifdef __cplusplus
    2.42  } // extern "C"
     3.1 --- a/test/test_basic_mempool.cpp	Wed Aug 03 17:27:55 2022 +0200
     3.2 +++ b/test/test_basic_mempool.cpp	Mon Aug 08 17:12:00 2022 +0200
     3.3 @@ -32,19 +32,17 @@
     3.4  
     3.5  class CxBasicMempool : public ::testing::Test {
     3.6  protected:
     3.7 -    CxTestingAllocator testingAllocator;
     3.8      CxMempool *pool = nullptr;
     3.9  
    3.10      void TearDown() override {
    3.11          if (pool != nullptr) {
    3.12              cxMempoolDestroy(pool);
    3.13          }
    3.14 -        EXPECT_TRUE(testingAllocator.verify());
    3.15      }
    3.16  };
    3.17  
    3.18  TEST_F(CxBasicMempool, Create) {
    3.19 -    pool = cxBasicMempoolCreateSimple(16);
    3.20 +    pool = cxBasicMempoolCreate(16);
    3.21      ASSERT_NE(pool->allocator, nullptr);
    3.22      ASSERT_NE(pool->cl, nullptr);
    3.23      EXPECT_NE(pool->cl->destroy, nullptr);
    3.24 @@ -56,41 +54,35 @@
    3.25      EXPECT_NE(pool->allocator->cl->free, nullptr);
    3.26  
    3.27      auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
    3.28 -    EXPECT_EQ(basic_pool->allocator, cxDefaultAllocator);
    3.29      EXPECT_EQ(basic_pool->size, 16);
    3.30      EXPECT_EQ(basic_pool->ndata, 0);
    3.31      EXPECT_NE(basic_pool->data, nullptr);
    3.32  }
    3.33  
    3.34  TEST_F(CxBasicMempool, malloc) {
    3.35 -    pool = cxBasicMempoolCreate(4, &testingAllocator);
    3.36 +    pool = cxBasicMempoolCreate(4);
    3.37      auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
    3.38      EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
    3.39      EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
    3.40 -    EXPECT_EQ(testingAllocator.alloc_total, 2);
    3.41      EXPECT_EQ(basic_pool->ndata, 2);
    3.42      EXPECT_EQ(basic_pool->size, 4);
    3.43      EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
    3.44      EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
    3.45 -    EXPECT_EQ(testingAllocator.alloc_total, 4);
    3.46      EXPECT_EQ(basic_pool->ndata, 4);
    3.47      EXPECT_EQ(basic_pool->size, 4);
    3.48      EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
    3.49      EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr);
    3.50 -    EXPECT_EQ(testingAllocator.alloc_total, 6);
    3.51      EXPECT_EQ(basic_pool->ndata, 6);
    3.52      EXPECT_GE(basic_pool->size, 6);
    3.53 -    EXPECT_TRUE(testingAllocator.used());
    3.54  }
    3.55  
    3.56  TEST_F(CxBasicMempool, calloc) {
    3.57 -    pool = cxBasicMempoolCreate(4, &testingAllocator);
    3.58 +    pool = cxBasicMempoolCreate(4);
    3.59  
    3.60      auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int));
    3.61      ASSERT_NE(test, nullptr);
    3.62      EXPECT_EQ(test[0], 0);
    3.63      EXPECT_EQ(test[1], 0);
    3.64 -    EXPECT_TRUE(testingAllocator.used());
    3.65  }
    3.66  
    3.67  static unsigned test_destructor_called = 0;
    3.68 @@ -100,7 +92,7 @@
    3.69  }
    3.70  
    3.71  TEST_F(CxBasicMempool, destructor) {
    3.72 -    pool = cxBasicMempoolCreate(4, &testingAllocator);
    3.73 +    pool = cxBasicMempoolCreate(4);
    3.74      auto data = cxMalloc(pool->allocator, sizeof(int));
    3.75      *((int *) data) = 13;
    3.76      cxMempoolSetDestructor(pool, data, test_destructor);
    3.77 @@ -116,7 +108,7 @@
    3.78  }
    3.79  
    3.80  TEST_F(CxBasicMempool, realloc) {
    3.81 -    pool = cxBasicMempoolCreate(4, &testingAllocator);
    3.82 +    pool = cxBasicMempoolCreate(4);
    3.83      auto data = cxMalloc(pool->allocator, sizeof(int));
    3.84      *((int *) data) = 13;
    3.85      cxMempoolSetDestructor(pool, data, test_destructor);
    3.86 @@ -138,7 +130,7 @@
    3.87  
    3.88  
    3.89  TEST_F(CxBasicMempool, free) {
    3.90 -    pool = cxBasicMempoolCreate(4, &testingAllocator);
    3.91 +    pool = cxBasicMempoolCreate(4);
    3.92      auto basic_pool = reinterpret_cast<cx_basic_mempool_s *>(pool);
    3.93  
    3.94      void *mem1;

mercurial