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 if (pool->size*2 < pool->size /* overflow check */ |
109 if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) { |
113 || ucx_mempool_chcap(pool, pool->size*2)) { |
110 return NULL; |
114 return NULL; |
111 } |
115 } |
112 } |
116 } |
113 |
117 |
114 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); |
118 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); |
145 return newm + sizeof(ucx_destructor); |
149 return newm + sizeof(ucx_destructor); |
146 } |
150 } |
147 } |
151 } |
148 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
152 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
149 (intptr_t)ptr, (intptr_t)pool); |
153 (intptr_t)ptr, (intptr_t)pool); |
150 exit(EXIT_FAILURE); |
154 abort(); |
151 } else { |
155 } else { |
152 return newm + sizeof(ucx_destructor); |
156 return newm + sizeof(ucx_destructor); |
153 } |
157 } |
154 } |
158 } |
155 |
159 |