ucx/mempool.c

changeset 135
a0aa1c15f46b
parent 131
fc3af16818a3
child 138
7800811078b8
equal deleted inserted replaced
134:4d320dc3a7af 135:a0aa1c15f46b
51 rd->destructor(rd->ptr); 51 rd->destructor(rd->ptr);
52 } 52 }
53 53
54 UcxMempool *ucx_mempool_new(size_t n) { 54 UcxMempool *ucx_mempool_new(size_t n) {
55 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); 55 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool));
56 if (pool == NULL) return NULL; 56 if (!pool) {
57 return NULL;
58 }
57 59
58 pool->data = (void**) malloc(n * sizeof(void*)); 60 pool->data = (void**) malloc(n * sizeof(void*));
59 if (pool->data == NULL) { 61 if (pool->data == NULL) {
60 free(pool); 62 free(pool);
61 return NULL; 63 return NULL;
66 return pool; 68 return pool;
67 } 69 }
68 70
69 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 71 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
70 void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); 72 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
71 if (data == NULL) { 73 if (data) {
72 return 1;
73 } else {
74 pool->data = data; 74 pool->data = data;
75 pool->size = newcap; 75 pool->size = newcap;
76 return EXIT_SUCCESS; 76 return EXIT_SUCCESS;
77 } else {
78 return EXIT_FAILURE;
77 } 79 }
78 } 80 }
79 81
80 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 82 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
81 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 83 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
82 if (mem == NULL) return NULL; 84 if (!mem) {
85 return NULL;
86 }
83 87
84 if (pool->ndata >= pool->size) { 88 if (pool->ndata >= pool->size) {
89 // The hard coded 16 is documented for this function and ucx_mempool_new
85 ucx_mempool_chcap(pool, pool->size + 16); 90 ucx_mempool_chcap(pool, pool->size + 16);
86 } 91 }
87 92
88 mem->destructor = NULL; 93 mem->destructor = NULL;
89 pool->data[pool->ndata] = mem; 94 pool->data[pool->ndata] = mem;
90 pool->ndata++; 95 pool->ndata++;
91 96
92 return &mem->c; 97 return &(mem->c);
93 } 98 }
94 99
95 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { 100 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
96 void *ptr = ucx_mempool_malloc(pool, nelem*elsize); 101 void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
97 if(ptr == NULL) { 102 if (!ptr) {
98 return NULL; 103 return NULL;
99 } 104 }
100 memset(ptr, 0, nelem * elsize); 105 memset(ptr, 0, nelem * elsize);
101 return ptr; 106 return ptr;
102 } 107 }
103 108
104 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 109 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
105 char *mem = ((char*)ptr) - sizeof(ucx_destructor); 110 char *mem = ((char*)ptr) - sizeof(ucx_destructor);
106 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 111 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
107 if (newm == NULL) return NULL; 112 if (!newm) {
113 return NULL;
114 }
108 if (mem != newm) { 115 if (mem != newm) {
109 for(size_t i=0 ; i < pool->ndata ; i++) { 116 for(size_t i=0 ; i < pool->ndata ; i++) {
110 if(pool->data[i] == mem) { 117 if(pool->data[i] == mem) {
111 pool->data[i] = newm; 118 pool->data[i] = newm;
112 return newm + sizeof(ucx_destructor); 119 return newm + sizeof(ucx_destructor);
136 return; 143 return;
137 } 144 }
138 } 145 }
139 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 146 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
140 (intptr_t)ptr, (intptr_t)pool); 147 (intptr_t)ptr, (intptr_t)pool);
141 exit(1); 148 exit(EXIT_FAILURE);
142 } 149 }
143 150
144 void ucx_mempool_destroy(UcxMempool *pool) { 151 void ucx_mempool_destroy(UcxMempool *pool) {
145 ucx_memchunk *chunk; 152 ucx_memchunk *chunk;
146 for(size_t i=0 ; i<pool->ndata ; i++) { 153 for(size_t i=0 ; i<pool->ndata ; i++) {
147 chunk = (ucx_memchunk*) pool->data[i]; 154 chunk = (ucx_memchunk*) pool->data[i];
148 if(chunk) { 155 if(chunk) {
149 if(chunk->destructor != NULL) { 156 if(chunk->destructor) {
150 chunk->destructor(&chunk->c); 157 chunk->destructor(&(chunk->c));
151 } 158 }
152 free(chunk); 159 free(chunk);
153 } 160 }
154 } 161 }
155 free(pool->data); 162 free(pool->data);

mercurial