77 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
80 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { |
78 char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
81 char *mem = ((char*)ptr) - sizeof(ucx_destructor); |
79 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
82 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); |
80 if (newm == NULL) return NULL; |
83 if (newm == NULL) return NULL; |
81 if (mem != newm) { |
84 if (mem != newm) { |
82 for(int i=0;i<pool->ndata;i++) { |
85 for(size_t i=0 ; i < pool->ndata ; i++) { |
83 if(pool->data[i] == mem) { |
86 if(pool->data[i] == mem) { |
84 pool->data[i] = newm; |
87 pool->data[i] = newm; |
85 return newm + sizeof(ucx_destructor); |
88 return newm + sizeof(ucx_destructor); |
86 } |
89 } |
87 } |
90 } |
88 fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08"PRIxPTR"\n", |
91 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
89 (intptr_t)ptr, (intptr_t)pool); |
92 (intptr_t)ptr, (intptr_t)pool); |
90 exit(1); |
93 exit(1); |
91 } else { |
94 } else { |
92 return newm + sizeof(ucx_destructor); |
95 return newm + sizeof(ucx_destructor); |
93 } |
96 } |
94 } |
97 } |
95 |
98 |
96 void ucx_mempool_free(UcxMempool *pool) { |
99 void ucx_mempool_free(UcxMempool *pool) { |
97 ucx_memchunk *chunk; |
100 ucx_memchunk *chunk; |
98 for(int i=0;i<pool->ndata;i++) { |
101 for(size_t i=0 ; i<pool->ndata ; i++) { |
99 chunk = (ucx_memchunk*) pool->data[i]; |
102 chunk = (ucx_memchunk*) pool->data[i]; |
100 if(chunk->destructor != NULL) { |
103 if(chunk->destructor != NULL) { |
101 chunk->destructor(&chunk->c); |
104 chunk->destructor(&chunk->c); |
102 } |
105 } |
103 free(chunk); |
106 free(chunk); |