Skip to content

Commit f1b8b27

Browse files
committed
Improvements on rbtree_best_fit::check_sanity.
Perform quick tests before iterating on the blocks. Early exit from loop if block size is 0 or accumulated free memory exceeds maximum. Signed-off-by: Miguel Company <miguelcompany@eprosima.com>
1 parent 30bceaf commit f1b8b27

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

include/boost/interprocess/mem_algo/rbtree_best_fit.hpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -651,29 +651,35 @@ bool rbtree_best_fit<MutexFamily, VoidPointer, MemAlignment>::
651651
//-----------------------
652652
boost::interprocess::scoped_lock<mutex_type> guard(m_header);
653653
//-----------------------
654-
imultiset_iterator ib(m_header.m_imultiset.begin()), ie(m_header.m_imultiset.end());
655-
656-
size_type free_memory = 0;
657-
658-
//Iterate through all blocks obtaining their size
659-
for(; ib != ie; ++ib){
660-
free_memory += (size_type)ib->m_size*Alignment;
661-
if(!algo_impl_t::check_alignment(&*ib))
662-
return false;
663-
}
664654

665655
//Check allocated bytes are less than size
666656
if(m_header.m_allocated > m_header.m_size){
667657
return false;
668658
}
669659

660+
//Calculate the maximum free memory available in the segment
670661
size_type block1_off =
671662
priv_first_block_offset_from_this(this, m_header.m_extra_hdr_bytes);
663+
size_type max_free_memory = m_header.m_size - block1_off;
672664

673-
//Check free bytes are less than size
674-
if(free_memory > (m_header.m_size - block1_off)){
675-
return false;
665+
//Iterate through all blocks obtaining their size
666+
imultiset_iterator ib(m_header.m_imultiset.begin()), ie(m_header.m_imultiset.end());
667+
size_type free_memory = 0;
668+
for(; ib != ie; ++ib){
669+
if(!algo_impl_t::check_alignment(&*ib)){
670+
return false;
671+
}
672+
//A size of 0 is not allowed in the multiset
673+
if(!ib->m_size){
674+
return false;
675+
}
676+
free_memory += (size_type)ib->m_size*Alignment;
677+
//Check free bytes are less than size
678+
if(free_memory > max_free_memory){
679+
return false;
680+
}
676681
}
682+
677683
return true;
678684
}
679685

0 commit comments

Comments
 (0)