The function allocates space for at least one more sequence element. If there are free sequence blocks (seq->free_blocks != 0) they are reused, otherwise the space is allocated in the storage: */
| 623 | If there are free sequence blocks (seq->free_blocks != 0) |
| 624 | they are reused, otherwise the space is allocated in the storage: */ |
| 625 | static void |
| 626 | icvGrowSeq( CvSeq *seq, int in_front_of ) |
| 627 | { |
| 628 | CvSeqBlock *block; |
| 629 | |
| 630 | if( !seq ) |
| 631 | CV_Error( CV_StsNullPtr, "" ); |
| 632 | block = seq->free_blocks; |
| 633 | |
| 634 | if( !block ) |
| 635 | { |
| 636 | int elem_size = seq->elem_size; |
| 637 | int delta_elems = seq->delta_elems; |
| 638 | CvMemStorage *storage = seq->storage; |
| 639 | |
| 640 | if( seq->total >= delta_elems*4 ) |
| 641 | cvSetSeqBlockSize( seq, delta_elems*2 ); |
| 642 | |
| 643 | if( !storage ) |
| 644 | CV_Error( CV_StsNullPtr, "The sequence has NULL storage pointer" ); |
| 645 | |
| 646 | /* If there is a free space just after last allocated block |
| 647 | and it is big enough then enlarge the last block. |
| 648 | This can happen only if the new block is added to the end of sequence: */ |
| 649 | if( (size_t)(ICV_FREE_PTR(storage) - seq->block_max) < CV_STRUCT_ALIGN && |
| 650 | storage->free_space >= seq->elem_size && !in_front_of ) |
| 651 | { |
| 652 | int delta = storage->free_space / elem_size; |
| 653 | |
| 654 | delta = MIN( delta, delta_elems ) * elem_size; |
| 655 | seq->block_max += delta; |
| 656 | storage->free_space = cvAlignLeft((int)(((schar*)storage->top + storage->block_size) - |
| 657 | seq->block_max), CV_STRUCT_ALIGN ); |
| 658 | return; |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | int delta = elem_size * delta_elems + ICV_ALIGNED_SEQ_BLOCK_SIZE; |
| 663 | |
| 664 | /* Try to allocate <delta_elements> elements: */ |
| 665 | if( storage->free_space < delta ) |
| 666 | { |
| 667 | int small_block_size = MAX(1, delta_elems/3)*elem_size + |
| 668 | ICV_ALIGNED_SEQ_BLOCK_SIZE; |
| 669 | /* try to allocate smaller part */ |
| 670 | if( storage->free_space >= small_block_size + CV_STRUCT_ALIGN ) |
| 671 | { |
| 672 | delta = (storage->free_space - ICV_ALIGNED_SEQ_BLOCK_SIZE)/seq->elem_size; |
| 673 | delta = delta*seq->elem_size + ICV_ALIGNED_SEQ_BLOCK_SIZE; |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | icvGoNextMemBlock( storage ); |
| 678 | assert( storage->free_space >= delta ); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | block = (CvSeqBlock*)cvMemStorageAlloc( storage, delta ); |
no test coverage detected