110 if(pool->data[i] == mem) { |
110 if(pool->data[i] == mem) { |
111 pool->data[i] = newm; |
111 pool->data[i] = newm; |
112 return newm + sizeof(ucx_destructor); |
112 return newm + sizeof(ucx_destructor); |
113 } |
113 } |
114 } |
114 } |
115 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
115 fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
116 (intptr_t)ptr, (intptr_t)pool); |
116 (intptr_t)ptr, (intptr_t)pool); |
117 exit(1); |
117 exit(1); |
118 } else { |
118 } else { |
119 return newm + sizeof(ucx_destructor); |
119 return newm + sizeof(ucx_destructor); |
120 } |
120 } |
121 } |
121 } |
122 |
122 |
123 void ucx_mempool_free(UcxMempool *pool) { |
123 void ucx_mempool_free(UcxMempool *pool, void *ptr) { |
|
124 ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor)); |
|
125 for(size_t i=0 ; i<pool->ndata ; i++) { |
|
126 if(chunk == pool->data[i]) { |
|
127 if(chunk->destructor != NULL) { |
|
128 chunk->destructor(&chunk->c); |
|
129 } |
|
130 free(chunk); |
|
131 size_t last_index = pool->ndata - 1; |
|
132 if(i != last_index) { |
|
133 pool->data[i] = pool->data[last_index]; |
|
134 } |
|
135 pool->ndata--; |
|
136 return; |
|
137 } |
|
138 } |
|
139 fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", |
|
140 (intptr_t)ptr, (intptr_t)pool); |
|
141 exit(1); |
|
142 } |
|
143 |
|
144 void ucx_mempool_destroy(UcxMempool *pool) { |
124 ucx_memchunk *chunk; |
145 ucx_memchunk *chunk; |
125 for(size_t i=0 ; i<pool->ndata ; i++) { |
146 for(size_t i=0 ; i<pool->ndata ; i++) { |
126 chunk = (ucx_memchunk*) pool->data[i]; |
147 chunk = (ucx_memchunk*) pool->data[i]; |
127 if(chunk->destructor != NULL) { |
148 if(chunk) { |
128 chunk->destructor(&chunk->c); |
149 if(chunk->destructor != NULL) { |
|
150 chunk->destructor(&chunk->c); |
|
151 } |
|
152 free(chunk); |
129 } |
153 } |
130 free(chunk); |
|
131 } |
154 } |
132 free(pool->data); |
155 free(pool->data); |
133 free(pool); |
156 free(pool); |
134 } |
157 } |
135 |
158 |
143 sizeof(ucx_regdestr)); |
166 sizeof(ucx_regdestr)); |
144 rd->destructor = destr; |
167 rd->destructor = destr; |
145 rd->ptr = ptr; |
168 rd->ptr = ptr; |
146 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); |
169 ucx_mempool_set_destr(rd, ucx_mempool_shared_destr); |
147 } |
170 } |
|
171 |
|
172 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) { |
|
173 UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc( |
|
174 pool, sizeof(UcxAllocator)); |
|
175 if(!allocator) { |
|
176 return NULL; |
|
177 } |
|
178 allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc; |
|
179 allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc; |
|
180 allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc; |
|
181 allocator->free = (ucx_allocator_free)ucx_mempool_free; |
|
182 allocator->pool = pool; |
|
183 return allocator; |
|
184 } |