Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // contained_range_map-inl.h: Hierarchically-organized range map implementation. |
michael@0 | 31 | // |
michael@0 | 32 | // See contained_range_map.h for documentation. |
michael@0 | 33 | // |
michael@0 | 34 | // Author: Mark Mentovai |
michael@0 | 35 | |
michael@0 | 36 | #ifndef PROCESSOR_CONTAINED_RANGE_MAP_INL_H__ |
michael@0 | 37 | #define PROCESSOR_CONTAINED_RANGE_MAP_INL_H__ |
michael@0 | 38 | |
michael@0 | 39 | #include "processor/contained_range_map.h" |
michael@0 | 40 | |
michael@0 | 41 | #include <assert.h> |
michael@0 | 42 | |
michael@0 | 43 | #include "common/logging.h" |
michael@0 | 44 | |
michael@0 | 45 | |
michael@0 | 46 | namespace google_breakpad { |
michael@0 | 47 | |
michael@0 | 48 | |
michael@0 | 49 | template<typename AddressType, typename EntryType> |
michael@0 | 50 | ContainedRangeMap<AddressType, EntryType>::~ContainedRangeMap() { |
michael@0 | 51 | // Clear frees the children pointed to by the map, and frees the map itself. |
michael@0 | 52 | Clear(); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | template<typename AddressType, typename EntryType> |
michael@0 | 57 | bool ContainedRangeMap<AddressType, EntryType>::StoreRange( |
michael@0 | 58 | const AddressType &base, const AddressType &size, const EntryType &entry) { |
michael@0 | 59 | AddressType high = base + size - 1; |
michael@0 | 60 | |
michael@0 | 61 | // Check for undersize or overflow. |
michael@0 | 62 | if (size <= 0 || high < base) { |
michael@0 | 63 | //TODO(nealsid) We are commenting this out in order to prevent |
michael@0 | 64 | // excessive logging. We plan to move to better logging as this |
michael@0 | 65 | // failure happens quite often and is expected(see comment in |
michael@0 | 66 | // basic_source_line_resolver.cc:671). |
michael@0 | 67 | // BPLOG(INFO) << "StoreRange failed, " << HexString(base) << "+" |
michael@0 | 68 | // << HexString(size) << ", " << HexString(high); |
michael@0 | 69 | return false; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | if (!map_) |
michael@0 | 73 | map_ = new AddressToRangeMap(); |
michael@0 | 74 | |
michael@0 | 75 | MapIterator iterator_base = map_->lower_bound(base); |
michael@0 | 76 | MapIterator iterator_high = map_->lower_bound(high); |
michael@0 | 77 | MapIterator iterator_end = map_->end(); |
michael@0 | 78 | |
michael@0 | 79 | if (iterator_base == iterator_high && iterator_base != iterator_end && |
michael@0 | 80 | base >= iterator_base->second->base_) { |
michael@0 | 81 | // The new range is entirely within an existing child range. |
michael@0 | 82 | |
michael@0 | 83 | // If the new range's geometry is exactly equal to an existing child |
michael@0 | 84 | // range's, it violates the containment rules, and an attempt to store |
michael@0 | 85 | // it must fail. iterator_base->first contains the key, which was the |
michael@0 | 86 | // containing child's high address. |
michael@0 | 87 | if (iterator_base->second->base_ == base && iterator_base->first == high) { |
michael@0 | 88 | // TODO(nealsid): See the TODO above on why this is commented out. |
michael@0 | 89 | // BPLOG(INFO) << "StoreRange failed, identical range is already " |
michael@0 | 90 | // "present: " << HexString(base) << "+" << HexString(size); |
michael@0 | 91 | return false; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // Pass the new range on to the child to attempt to store. |
michael@0 | 95 | return iterator_base->second->StoreRange(base, size, entry); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // iterator_high might refer to an irrelevant range: one whose base address |
michael@0 | 99 | // is higher than the new range's high address. Set contains_high to true |
michael@0 | 100 | // only if iterator_high refers to a range that is at least partially |
michael@0 | 101 | // within the new range. |
michael@0 | 102 | bool contains_high = iterator_high != iterator_end && |
michael@0 | 103 | high >= iterator_high->second->base_; |
michael@0 | 104 | |
michael@0 | 105 | // If the new range encompasses any existing child ranges, it must do so |
michael@0 | 106 | // fully. Partial containment isn't allowed. |
michael@0 | 107 | if ((iterator_base != iterator_end && base > iterator_base->second->base_) || |
michael@0 | 108 | (contains_high && high < iterator_high->first)) { |
michael@0 | 109 | // TODO(mmentovai): Some symbol files will trip this check frequently |
michael@0 | 110 | // on STACK lines. Too many messages will be produced. These are more |
michael@0 | 111 | // suitable for a DEBUG channel than an INFO channel. |
michael@0 | 112 | // BPLOG(INFO) << "StoreRange failed, new range partially contains " |
michael@0 | 113 | // "existing range: " << HexString(base) << "+" << |
michael@0 | 114 | // HexString(size); |
michael@0 | 115 | return false; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | // When copying and erasing contained ranges, the "end" iterator needs to |
michael@0 | 119 | // point one past the last item of the range to copy. If contains_high is |
michael@0 | 120 | // false, the iterator's already in the right place; the increment is safe |
michael@0 | 121 | // because contains_high can't be true if iterator_high == iterator_end. |
michael@0 | 122 | if (contains_high) |
michael@0 | 123 | ++iterator_high; |
michael@0 | 124 | |
michael@0 | 125 | // Optimization: if the iterators are equal, no child ranges would be |
michael@0 | 126 | // moved. Create the new child range with a NULL map to conserve space |
michael@0 | 127 | // in leaf nodes, of which there will be many. |
michael@0 | 128 | AddressToRangeMap *child_map = NULL; |
michael@0 | 129 | |
michael@0 | 130 | if (iterator_base != iterator_high) { |
michael@0 | 131 | // The children of this range that are contained by the new range must |
michael@0 | 132 | // be transferred over to the new range. Create the new child range map |
michael@0 | 133 | // and copy the pointers to range maps it should contain into it. |
michael@0 | 134 | child_map = new AddressToRangeMap(iterator_base, iterator_high); |
michael@0 | 135 | |
michael@0 | 136 | // Remove the copied child pointers from this range's map of children. |
michael@0 | 137 | map_->erase(iterator_base, iterator_high); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | // Store the new range in the map by its high address. Any children that |
michael@0 | 141 | // the new child range contains were formerly children of this range but |
michael@0 | 142 | // are now this range's grandchildren. Ownership of these is transferred |
michael@0 | 143 | // to the new child range. |
michael@0 | 144 | map_->insert(MapValue(high, |
michael@0 | 145 | new ContainedRangeMap(base, entry, child_map))); |
michael@0 | 146 | return true; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | |
michael@0 | 150 | template<typename AddressType, typename EntryType> |
michael@0 | 151 | bool ContainedRangeMap<AddressType, EntryType>::RetrieveRange( |
michael@0 | 152 | const AddressType &address, EntryType *entry) const { |
michael@0 | 153 | BPLOG_IF(ERROR, !entry) << "ContainedRangeMap::RetrieveRange requires " |
michael@0 | 154 | "|entry|"; |
michael@0 | 155 | assert(entry); |
michael@0 | 156 | |
michael@0 | 157 | // If nothing was ever stored, then there's nothing to retrieve. |
michael@0 | 158 | if (!map_) |
michael@0 | 159 | return false; |
michael@0 | 160 | |
michael@0 | 161 | // Get an iterator to the child range whose high address is equal to or |
michael@0 | 162 | // greater than the supplied address. If the supplied address is higher |
michael@0 | 163 | // than all of the high addresses in the range, then this range does not |
michael@0 | 164 | // contain a child at address, so return false. If the supplied address |
michael@0 | 165 | // is lower than the base address of the child range, then it is not within |
michael@0 | 166 | // the child range, so return false. |
michael@0 | 167 | MapConstIterator iterator = map_->lower_bound(address); |
michael@0 | 168 | if (iterator == map_->end() || address < iterator->second->base_) |
michael@0 | 169 | return false; |
michael@0 | 170 | |
michael@0 | 171 | // The child in iterator->second contains the specified address. Find out |
michael@0 | 172 | // if it has a more-specific descendant that also contains it. If it does, |
michael@0 | 173 | // it will set |entry| appropriately. If not, set |entry| to the child. |
michael@0 | 174 | if (!iterator->second->RetrieveRange(address, entry)) |
michael@0 | 175 | *entry = iterator->second->entry_; |
michael@0 | 176 | |
michael@0 | 177 | return true; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | |
michael@0 | 181 | template<typename AddressType, typename EntryType> |
michael@0 | 182 | void ContainedRangeMap<AddressType, EntryType>::Clear() { |
michael@0 | 183 | if (map_) { |
michael@0 | 184 | MapConstIterator end = map_->end(); |
michael@0 | 185 | for (MapConstIterator child = map_->begin(); child != end; ++child) |
michael@0 | 186 | delete child->second; |
michael@0 | 187 | |
michael@0 | 188 | delete map_; |
michael@0 | 189 | map_ = NULL; |
michael@0 | 190 | } |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | |
michael@0 | 194 | } // namespace google_breakpad |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | #endif // PROCESSOR_CONTAINED_RANGE_MAP_INL_H__ |