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

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

mercurial