--- a/ucx/mempool.c Sat Dec 01 20:34:55 2012 +0100 +++ b/ucx/mempool.c Mon Aug 12 14:40:19 2013 +0200 @@ -1,10 +1,37 @@ /* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ #include <stdlib.h> #include <string.h> #include <stdio.h> +#ifdef __cplusplus +#define __STDC_FORMAT_MACROS +#endif #include <inttypes.h> #include "mempool.h" @@ -19,14 +46,16 @@ void *ptr; } ucx_regdestr; -void ucx_mempool_shared_destr(void* ptr) { +UCX_EXTERN void ucx_mempool_shared_destr(void* ptr) { ucx_regdestr *rd = (ucx_regdestr*)ptr; rd->destructor(rd->ptr); } UcxMempool *ucx_mempool_new(size_t n) { UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); - if (pool == NULL) return NULL; + if (!pool) { + return NULL; + } pool->data = (void**) malloc(n * sizeof(void*)); if (pool->data == NULL) { @@ -41,20 +70,23 @@ int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); - if (data == NULL) { - return 1; - } else { + if (data) { pool->data = data; pool->size = newcap; return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; } } void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); - if (mem == NULL) return NULL; + if (!mem) { + return NULL; + } if (pool->ndata >= pool->size) { + // The hard coded 16 is documented for this function and ucx_mempool_new ucx_mempool_chcap(pool, pool->size + 16); } @@ -62,12 +94,12 @@ pool->data[pool->ndata] = mem; pool->ndata++; - return &mem->c; + return &(mem->c); } void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { void *ptr = ucx_mempool_malloc(pool, nelem*elsize); - if(ptr == NULL) { + if (!ptr) { return NULL; } memset(ptr, 0, nelem * elsize); @@ -77,15 +109,17 @@ void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { char *mem = ((char*)ptr) - sizeof(ucx_destructor); char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); - if (newm == NULL) return NULL; + if (!newm) { + return NULL; + } if (mem != newm) { - for(int i=0;i<pool->ndata;i++) { + for(size_t i=0 ; i < pool->ndata ; i++) { if(pool->data[i] == mem) { pool->data[i] = newm; return newm + sizeof(ucx_destructor); } } - fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08"PRIxPTR"\n", + fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", (intptr_t)ptr, (intptr_t)pool); exit(1); } else { @@ -93,14 +127,37 @@ } } -void ucx_mempool_free(UcxMempool *pool) { +void ucx_mempool_free(UcxMempool *pool, void *ptr) { + ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); + for(size_t i=0 ; i<pool->ndata ; i++) { + if(chunk == pool->data[i]) { + if(chunk->destructor != NULL) { + chunk->destructor(&chunk->c); + } + free(chunk); + size_t last_index = pool->ndata - 1; + if(i != last_index) { + pool->data[i] = pool->data[last_index]; + } + pool->ndata--; + return; + } + } + fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", + (intptr_t)ptr, (intptr_t)pool); + exit(EXIT_FAILURE); +} + +void ucx_mempool_destroy(UcxMempool *pool) { ucx_memchunk *chunk; - for(int i=0;i<pool->ndata;i++) { + for(size_t i=0 ; i<pool->ndata ; i++) { chunk = (ucx_memchunk*) pool->data[i]; - if(chunk->destructor != NULL) { - chunk->destructor(&chunk->c); + if(chunk) { + if(chunk->destructor) { + chunk->destructor(&(chunk->c)); + } + free(chunk); } - free(chunk); } free(pool->data); free(pool); @@ -118,3 +175,17 @@ rd->ptr = ptr; ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); } + +UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) { + UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc( + pool, sizeof(UcxAllocator)); + if(!allocator) { + 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; + return allocator; +}