michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // contained_range_map-inl.h: Hierarchically-organized range map implementation. michael@0: // michael@0: // See contained_range_map.h for documentation. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #ifndef PROCESSOR_CONTAINED_RANGE_MAP_INL_H__ michael@0: #define PROCESSOR_CONTAINED_RANGE_MAP_INL_H__ michael@0: michael@0: #include "processor/contained_range_map.h" michael@0: michael@0: #include michael@0: michael@0: #include "common/logging.h" michael@0: michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: michael@0: template michael@0: ContainedRangeMap::~ContainedRangeMap() { michael@0: // Clear frees the children pointed to by the map, and frees the map itself. michael@0: Clear(); michael@0: } michael@0: michael@0: michael@0: template michael@0: bool ContainedRangeMap::StoreRange( michael@0: const AddressType &base, const AddressType &size, const EntryType &entry) { michael@0: AddressType high = base + size - 1; michael@0: michael@0: // Check for undersize or overflow. michael@0: if (size <= 0 || high < base) { michael@0: //TODO(nealsid) We are commenting this out in order to prevent michael@0: // excessive logging. We plan to move to better logging as this michael@0: // failure happens quite often and is expected(see comment in michael@0: // basic_source_line_resolver.cc:671). michael@0: // BPLOG(INFO) << "StoreRange failed, " << HexString(base) << "+" michael@0: // << HexString(size) << ", " << HexString(high); michael@0: return false; michael@0: } michael@0: michael@0: if (!map_) michael@0: map_ = new AddressToRangeMap(); michael@0: michael@0: MapIterator iterator_base = map_->lower_bound(base); michael@0: MapIterator iterator_high = map_->lower_bound(high); michael@0: MapIterator iterator_end = map_->end(); michael@0: michael@0: if (iterator_base == iterator_high && iterator_base != iterator_end && michael@0: base >= iterator_base->second->base_) { michael@0: // The new range is entirely within an existing child range. michael@0: michael@0: // If the new range's geometry is exactly equal to an existing child michael@0: // range's, it violates the containment rules, and an attempt to store michael@0: // it must fail. iterator_base->first contains the key, which was the michael@0: // containing child's high address. michael@0: if (iterator_base->second->base_ == base && iterator_base->first == high) { michael@0: // TODO(nealsid): See the TODO above on why this is commented out. michael@0: // BPLOG(INFO) << "StoreRange failed, identical range is already " michael@0: // "present: " << HexString(base) << "+" << HexString(size); michael@0: return false; michael@0: } michael@0: michael@0: // Pass the new range on to the child to attempt to store. michael@0: return iterator_base->second->StoreRange(base, size, entry); michael@0: } michael@0: michael@0: // iterator_high might refer to an irrelevant range: one whose base address michael@0: // is higher than the new range's high address. Set contains_high to true michael@0: // only if iterator_high refers to a range that is at least partially michael@0: // within the new range. michael@0: bool contains_high = iterator_high != iterator_end && michael@0: high >= iterator_high->second->base_; michael@0: michael@0: // If the new range encompasses any existing child ranges, it must do so michael@0: // fully. Partial containment isn't allowed. michael@0: if ((iterator_base != iterator_end && base > iterator_base->second->base_) || michael@0: (contains_high && high < iterator_high->first)) { michael@0: // TODO(mmentovai): Some symbol files will trip this check frequently michael@0: // on STACK lines. Too many messages will be produced. These are more michael@0: // suitable for a DEBUG channel than an INFO channel. michael@0: // BPLOG(INFO) << "StoreRange failed, new range partially contains " michael@0: // "existing range: " << HexString(base) << "+" << michael@0: // HexString(size); michael@0: return false; michael@0: } michael@0: michael@0: // When copying and erasing contained ranges, the "end" iterator needs to michael@0: // point one past the last item of the range to copy. If contains_high is michael@0: // false, the iterator's already in the right place; the increment is safe michael@0: // because contains_high can't be true if iterator_high == iterator_end. michael@0: if (contains_high) michael@0: ++iterator_high; michael@0: michael@0: // Optimization: if the iterators are equal, no child ranges would be michael@0: // moved. Create the new child range with a NULL map to conserve space michael@0: // in leaf nodes, of which there will be many. michael@0: AddressToRangeMap *child_map = NULL; michael@0: michael@0: if (iterator_base != iterator_high) { michael@0: // The children of this range that are contained by the new range must michael@0: // be transferred over to the new range. Create the new child range map michael@0: // and copy the pointers to range maps it should contain into it. michael@0: child_map = new AddressToRangeMap(iterator_base, iterator_high); michael@0: michael@0: // Remove the copied child pointers from this range's map of children. michael@0: map_->erase(iterator_base, iterator_high); michael@0: } michael@0: michael@0: // Store the new range in the map by its high address. Any children that michael@0: // the new child range contains were formerly children of this range but michael@0: // are now this range's grandchildren. Ownership of these is transferred michael@0: // to the new child range. michael@0: map_->insert(MapValue(high, michael@0: new ContainedRangeMap(base, entry, child_map))); michael@0: return true; michael@0: } michael@0: michael@0: michael@0: template michael@0: bool ContainedRangeMap::RetrieveRange( michael@0: const AddressType &address, EntryType *entry) const { michael@0: BPLOG_IF(ERROR, !entry) << "ContainedRangeMap::RetrieveRange requires " michael@0: "|entry|"; michael@0: assert(entry); michael@0: michael@0: // If nothing was ever stored, then there's nothing to retrieve. michael@0: if (!map_) michael@0: return false; michael@0: michael@0: // Get an iterator to the child range whose high address is equal to or michael@0: // greater than the supplied address. If the supplied address is higher michael@0: // than all of the high addresses in the range, then this range does not michael@0: // contain a child at address, so return false. If the supplied address michael@0: // is lower than the base address of the child range, then it is not within michael@0: // the child range, so return false. michael@0: MapConstIterator iterator = map_->lower_bound(address); michael@0: if (iterator == map_->end() || address < iterator->second->base_) michael@0: return false; michael@0: michael@0: // The child in iterator->second contains the specified address. Find out michael@0: // if it has a more-specific descendant that also contains it. If it does, michael@0: // it will set |entry| appropriately. If not, set |entry| to the child. michael@0: if (!iterator->second->RetrieveRange(address, entry)) michael@0: *entry = iterator->second->entry_; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: template michael@0: void ContainedRangeMap::Clear() { michael@0: if (map_) { michael@0: MapConstIterator end = map_->end(); michael@0: for (MapConstIterator child = map_->begin(); child != end; ++child) michael@0: delete child->second; michael@0: michael@0: delete map_; michael@0: map_ = NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: michael@0: #endif // PROCESSOR_CONTAINED_RANGE_MAP_INL_H__