ucx/mempool.c

changeset 113
8693d7874773
parent 103
08018864fb91
child 116
234920008754
     1.1 --- a/ucx/mempool.c	Mon Jul 15 15:43:18 2013 +0200
     1.2 +++ b/ucx/mempool.c	Mon Jul 15 16:59:52 2013 +0200
     1.3 @@ -112,7 +112,7 @@
     1.4                  return newm + sizeof(ucx_destructor);
     1.5              }
     1.6          }
     1.7 -        fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
     1.8 +        fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
     1.9            (intptr_t)ptr, (intptr_t)pool);
    1.10          exit(1);
    1.11      } else {
    1.12 @@ -120,14 +120,37 @@
    1.13      }
    1.14  }
    1.15  
    1.16 -void ucx_mempool_free(UcxMempool *pool) {
    1.17 +void ucx_mempool_free(UcxMempool *pool, void *ptr) {
    1.18 +    ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor));
    1.19 +    for(size_t i=0 ; i<pool->ndata ; i++) {
    1.20 +        if(chunk == pool->data[i]) {
    1.21 +            if(chunk->destructor != NULL) {
    1.22 +                chunk->destructor(&chunk->c);
    1.23 +            }
    1.24 +            free(chunk);
    1.25 +            size_t last_index = pool->ndata - 1;
    1.26 +            if(i != last_index) {
    1.27 +                pool->data[i] = pool->data[last_index];
    1.28 +            }
    1.29 +            pool->ndata--;
    1.30 +            return;
    1.31 +        }
    1.32 +    }
    1.33 +    fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
    1.34 +            (intptr_t)ptr, (intptr_t)pool);
    1.35 +    exit(1);
    1.36 +}
    1.37 +
    1.38 +void ucx_mempool_destroy(UcxMempool *pool) {
    1.39      ucx_memchunk *chunk;
    1.40      for(size_t i=0 ; i<pool->ndata ; i++) {
    1.41          chunk = (ucx_memchunk*) pool->data[i];
    1.42 -        if(chunk->destructor != NULL) {
    1.43 -            chunk->destructor(&chunk->c);
    1.44 +        if(chunk) {
    1.45 +            if(chunk->destructor != NULL) {
    1.46 +                chunk->destructor(&chunk->c);
    1.47 +            }
    1.48 +            free(chunk);
    1.49          }
    1.50 -        free(chunk);
    1.51      }
    1.52      free(pool->data);
    1.53      free(pool);
    1.54 @@ -145,3 +168,17 @@
    1.55      rd->ptr = ptr;
    1.56      ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
    1.57  }
    1.58 +
    1.59 +UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) {
    1.60 +    UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc(
    1.61 +            pool, sizeof(UcxAllocator));
    1.62 +    if(!allocator) {
    1.63 +        return NULL;
    1.64 +    }
    1.65 +    allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
    1.66 +    allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
    1.67 +    allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
    1.68 +    allocator->free = (ucx_allocator_free)ucx_mempool_free;
    1.69 +    allocator->pool = pool;
    1.70 +    return allocator;
    1.71 +}

mercurial