-UcxMempool *ucx_mempool_new(size_t n) {
- size_t poolsz;
- if(ucx_szmul(n, sizeof(void*), &poolsz)) {
- return NULL;
- }
-
- UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
- if (!pool) {
- return NULL;
- }
-
- pool->data = (void**) malloc(poolsz);
- if (pool->data == NULL) {
- free(pool);
- return NULL;
- }
-
- pool->ndata = 0;
- pool->size = n;
-
- UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator));
- if(!allocator) {
- free(pool->data);
- free(pool);
- return NULL;
- }
- allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
- allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
- allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
- allocator->free = (ucx_allocator_free)ucx_mempool_free;
- allocator->pool = pool;
- pool->allocator = allocator;
-
- return pool;
-}
-
-int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
- if (newcap < pool->ndata) {
- return 1;
- }
-
- size_t newcapsz;
- if(ucx_szmul(newcap, sizeof(void*), &newcapsz)) {
- return 1;
- }
-
- void **data = (void**) realloc(pool->data, newcapsz);
- if (data) {
- pool->data = data;
- pool->size = newcap;
- return 0;
- } else {
- return 1;
- }
-}
-
-void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
- if(((size_t)-1) - sizeof(ucx_destructor) < n) {
- return NULL;
- }
-
- if (pool->ndata >= pool->size) {
- size_t newcap = pool->size*2;
- if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
+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) {