ucx/mempool.c

Fri, 12 Oct 2012 10:54:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 10:54:55 +0200
changeset 69
fb59270b1de3
parent 57
e18157c52985
child 75
990734f548ef
permissions
-rw-r--r--

made the code work with VC++ compiler (use make CONF=windows)

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

mercurial