ucx/mpool.c

Sat, 31 Dec 2011 21:05:59 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 21:05:59 +0100
changeset 13
98ac89e3aa37
child 14
b78e174b6814
permissions
-rw-r--r--

Added mempool

     1 /*
     2  *
     3  */
     5 #include <stdlib.h>
     6 #include <string.h>
     7 #include <stdio.h>
     9 #include "mpool.h"
    11 typedef struct {
    12     ucx_destructor destructor;
    13     char c;
    14 } ucx_memchunk;
    16 typedef struct {
    17     ucx_destructor destructor;
    18     void           *ptr;
    19 } ucx_regdestr;
    21 void ucx_mempool_shared_destr(void* ptr) {
    22     ucx_regdestr *rd = (ucx_regdestr*)ptr;
    23     rd->destructor(rd->ptr);
    24 }
    26 UcxMempool *ucx_mempool_new(size_t n) {
    27     UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool*));
    28     pool->data = malloc(n * sizeof(void*));
    29     pool->ndata = 0;
    30     pool->size = n;
    31     return pool;
    32 }
    34 void ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {    
    35     void **data = realloc(pool->data, newcap*sizeof(void*));
    36     pool->data = data; 
    37     pool->size = newcap;
    38 }
    40 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
    41     ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
    42     if(mem == NULL) {
    43         return NULL;
    44     }
    46     if (pool->ndata >= pool->size) {
    47         ucx_mempool_chcap(pool, pool->size + 16);
    48     }
    50     mem->destructor = NULL;
    51     pool->data[pool->ndata] = mem;
    52     pool->ndata++;
    54     return &mem->c;
    55 }
    57 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
    58     void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
    59     if(ptr == NULL) {
    60         return NULL;
    61     }
    62     memset(ptr, 0, nelem * elsize);
    63     return ptr;
    64 }
    66 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
    67     void *mem = ((char*)ptr) - sizeof(ucx_destructor);
    68     for(int i=0;i<pool->ndata;i++) {
    69         if(pool->data[i] == mem) {
    70             mem == realloc(mem, n);
    71             pool->data[i] = mem;
    72             return mem;
    73         }
    74     }
    75 }
    77 void ucx_mempool_free(UcxMempool *pool) {
    78     ucx_memchunk *chunk;
    79     for(int i=0;i<pool->ndata;i++) {
    80         chunk = (ucx_memchunk*) pool->data[i];
    81         if(chunk->destructor != NULL) {
    82             chunk->destructor(&chunk->c);
    83         }
    84         free(chunk);
    85     }
    86     free(pool->data);
    87     free(pool);
    88 }
    90 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
    91     *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
    92 }
    94 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
    95     ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
    96             pool,
    97             sizeof(ucx_regdestr));
    98     rd->destructor = destr;
    99     rd->ptr;
   100     ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
   101 }

mercurial