ucx/mempool.c

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

mercurial