olaf@13: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. olaf@13: * universe@259: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@13: */ olaf@13: universe@251: #include "ucx/mempool.h" universe@251: olaf@13: #include olaf@13: #include olaf@13: #include universe@95: #ifdef __cplusplus universe@95: #define __STDC_FORMAT_MACROS universe@95: #endif universe@75: #include olaf@13: universe@138: /** Capsule for destructible memory chunks. */ olaf@13: typedef struct { universe@138: /** The destructor for the memory chunk. */ olaf@13: ucx_destructor destructor; universe@138: /** universe@138: * First byte of the memory chunk. universe@138: * Note, that the address &c is also the address universe@138: * of the whole memory chunk. universe@138: */ olaf@13: char c; olaf@13: } ucx_memchunk; olaf@13: universe@138: /** Capsule for data and its destructor. */ olaf@13: typedef struct { universe@138: /** The destructor for the data. */ olaf@13: ucx_destructor destructor; universe@138: /** A pointer to the data. */ olaf@13: void *ptr; olaf@13: } ucx_regdestr; olaf@13: universe@253: #ifdef __cplusplus universe@253: extern "C" universe@253: #endif universe@253: void ucx_mempool_shared_destr(void* ptr) { olaf@13: ucx_regdestr *rd = (ucx_regdestr*)ptr; olaf@13: rd->destructor(rd->ptr); olaf@13: } olaf@13: olaf@13: UcxMempool *ucx_mempool_new(size_t n) { olaf@14: UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); universe@135: if (!pool) { universe@135: return NULL; universe@135: } universe@15: universe@69: pool->data = (void**) malloc(n * sizeof(void*)); universe@15: if (pool->data == NULL) { universe@15: free(pool); universe@15: return NULL; universe@15: } universe@15: olaf@13: pool->ndata = 0; olaf@13: pool->size = n; olaf@158: olaf@158: UcxAllocator *allocator = (UcxAllocator*)malloc(sizeof(UcxAllocator)); olaf@158: if(!allocator) { olaf@158: free(pool->data); olaf@158: free(pool); olaf@158: return NULL; olaf@158: } olaf@158: allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; olaf@158: allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; olaf@158: allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; olaf@158: allocator->free = (ucx_allocator_free)ucx_mempool_free; olaf@158: allocator->pool = pool; olaf@158: pool->allocator = allocator; olaf@158: olaf@13: return pool; olaf@13: } olaf@13: universe@15: int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { universe@241: if (newcap < pool->ndata) { universe@241: return 1; universe@241: } universe@241: universe@69: void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); universe@135: if (data) { universe@15: pool->data = data; universe@15: pool->size = newcap; universe@241: return 0; universe@135: } else { universe@241: return 1; universe@15: } olaf@13: } olaf@13: olaf@13: void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { universe@141: if (pool->ndata >= pool->size) { universe@242: size_t newcap = pool->size*2; universe@242: if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) { universe@141: return NULL; universe@141: } universe@141: } universe@141: universe@248: void *p = malloc(sizeof(ucx_destructor) + n); universe@248: ucx_memchunk *mem = (ucx_memchunk*)p; universe@135: if (!mem) { universe@135: return NULL; universe@135: } olaf@13: olaf@13: mem->destructor = NULL; olaf@13: pool->data[pool->ndata] = mem; olaf@13: pool->ndata++; olaf@13: universe@135: return &(mem->c); olaf@13: } olaf@13: olaf@13: void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { olaf@13: void *ptr = ucx_mempool_malloc(pool, nelem*elsize); universe@135: if (!ptr) { olaf@13: return NULL; olaf@13: } olaf@13: memset(ptr, 0, nelem * elsize); olaf@13: return ptr; olaf@13: } olaf@13: olaf@13: void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { universe@28: char *mem = ((char*)ptr) - sizeof(ucx_destructor); universe@15: char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); universe@135: if (!newm) { universe@135: return NULL; universe@135: } universe@15: if (mem != newm) { universe@95: for(size_t i=0 ; i < pool->ndata ; i++) { olaf@14: if(pool->data[i] == mem) { olaf@14: pool->data[i] = newm; universe@28: return newm + sizeof(ucx_destructor); olaf@14: } olaf@13: } universe@116: fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", universe@75: (intptr_t)ptr, (intptr_t)pool); universe@241: abort(); universe@16: } else { universe@28: return newm + sizeof(ucx_destructor); olaf@13: } olaf@13: } olaf@13: olaf@113: void ucx_mempool_free(UcxMempool *pool, void *ptr) { olaf@113: ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); olaf@113: for(size_t i=0 ; indata ; i++) { olaf@113: if(chunk == pool->data[i]) { olaf@113: if(chunk->destructor != NULL) { universe@141: chunk->destructor(&(chunk->c)); olaf@113: } olaf@113: free(chunk); olaf@113: size_t last_index = pool->ndata - 1; olaf@113: if(i != last_index) { olaf@113: pool->data[i] = pool->data[last_index]; universe@141: pool->data[last_index] = NULL; olaf@113: } olaf@113: pool->ndata--; olaf@113: return; olaf@113: } olaf@113: } universe@116: fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", olaf@113: (intptr_t)ptr, (intptr_t)pool); universe@240: abort(); olaf@113: } olaf@113: olaf@113: void ucx_mempool_destroy(UcxMempool *pool) { olaf@13: ucx_memchunk *chunk; universe@95: for(size_t i=0 ; indata ; i++) { olaf@13: chunk = (ucx_memchunk*) pool->data[i]; olaf@113: if(chunk) { universe@135: if(chunk->destructor) { universe@135: chunk->destructor(&(chunk->c)); olaf@113: } olaf@113: free(chunk); olaf@13: } olaf@13: } olaf@13: free(pool->data); olaf@158: free(pool->allocator); olaf@13: free(pool); olaf@13: } olaf@13: olaf@13: void ucx_mempool_set_destr(void *ptr, ucx_destructor func) { olaf@13: *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func; olaf@13: } olaf@13: olaf@13: void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) { olaf@13: ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc( olaf@13: pool, olaf@13: sizeof(ucx_regdestr)); olaf@13: rd->destructor = destr; olaf@14: rd->ptr = ptr; olaf@13: ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); olaf@13: } olaf@113: