void try_merge_buddy_recursive( ChunkMap::iterator cmit ) { // compute address of buddy intptr_t address = cmit->second.address; intptr_t buddy_address = (address ^ cmit->second.size); DVLOG(5) << cmit->second << " buddy address " << (void *) buddy_address; // does it exist? ChunkMap::iterator buddy_iterator = chunks_.find( buddy_address ); if( buddy_iterator != chunks_.end() && buddy_iterator->second.size == cmit->second.size && buddy_iterator->second.in_use == false ) { DVLOG(5) << "buddy found! address " << (void *) address << " buddy address " << (void *) buddy_address; // remove the higher-addressed chunk ChunkMap::iterator higher_iterator = address < buddy_address ? buddy_iterator : cmit; remove_from_free_list( higher_iterator ); chunks_.erase( higher_iterator ); // keep the the lower-addressed chunk in the map: // update its size and move it to the right free list ChunkMap::iterator lower_iterator = address < buddy_address ? cmit : buddy_iterator; remove_from_free_list( lower_iterator ); // should these be swapped? I think so. lower_iterator->second.size *= 2; add_to_free_list( lower_iterator ); // see if we have more to merge try_merge_buddy_recursive( lower_iterator ); } }
void ChunkRangeManager::assertValid() const { if (_ranges.empty()) return; try { // No Nulls for (ChunkRangeMap::const_iterator it = _ranges.begin(), end = _ranges.end(); it != end; ++it) { verify(it->second); } // Check endpoints verify(allOfType(MinKey, _ranges.begin()->second->getMin())); verify(allOfType(MaxKey, boost::prior(_ranges.end())->second->getMax())); // Make sure there are no gaps or overlaps for (ChunkRangeMap::const_iterator it = boost::next(_ranges.begin()), end = _ranges.end(); it != end; ++it) { ChunkRangeMap::const_iterator last = boost::prior(it); verify(it->second->getMin() == last->second->getMax()); } // Check Map keys for (ChunkRangeMap::const_iterator it = _ranges.begin(), end = _ranges.end(); it != end; ++it) { verify(it->first == it->second->getMax()); } // Make sure we match the original chunks const ChunkMap chunks = _ranges.begin()->second->getManager()->_chunkMap; for (ChunkMap::const_iterator i = chunks.begin(); i != chunks.end(); ++i) { const ChunkPtr chunk = i->second; ChunkRangeMap::const_iterator min = _ranges.upper_bound(chunk->getMin()); ChunkRangeMap::const_iterator max = _ranges.lower_bound(chunk->getMax()); verify(min != _ranges.end()); verify(max != _ranges.end()); verify(min == max); verify(min->second->getShardId() == chunk->getShardId()); verify(min->second->containsKey(chunk->getMin())); verify(min->second->containsKey(chunk->getMax()) || (min->second->getMax() == chunk->getMax())); } } catch (...) { error() << "\t invalid ChunkRangeMap! printing ranges:"; for (ChunkRangeMap::const_iterator it = _ranges.begin(), end = _ranges.end(); it != end; ++it) { log() << it->first << ": " << it->second->toString(); } throw; } }
void ChunkRangeManager::reloadAll(const ChunkMap& chunks) { _ranges.clear(); _insertRange(chunks.begin(), chunks.end()); DEV assertValid(); }