ucx/mempool.c

Mon, 20 Feb 2012 15:30:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Feb 2012 15:30:45 +0100
changeset 28
1666cbeb1db8
parent 17
2e7050c3a18e
child 48
621a4430c404
permissions
-rw-r--r--

new mempool tests

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>
universe@15 8 #include <errno.h>
olaf@13 9
olaf@17 10 #include "mempool.h"
olaf@13 11
olaf@13 12 typedef struct {
olaf@13 13 ucx_destructor destructor;
olaf@13 14 char c;
olaf@13 15 } ucx_memchunk;
olaf@13 16
olaf@13 17 typedef struct {
olaf@13 18 ucx_destructor destructor;
olaf@13 19 void *ptr;
olaf@13 20 } ucx_regdestr;
olaf@13 21
olaf@13 22 void ucx_mempool_shared_destr(void* ptr) {
olaf@13 23 ucx_regdestr *rd = (ucx_regdestr*)ptr;
olaf@13 24 rd->destructor(rd->ptr);
olaf@13 25 }
olaf@13 26
olaf@13 27 UcxMempool *ucx_mempool_new(size_t n) {
olaf@14 28 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
universe@15 29 if (pool == NULL) return NULL;
universe@15 30
olaf@13 31 pool->data = malloc(n * sizeof(void*));
universe@15 32 if (pool->data == NULL) {
universe@15 33 free(pool);
universe@15 34 return NULL;
universe@15 35 }
universe@15 36
olaf@13 37 pool->ndata = 0;
olaf@13 38 pool->size = n;
olaf@13 39 return pool;
olaf@13 40 }
olaf@13 41
universe@15 42 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
olaf@13 43 void **data = realloc(pool->data, newcap*sizeof(void*));
universe@15 44 if (data == NULL) {
universe@15 45 return ENOMEM;
universe@15 46 } else {
universe@15 47 pool->data = data;
universe@15 48 pool->size = newcap;
universe@15 49 return EXIT_SUCCESS;
universe@15 50 }
olaf@13 51 }
olaf@13 52
olaf@13 53 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
olaf@13 54 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
universe@15 55 if (mem == NULL) return NULL;
olaf@13 56
olaf@13 57 if (pool->ndata >= pool->size) {
olaf@13 58 ucx_mempool_chcap(pool, pool->size + 16);
universe@15 59 }
olaf@13 60
olaf@13 61 mem->destructor = NULL;
olaf@13 62 pool->data[pool->ndata] = mem;
olaf@13 63 pool->ndata++;
olaf@13 64
olaf@13 65 return &mem->c;
olaf@13 66 }
olaf@13 67
olaf@13 68 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
olaf@13 69 void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
olaf@13 70 if(ptr == NULL) {
olaf@13 71 return NULL;
olaf@13 72 }
olaf@13 73 memset(ptr, 0, nelem * elsize);
olaf@13 74 return ptr;
olaf@13 75 }
olaf@13 76
olaf@13 77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
universe@28 78 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
universe@15 79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
olaf@14 80 if (newm == NULL) return NULL;
universe@15 81 if (mem != newm) {
olaf@14 82 for(int i=0;i<pool->ndata;i++) {
olaf@14 83 if(pool->data[i] == mem) {
olaf@14 84 pool->data[i] = newm;
universe@28 85 return newm + sizeof(ucx_destructor);
olaf@14 86 }
olaf@13 87 }
universe@28 88 fprintf(stderr, "FATAL: %8x not in mpool %8x\n", ptr, pool);
universe@15 89 exit(1);
universe@16 90 } else {
universe@28 91 return newm + sizeof(ucx_destructor);
olaf@13 92 }
olaf@13 93 }
olaf@13 94
olaf@13 95 void ucx_mempool_free(UcxMempool *pool) {
olaf@13 96 ucx_memchunk *chunk;
olaf@13 97 for(int i=0;i<pool->ndata;i++) {
olaf@13 98 chunk = (ucx_memchunk*) pool->data[i];
olaf@13 99 if(chunk->destructor != NULL) {
olaf@13 100 chunk->destructor(&chunk->c);
olaf@13 101 }
olaf@13 102 free(chunk);
olaf@13 103 }
olaf@13 104 free(pool->data);
olaf@13 105 free(pool);
olaf@13 106 }
olaf@13 107
olaf@13 108 void ucx_mempool_set_destr(void *ptr, ucx_destructor func) {
olaf@13 109 *(ucx_destructor*)((char*)ptr-sizeof(ucx_destructor)) = func;
olaf@13 110 }
olaf@13 111
olaf@13 112 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr) {
olaf@13 113 ucx_regdestr *rd = (ucx_regdestr*)ucx_mempool_malloc(
olaf@13 114 pool,
olaf@13 115 sizeof(ucx_regdestr));
olaf@13 116 rd->destructor = destr;
olaf@14 117 rd->ptr = ptr;
olaf@13 118 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
olaf@13 119 }

mercurial