ucx/mempool.c

Wed, 27 Feb 2013 13:30:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 13:30:21 +0100
changeset 95
ecfdc1c4a552
parent 75
990734f548ef
child 103
08018864fb91
permissions
-rw-r--r--

added gnu++11 support

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

mercurial