diff --git a/src/Dict.cc b/src/Dict.cc index 368de9a2ed..0bd1bb6773 100644 --- a/src/Dict.cc +++ b/src/Dict.cc @@ -1527,7 +1527,7 @@ void Dictionary::MakeRobustCookie(IterCookie* cookie) IterCookie* Dictionary::InitForIterationNonConst() //const { - num_iterators++; + IncrIters(); return new IterCookie(const_cast(this)); } @@ -1535,7 +1535,7 @@ void Dictionary::StopIterationNonConst(IterCookie* cookie) //const { ASSERT(num_iterators > 0); if ( num_iterators > 0 ) - num_iterators--; + DecrIters(); delete cookie; } @@ -1549,7 +1549,7 @@ void* Dictionary::NextEntryNonConst(detail::HashKey*& h, IterCookie*& c, bool re if ( ! table ) { if ( num_iterators > 0 ) - num_iterators--; + DecrIters(); delete c; c = nullptr; return nullptr; //end of iteration. @@ -1589,7 +1589,7 @@ void* Dictionary::NextEntryNonConst(detail::HashKey*& h, IterCookie*& c, bool re if ( c->next >= capacity ) {//end. if ( num_iterators > 0 ) - num_iterators--; + DecrIters(); delete c; c = nullptr; return nullptr; //end of iteration. @@ -1641,7 +1641,7 @@ DictIterator::DictIterator(const Dictionary* d, detail::DictEntry* begin, detail // violate the constness guarantees of const-begin()/end() and cbegin()/cend(), but we're not modifying the // actual data in the collection, just a counter in the wrapper of the collection. dict = const_cast(d); - dict->num_iterators++; + dict->IncrIters(); } DictIterator::~DictIterator() @@ -1649,7 +1649,7 @@ DictIterator::~DictIterator() if ( dict ) { assert(dict->num_iterators > 0); - dict->num_iterators--; + dict->DecrIters(); } } @@ -1673,13 +1673,13 @@ DictIterator::DictIterator(const DictIterator& that) if ( dict ) { assert(dict->num_iterators > 0); - dict->num_iterators--; + dict->DecrIters(); } dict = that.dict; curr = that.curr; end = that.end; - dict->num_iterators++; + dict->IncrIters(); } DictIterator& DictIterator::operator=(const DictIterator& that) @@ -1690,13 +1690,13 @@ DictIterator& DictIterator::operator=(const DictIterator& that) if ( dict ) { assert(dict->num_iterators > 0); - dict->num_iterators--; + dict->DecrIters(); } dict = that.dict; curr = that.curr; end = that.end; - dict->num_iterators++; + dict->IncrIters(); return *this; } @@ -1709,7 +1709,7 @@ DictIterator::DictIterator(DictIterator&& that) if ( dict ) { assert(dict->num_iterators > 0); - dict->num_iterators--; + dict->DecrIters(); } dict = that.dict; @@ -1727,7 +1727,7 @@ DictIterator& DictIterator::operator=(DictIterator&& that) if ( dict ) { assert(dict->num_iterators > 0); - dict->num_iterators--; + dict->DecrIters(); } dict = that.dict; @@ -1809,7 +1809,7 @@ RobustDictIterator::RobustDictIterator(Dictionary* d) : curr(nullptr), dict(d) inserted = new std::vector(); visited = new std::vector(); - dict->num_iterators++; + dict->IncrIters(); dict->iterators->push_back(this); // Advance the iterator one step so that we're at the first element. @@ -1833,7 +1833,7 @@ RobustDictIterator::RobustDictIterator(const RobustDictIterator& other) : curr(n std::copy(other.visited->begin(), other.visited->end(), std::back_inserter(*visited)); dict = other.dict; - dict->num_iterators++; + dict->IncrIters(); dict->iterators->push_back(this); curr = other.curr; @@ -1870,7 +1870,7 @@ void RobustDictIterator::Complete() if ( dict ) { assert(dict->num_iterators > 0); - dict->num_iterators--; + dict->DecrIters(); dict->iterators->erase(std::remove(dict->iterators->begin(), dict->iterators->end(), this), dict->iterators->end()); diff --git a/src/Dict.h b/src/Dict.h index beefafaab8..736679ac08 100644 --- a/src/Dict.h +++ b/src/Dict.h @@ -477,6 +477,9 @@ private: RobustDictIterator MakeRobustIterator(); detail::DictEntry GetNextRobustIteration(RobustDictIterator* iter); + void IncrIters() { ++num_iterators; } + void DecrIters() { --num_iterators; } + //alligned on 8-bytes with 4-leading bytes. 7*8=56 bytes a dictionary. // when sizeup but the current mapping is in progress. the current mapping will be ignored