ucx/mpool.c

changeset 16
b4769e4eb4d1
parent 15
2dc4c688c262
child 17
2e7050c3a18e
     1.1 --- a/ucx/mpool.c	Sat Dec 31 22:41:16 2011 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,118 +0,0 @@
     1.4 -/*
     1.5 - *
     1.6 - */
     1.7 -
     1.8 -#include <stdlib.h>
     1.9 -#include <string.h>
    1.10 -#include <stdio.h>
    1.11 -#include <errno.h>
    1.12 -
    1.13 -#include "mpool.h"
    1.14 -
    1.15 -typedef struct {
    1.16 -    ucx_destructor destructor;
    1.17 -    char c;
    1.18 -} ucx_memchunk;
    1.19 -
    1.20 -typedef struct {
    1.21 -    ucx_destructor destructor;
    1.22 -    void           *ptr;
    1.23 -} ucx_regdestr;
    1.24 -
    1.25 -void ucx_mempool_shared_destr(void* ptr) {
    1.26 -    ucx_regdestr *rd = (ucx_regdestr*)ptr;
    1.27 -    rd->destructor(rd->ptr);
    1.28 -}
    1.29 -
    1.30 -UcxMempool *ucx_mempool_new(size_t n) {
    1.31 -    UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
    1.32 -    if (pool == NULL) return NULL;
    1.33 -    
    1.34 -    pool->data = malloc(n * sizeof(void*));
    1.35 -    if (pool->data == NULL) {
    1.36 -        free(pool);
    1.37 -        return NULL;
    1.38 -    }
    1.39 -    
    1.40 -    pool->ndata = 0;
    1.41 -    pool->size = n;
    1.42 -    return pool;
    1.43 -}
    1.44 -
    1.45 -int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
    1.46 -    void **data = realloc(pool->data, newcap*sizeof(void*));
    1.47 -    if (data == NULL) {
    1.48 -        return ENOMEM;
    1.49 -    } else {
    1.50 -        pool->data = data; 
    1.51 -        pool->size = newcap;
    1.52 -        return EXIT_SUCCESS;
    1.53 -    }
    1.54 -}
    1.55 -
    1.56 -void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
    1.57 -    ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
    1.58 -    if (mem == NULL) return NULL;
    1.59 -
    1.60 -    if (pool->ndata >= pool->size) {
    1.61 -        ucx_mempool_chcap(pool, pool->size + 16);
    1.62 -     }
    1.63 -
    1.64 -    mem->destructor = NULL;
    1.65 -    pool->data[pool->ndata] = mem;
    1.66 -    pool->ndata++;
    1.67 -
    1.68 -    return &mem->c;
    1.69 -}
    1.70 -
    1.71 -void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
    1.72 -    void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
    1.73 -    if(ptr == NULL) {
    1.74 -        return NULL;
    1.75 -    }
    1.76 -    memset(ptr, 0, nelem * elsize);
    1.77 -    return ptr;
    1.78 -}
    1.79 -
    1.80 -void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
    1.81 -    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
    1.82 -    char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
    1.83 -    if (newm == NULL) return NULL;
    1.84 -    if (mem != newm) {
    1.85 -        for(int i=0;i<pool->ndata;i++) {
    1.86 -            if(pool->data[i] == mem) {
    1.87 -                pool->data[i] = newm;
    1.88 -                break;
    1.89 -            }
    1.90 -        }
    1.91 -        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
    1.92 -        exit(1);
    1.93 -    }
    1.94 -    return ((char*) newm) + sizeof(ucx_destructor);
    1.95 -}
    1.96 -
    1.97 -void ucx_mempool_free(UcxMempool *pool) {
    1.98 -    ucx_memchunk *chunk;
    1.99 -    for(int i=0;i<pool->ndata;i++) {
   1.100 -        chunk = (ucx_memchunk*) pool->data[i];
   1.101 -        if(chunk->destructor != NULL) {
   1.102 -            chunk->destructor(&chunk->c);
   1.103 -        }
   1.104 -        free(chunk);
   1.105 -    }
   1.106 -    free(pool->data);
   1.107 -    free(pool);
   1.108 -}
   1.109 -
   1.110 -void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
   1.111 -    *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
   1.112 -}
   1.113 -
   1.114 -void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
   1.115 -    ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
   1.116 -            pool,
   1.117 -            sizeof(ucx_regdestr));
   1.118 -    rd->destructor = destr;
   1.119 -    rd->ptr = ptr;
   1.120 -    ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
   1.121 -}

mercurial