562 pool->capacity = capacity; |
562 pool->capacity = capacity; |
563 |
563 |
564 return pool; |
564 return pool; |
565 } |
565 } |
566 |
566 |
|
567 void cxMempoolGlobalDestructor(CxMempool *pool, cx_destructor_func fnc) { |
|
568 pool->destr = fnc; |
|
569 } |
|
570 |
|
571 void cxMempoolGlobalDestructor2(CxMempool *pool, cx_destructor_func2 fnc, void *data) { |
|
572 pool->destr2 = fnc; |
|
573 pool->destr2_data = data; |
|
574 } |
|
575 |
567 static void cx_mempool_free_transferred_allocator(void *al) { |
576 static void cx_mempool_free_transferred_allocator(void *al) { |
568 cxFreeDefault(al); |
577 cxFreeDefault(al); |
569 } |
578 } |
570 |
579 |
571 int cxMempoolTransfer( |
580 int cxMempoolTransfer( |
626 const void *obj |
635 const void *obj |
627 ) { |
636 ) { |
628 // safety check |
637 // safety check |
629 if (source == dest) return 1; |
638 if (source == dest) return 1; |
630 |
639 |
631 // first, make sure that the dest pool can take the object |
|
632 if (cx_mempool_ensure_capacity(dest, dest->size + 1)) { |
|
633 return 1; // LCOV_EXCL_LINE |
|
634 } |
|
635 // search for the object |
640 // search for the object |
636 for (size_t i = 0; i < source->size; i++) { |
641 for (size_t i = 0; i < source->size; i++) { |
637 struct cx_mempool_memory_s *mem = source->data[i]; |
642 struct cx_mempool_memory_s *mem = source->data[i]; |
638 if (mem->c == obj) { |
643 if (mem->c == obj) { |
|
644 // first, make sure that the dest pool can take the object |
|
645 if (cx_mempool_ensure_capacity(dest, dest->size + 1)) { |
|
646 return 1; // LCOV_EXCL_LINE |
|
647 } |
639 // remove from the source pool |
648 // remove from the source pool |
640 size_t last_index = source->size - 1; |
649 size_t last_index = source->size - 1; |
641 if (i != last_index) { |
650 if (i != last_index) { |
642 source->data[i] = source->data[last_index]; |
651 source->data[i] = source->data[last_index]; |
643 source->data[last_index] = NULL; |
652 source->data[last_index] = NULL; |
646 // add to the target pool |
655 // add to the target pool |
647 dest->data[dest->size++] = mem; |
656 dest->data[dest->size++] = mem; |
648 return 0; |
657 return 0; |
649 } |
658 } |
650 } |
659 } |
|
660 // search in the registered objects |
|
661 for (size_t i = 0; i < source->registered_size; i++) { |
|
662 struct cx_mempool_foreign_memory_s *mem = &source->registered[i]; |
|
663 if (mem->mem == obj) { |
|
664 // first, make sure that the dest pool can take the object |
|
665 if (cx_mempool_ensure_registered_capacity(dest, |
|
666 dest->registered_size + 1)) { |
|
667 return 1; // LCOV_EXCL_LINE |
|
668 } |
|
669 dest->registered[dest->registered_size++] = *mem; |
|
670 // remove from the source pool |
|
671 size_t last_index = source->registered_size - 1; |
|
672 if (i != last_index) { |
|
673 source->registered[i] = source->registered[last_index]; |
|
674 memset(&source->registered[last_index], 0, |
|
675 sizeof(struct cx_mempool_foreign_memory_s)); |
|
676 } |
|
677 source->registered_size--; |
|
678 return 0; |
|
679 } |
|
680 } |
651 // not found |
681 // not found |
652 return 1; |
682 return 1; |
653 } |
683 } |