diff -r 6384016df2a3 -r 8693d7874773 ucx/mempool.c --- a/ucx/mempool.c Mon Jul 15 15:43:18 2013 +0200 +++ b/ucx/mempool.c Mon Jul 15 16:59:52 2013 +0200 @@ -112,7 +112,7 @@ 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 { @@ -120,14 +120,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 ; indata ; 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(1); +} + +void ucx_mempool_destroy(UcxMempool *pool) { ucx_memchunk *chunk; for(size_t i=0 ; indata ; i++) { chunk = (ucx_memchunk*) pool->data[i]; - if(chunk->destructor != NULL) { - chunk->destructor(&chunk->c); + if(chunk) { + if(chunk->destructor != NULL) { + chunk->destructor(&chunk->c); + } + free(chunk); } - free(chunk); } free(pool->data); free(pool); @@ -145,3 +168,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; +}