253 // safety check |
253 // safety check |
254 if (source == dest) return 1; |
254 if (source == dest) return 1; |
255 |
255 |
256 // ensure enough capacity in the destination pool |
256 // ensure enough capacity in the destination pool |
257 if (cx_mempool_ensure_capacity(dest, dest->size + source->size + 1)) { |
257 if (cx_mempool_ensure_capacity(dest, dest->size + source->size + 1)) { |
258 return 1; |
258 return 1; // LCOV_EXCL_LINE |
259 } |
259 } |
260 |
260 |
261 // allocate a replacement allocator for the source pool |
261 // allocate a replacement allocator for the source pool |
262 CxAllocator *new_source_allocator = malloc(sizeof(CxAllocator)); |
262 CxAllocator *new_source_allocator = malloc(sizeof(CxAllocator)); |
263 if (new_source_allocator == NULL) { // LCOV_EXCL_START |
263 if (new_source_allocator == NULL) { // LCOV_EXCL_START |
281 memset(source->data, 0, source->size * sizeof(source->data[0])); |
281 memset(source->data, 0, source->size * sizeof(source->data[0])); |
282 source->size = 0; |
282 source->size = 0; |
283 |
283 |
284 return 0; |
284 return 0; |
285 } |
285 } |
|
286 |
|
287 int cxMempoolTransferObject( |
|
288 CxMempool *source, |
|
289 CxMempool *dest, |
|
290 const void *obj |
|
291 ) { |
|
292 // safety check |
|
293 if (source == dest) return 1; |
|
294 |
|
295 // first, make sure that the dest pool can take the object |
|
296 if (cx_mempool_ensure_capacity(dest, dest->size + 1)) { |
|
297 return 1; // LCOV_EXCL_LINE |
|
298 } |
|
299 // search for the object |
|
300 for (size_t i = 0; i < source->size; i++) { |
|
301 struct cx_mempool_memory_s *mem = source->data[i]; |
|
302 if (mem->c == obj) { |
|
303 // remove from the source pool |
|
304 size_t last_index = source->size - 1; |
|
305 if (i != last_index) { |
|
306 source->data[i] = source->data[last_index]; |
|
307 source->data[last_index] = NULL; |
|
308 } |
|
309 source->size--; |
|
310 // add to the target pool |
|
311 dest->data[dest->size++] = mem; |
|
312 return 0; |
|
313 } |
|
314 } |
|
315 // not found |
|
316 return 1; |
|
317 } |