universe@571: /* universe@571: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@571: * universe@571: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@571: * universe@571: * Redistribution and use in source and binary forms, with or without universe@571: * modification, are permitted provided that the following conditions are met: universe@571: * universe@571: * 1. Redistributions of source code must retain the above copyright universe@571: * notice, this list of conditions and the following disclaimer. universe@571: * universe@571: * 2. Redistributions in binary form must reproduce the above copyright universe@571: * notice, this list of conditions and the following disclaimer in the universe@571: * documentation and/or other materials provided with the distribution. universe@571: * universe@571: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@571: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@571: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@571: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@571: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@571: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@571: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@571: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@571: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@571: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@571: * POSSIBILITY OF SUCH DAMAGE. universe@571: */ universe@571: universe@571: #include "cx/basic_mempool.h" universe@571: #include "cx/utils.h" universe@571: #include universe@571: universe@571: #define of_chk_(n) if (SIZE_MAX - sizeof(cx_destructor_func) < (n)) return NULL universe@571: universe@571: /** Internal structure for denoting pooled memory. */ universe@571: typedef struct { universe@571: /** The destructor. */ universe@571: cx_destructor_func destructor; universe@571: /** universe@571: * Access to the first byte of the polled memory. universe@571: */ universe@571: char c; universe@571: } cx_basic_mempool_memory; universe@571: universe@571: static int cx_basic_mempool_chcap( universe@571: struct cx_basic_mempool_s *pool, universe@571: size_t newcap universe@571: ) { universe@571: if (newcap < pool->ndata) { universe@571: return 1; universe@571: } universe@571: universe@571: size_t newcapsz; universe@571: if (cx_szmul(newcap, sizeof(void *), &newcapsz)) { universe@571: return 1; universe@571: } universe@571: universe@571: void **data = realloc(pool->data, newcapsz); universe@571: if (data) { universe@571: pool->data = data; universe@571: pool->size = newcap; universe@571: return 0; universe@571: } else { universe@571: return 1; universe@571: } universe@571: } universe@571: universe@571: void *cx_malloc_basic_mempool( universe@571: void *data, universe@571: size_t n universe@571: ) { universe@571: of_chk_(n); universe@571: struct cx_basic_mempool_s *pool = data; universe@571: universe@571: if (pool->ndata >= pool->size) { universe@571: size_t newcap = pool->size * 2; universe@571: if (newcap < pool->size || cx_basic_mempool_chcap(pool, newcap)) { universe@571: return NULL; universe@571: } universe@571: } universe@571: universe@572: cx_basic_mempool_memory *mem = malloc(sizeof(cx_destructor_func) + n); universe@571: if (mem == NULL) { universe@571: return NULL; universe@571: } universe@571: universe@571: mem->destructor = NULL; universe@571: pool->data[pool->ndata] = mem; universe@571: pool->ndata++; universe@571: universe@571: return &(mem->c); universe@571: } universe@571: universe@571: void *cx_calloc_basic_mempool( universe@571: void *data, universe@571: size_t nelem, universe@571: size_t elsize universe@571: ) { universe@571: size_t msz; universe@571: if (cx_szmul(nelem, elsize, &msz)) { universe@571: return NULL; universe@571: } universe@571: void *ptr = cx_malloc_basic_mempool(data, msz); universe@571: if (ptr == NULL) { universe@571: return NULL; universe@571: } universe@571: memset(ptr, 0, nelem * elsize); universe@571: return ptr; universe@571: } universe@571: universe@571: void *cx_realloc_basic_mempool( universe@571: void *data, universe@571: void *ptr, universe@571: size_t n universe@571: ) { universe@571: of_chk_(n); universe@571: struct cx_basic_mempool_s *pool = data; universe@571: universe@571: char *mem = ((char *) ptr) - sizeof(cx_destructor_func); universe@572: char *newm = (char *) realloc(mem, n + sizeof(cx_destructor_func)); universe@571: if (newm == NULL) { universe@571: return NULL; universe@571: } universe@571: if (mem != newm) { universe@571: cx_for_n(i, pool->ndata) { universe@571: if (pool->data[i] == mem) { universe@571: pool->data[i] = newm; universe@571: return newm + sizeof(cx_destructor_func); universe@571: } universe@571: } universe@571: abort(); universe@571: } else { universe@571: return newm + sizeof(cx_destructor_func); universe@571: } universe@571: } universe@571: universe@571: void cx_free_basic_mempool( universe@571: void *data, universe@571: void *ptr universe@571: ) { universe@571: struct cx_basic_mempool_s *pool = data; universe@571: universe@571: cx_basic_mempool_memory *mem = (cx_basic_mempool_memory *) universe@571: ((char *) ptr - sizeof(cx_destructor_func)); universe@571: cx_for_n(i, pool->ndata) { universe@571: if (mem == pool->data[i]) { universe@571: if (mem->destructor != NULL) { universe@571: mem->destructor(&(mem->c)); universe@571: } universe@572: free(mem); universe@571: size_t last_index = pool->ndata - 1; universe@571: if (i != last_index) { universe@571: pool->data[i] = pool->data[last_index]; universe@571: pool->data[last_index] = NULL; universe@571: } universe@571: pool->ndata--; universe@571: return; universe@571: } universe@571: } universe@571: abort(); universe@571: } universe@571: universe@571: void cx_basic_mempool_destroy(CxMempool *p) { universe@571: struct cx_basic_mempool_s *pool = (struct cx_basic_mempool_s *) p; universe@571: cx_basic_mempool_memory *mem; universe@571: cx_for_n(i, pool->ndata) { universe@571: mem = (cx_basic_mempool_memory *) pool->data[i]; universe@571: if (mem) { universe@571: if (mem->destructor) { universe@571: mem->destructor(&(mem->c)); universe@571: } universe@572: free(mem); universe@571: } universe@571: } universe@571: free(pool->data); universe@571: free((void *) p->allocator); universe@571: free(pool); universe@571: } universe@571: universe@571: void cx_basic_mempool_set_destr( universe@571: __attribute__((__unused__)) CxMempool *pool, universe@571: void *ptr, universe@571: cx_destructor_func func universe@571: ) { universe@571: *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func; universe@571: } universe@571: universe@571: static cx_allocator_class cx_basic_mempool_allocator_class = { universe@571: cx_malloc_basic_mempool, universe@571: cx_realloc_basic_mempool, universe@571: cx_calloc_basic_mempool, universe@571: cx_free_basic_mempool universe@571: }; universe@571: universe@571: static cx_mempool_class cx_basic_mempool_class = { universe@571: cx_basic_mempool_destroy, universe@571: cx_basic_mempool_set_destr, universe@571: }; universe@571: universe@572: CxMempool *cxBasicMempoolCreate(size_t capacity) { universe@571: size_t poolsize; universe@571: if (cx_szmul(capacity, sizeof(void *), &poolsize)) { universe@571: return NULL; universe@571: } universe@571: universe@571: struct cx_basic_mempool_s *pool = universe@571: malloc(sizeof(struct cx_basic_mempool_s)); universe@571: if (pool == NULL) { universe@571: return NULL; universe@571: } universe@571: universe@571: universe@571: CxAllocator *provided_allocator = malloc(sizeof(CxAllocator)); universe@571: if (!provided_allocator) { universe@571: free(pool); universe@571: return NULL; universe@571: } universe@571: provided_allocator->cl = &cx_basic_mempool_allocator_class; universe@571: provided_allocator->data = pool; universe@571: universe@571: pool->base.cl = &cx_basic_mempool_class; universe@571: pool->base.allocator = provided_allocator; universe@571: universe@571: pool->data = malloc(poolsize); universe@571: if (pool->data == NULL) { universe@571: free(provided_allocator); universe@571: free(pool); universe@571: return NULL; universe@571: } universe@571: universe@571: pool->ndata = 0; universe@571: pool->size = capacity; universe@571: universe@571: return (CxMempool *) pool; universe@571: }