ucx/mempool.c

changeset 314
8722a668fb2a
parent 255
bf19378aed58
child 335
c1bc13faadaa
equal deleted inserted replaced
313:d721250984d0 314:8722a668fb2a
91 91
92 return pool; 92 return pool;
93 } 93 }
94 94
95 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 95 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
96 if (newcap < pool->ndata) {
97 return 1;
98 }
99
96 void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); 100 void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
97 if (data) { 101 if (data) {
98 pool->data = data; 102 pool->data = data;
99 pool->size = newcap; 103 pool->size = newcap;
100 return EXIT_SUCCESS; 104 return 0;
101 } else { 105 } else {
102 return EXIT_FAILURE; 106 return 1;
103 } 107 }
104 } 108 }
105 109
106 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 110 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
107 if (pool->ndata >= pool->size) { 111 if (pool->ndata >= pool->size) {
108 // The hard coded 16 is documented for this function and ucx_mempool_new 112 size_t newcap = pool->size*2;
109 if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) { 113 if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
110 return NULL; 114 return NULL;
111 } 115 }
112 } 116 }
113 117
114 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 118 void *p = malloc(sizeof(ucx_destructor) + n);
119 ucx_memchunk *mem = (ucx_memchunk*)p;
115 if (!mem) { 120 if (!mem) {
116 return NULL; 121 return NULL;
117 } 122 }
118 123
119 mem->destructor = NULL; 124 mem->destructor = NULL;
145 return newm + sizeof(ucx_destructor); 150 return newm + sizeof(ucx_destructor);
146 } 151 }
147 } 152 }
148 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 153 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
149 (intptr_t)ptr, (intptr_t)pool); 154 (intptr_t)ptr, (intptr_t)pool);
150 exit(EXIT_FAILURE); 155 abort();
151 } else { 156 } else {
152 return newm + sizeof(ucx_destructor); 157 return newm + sizeof(ucx_destructor);
153 } 158 }
154 } 159 }
155 160
170 return; 175 return;
171 } 176 }
172 } 177 }
173 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 178 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
174 (intptr_t)ptr, (intptr_t)pool); 179 (intptr_t)ptr, (intptr_t)pool);
175 exit(EXIT_FAILURE); 180 abort();
176 } 181 }
177 182
178 void ucx_mempool_destroy(UcxMempool *pool) { 183 void ucx_mempool_destroy(UcxMempool *pool) {
179 ucx_memchunk *chunk; 184 ucx_memchunk *chunk;
180 for(size_t i=0 ; i<pool->ndata ; i++) { 185 for(size_t i=0 ; i<pool->ndata ; i++) {

mercurial