ucx/mempool.c

Fri, 05 Oct 2012 11:52:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Oct 2012 11:52:53 +0200
changeset 48
621a4430c404
parent 28
1666cbeb1db8
child 50
ff194559eb41
permissions
-rw-r--r--

map can now load values from file into pooled memory

use with care when using a decoder that also allocates memory

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

mercurial