--- a/ucx/mempool.c Thu Nov 28 17:53:13 2024 +0100 +++ b/ucx/mempool.c Mon Jan 06 21:18:36 2025 +0100 @@ -27,8 +27,9 @@ */ #include "cx/mempool.h" -#include "cx/utils.h" + #include <string.h> +#include <errno.h> struct cx_mempool_memory_s { /** The destructor. */ @@ -45,18 +46,20 @@ 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) { + size_t newmsize; + if (pool->capacity > newcap || cx_szmul(newcap, + sizeof(struct cx_mempool_memory_s*), &newmsize)) { + errno = EOVERFLOW; return NULL; } + struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize); + 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; - } + if (mem == NULL) return NULL; mem->destructor = pool->auto_destr; pool->data[pool->size] = mem; @@ -72,12 +75,11 @@ ) { size_t msz; if (cx_szmul(nelem, elsize, &msz)) { + errno = EOVERFLOW; return NULL; } void *ptr = cx_mempool_malloc(p, msz); - if (ptr == NULL) { - return NULL; - } + if (ptr == NULL) return NULL; memset(ptr, 0, nelem * elsize); return ptr; } @@ -93,17 +95,15 @@ 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 (newm == NULL) return NULL; if (mem != newm) { - cx_for_n(i, pool->size) { + for (size_t i = 0; i < pool->size; i++) { if (pool->data[i] == mem) { pool->data[i] = newm; return ((char*)newm) + sizeof(cx_destructor_func); } } - abort(); + abort(); // LCOV_EXCL_LINE } else { return ptr; } @@ -113,13 +113,13 @@ void *p, void *ptr ) { - if(!ptr) return; + if (!ptr) return; 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) { + for (size_t i = 0; i < pool->size; i++) { if (mem == pool->data[i]) { if (mem->destructor) { mem->destructor(mem->c); @@ -134,12 +134,13 @@ return; } } - abort(); + abort(); // LCOV_EXCL_LINE } -void cxMempoolDestroy(CxMempool *pool) { +void cxMempoolFree(CxMempool *pool) { + if (pool == NULL) return; struct cx_mempool_memory_s *mem; - cx_for_n(i, pool->size) { + for (size_t i = 0; i < pool->size; i++) { mem = pool->data[i]; if (mem->destructor) { mem->destructor(mem->c); @@ -158,6 +159,10 @@ *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; } +void cxMempoolRemoveDestructor(void *ptr) { + *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = NULL; +} + struct cx_mempool_foreign_mem_s { cx_destructor_func destr; void* mem; @@ -199,35 +204,34 @@ ) { size_t poolsize; if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) { + errno = EOVERFLOW; return NULL; } struct cx_mempool_s *pool = malloc(sizeof(struct cx_mempool_s)); - if (pool == NULL) { - return NULL; - } + if (pool == NULL) return NULL; CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); - if (provided_allocator == NULL) { + if (provided_allocator == NULL) { // LCOV_EXCL_START free(pool); return NULL; - } + } // LCOV_EXCL_STOP provided_allocator->cl = &cx_mempool_allocator_class; provided_allocator->data = pool; pool->allocator = provided_allocator; pool->data = malloc(poolsize); - if (pool->data == NULL) { + if (pool->data == NULL) { // LCOV_EXCL_START free(provided_allocator); free(pool); return NULL; - } + } // LCOV_EXCL_STOP pool->size = 0; pool->capacity = capacity; pool->auto_destr = destr; - return (CxMempool *) pool; + return pool; }