1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/range_map-inl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,210 @@ 1.4 +// Copyright (c) 2010 Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// range_map-inl.h: Range map implementation. 1.34 +// 1.35 +// See range_map.h for documentation. 1.36 +// 1.37 +// Author: Mark Mentovai 1.38 + 1.39 +#ifndef PROCESSOR_RANGE_MAP_INL_H__ 1.40 +#define PROCESSOR_RANGE_MAP_INL_H__ 1.41 + 1.42 + 1.43 +#include <assert.h> 1.44 + 1.45 +#include "processor/range_map.h" 1.46 +#include "common/logging.h" 1.47 + 1.48 + 1.49 +namespace google_breakpad { 1.50 + 1.51 + 1.52 +template<typename AddressType, typename EntryType> 1.53 +bool RangeMap<AddressType, EntryType>::StoreRange(const AddressType &base, 1.54 + const AddressType &size, 1.55 + const EntryType &entry) { 1.56 + AddressType high = base + size - 1; 1.57 + 1.58 + // Check for undersize or overflow. 1.59 + if (size <= 0 || high < base) { 1.60 + // The processor will hit this case too frequently with common symbol 1.61 + // files in the size == 0 case, which is more suited to a DEBUG channel. 1.62 + // Filter those out since there's no DEBUG channel at the moment. 1.63 + BPLOG_IF(INFO, size != 0) << "StoreRange failed, " << HexString(base) << 1.64 + "+" << HexString(size) << ", " << 1.65 + HexString(high); 1.66 + return false; 1.67 + } 1.68 + 1.69 + // Ensure that this range does not overlap with another one already in the 1.70 + // map. 1.71 + MapConstIterator iterator_base = map_.lower_bound(base); 1.72 + MapConstIterator iterator_high = map_.lower_bound(high); 1.73 + 1.74 + if (iterator_base != iterator_high) { 1.75 + // Some other range begins in the space used by this range. It may be 1.76 + // contained within the space used by this range, or it may extend lower. 1.77 + // Regardless, it is an error. 1.78 + AddressType other_base = iterator_base->second.base(); 1.79 + AddressType other_size = iterator_base->first - other_base + 1; 1.80 + BPLOG(INFO) << "StoreRange failed, an existing range is contained by or " 1.81 + "extends lower than the new range: new " << 1.82 + HexString(base) << "+" << HexString(size) << ", existing " << 1.83 + HexString(other_base) << "+" << HexString(other_size); 1.84 + return false; 1.85 + } 1.86 + 1.87 + if (iterator_high != map_.end()) { 1.88 + if (iterator_high->second.base() <= high) { 1.89 + // The range above this one overlaps with this one. It may fully 1.90 + // contain this range, or it may begin within this range and extend 1.91 + // higher. Regardless, it's an error. 1.92 + AddressType other_base = iterator_high->second.base(); 1.93 + AddressType other_size = iterator_high->first - other_base + 1; 1.94 + BPLOG(INFO) << "StoreRange failed, an existing range contains or " 1.95 + "extends higher than the new range: new " << 1.96 + HexString(base) << "+" << HexString(size) << 1.97 + ", existing " << 1.98 + HexString(other_base) << "+" << HexString(other_size); 1.99 + return false; 1.100 + } 1.101 + } 1.102 + 1.103 + // Store the range in the map by its high address, so that lower_bound can 1.104 + // be used to quickly locate a range by address. 1.105 + map_.insert(MapValue(high, Range(base, entry))); 1.106 + return true; 1.107 +} 1.108 + 1.109 + 1.110 +template<typename AddressType, typename EntryType> 1.111 +bool RangeMap<AddressType, EntryType>::RetrieveRange( 1.112 + const AddressType &address, EntryType *entry, 1.113 + AddressType *entry_base, AddressType *entry_size) const { 1.114 + BPLOG_IF(ERROR, !entry) << "RangeMap::RetrieveRange requires |entry|"; 1.115 + assert(entry); 1.116 + 1.117 + MapConstIterator iterator = map_.lower_bound(address); 1.118 + if (iterator == map_.end()) 1.119 + return false; 1.120 + 1.121 + // The map is keyed by the high address of each range, so |address| is 1.122 + // guaranteed to be lower than the range's high address. If |range| is 1.123 + // not directly preceded by another range, it's possible for address to 1.124 + // be below the range's low address, though. When that happens, address 1.125 + // references something not within any range, so return false. 1.126 + if (address < iterator->second.base()) 1.127 + return false; 1.128 + 1.129 + *entry = iterator->second.entry(); 1.130 + if (entry_base) 1.131 + *entry_base = iterator->second.base(); 1.132 + if (entry_size) 1.133 + *entry_size = iterator->first - iterator->second.base() + 1; 1.134 + 1.135 + return true; 1.136 +} 1.137 + 1.138 + 1.139 +template<typename AddressType, typename EntryType> 1.140 +bool RangeMap<AddressType, EntryType>::RetrieveNearestRange( 1.141 + const AddressType &address, EntryType *entry, 1.142 + AddressType *entry_base, AddressType *entry_size) const { 1.143 + BPLOG_IF(ERROR, !entry) << "RangeMap::RetrieveNearestRange requires |entry|"; 1.144 + assert(entry); 1.145 + 1.146 + // If address is within a range, RetrieveRange can handle it. 1.147 + if (RetrieveRange(address, entry, entry_base, entry_size)) 1.148 + return true; 1.149 + 1.150 + // upper_bound gives the first element whose key is greater than address, 1.151 + // but we want the first element whose key is less than or equal to address. 1.152 + // Decrement the iterator to get there, but not if the upper_bound already 1.153 + // points to the beginning of the map - in that case, address is lower than 1.154 + // the lowest stored key, so return false. 1.155 + MapConstIterator iterator = map_.upper_bound(address); 1.156 + if (iterator == map_.begin()) 1.157 + return false; 1.158 + --iterator; 1.159 + 1.160 + *entry = iterator->second.entry(); 1.161 + if (entry_base) 1.162 + *entry_base = iterator->second.base(); 1.163 + if (entry_size) 1.164 + *entry_size = iterator->first - iterator->second.base() + 1; 1.165 + 1.166 + return true; 1.167 +} 1.168 + 1.169 + 1.170 +template<typename AddressType, typename EntryType> 1.171 +bool RangeMap<AddressType, EntryType>::RetrieveRangeAtIndex( 1.172 + int index, EntryType *entry, 1.173 + AddressType *entry_base, AddressType *entry_size) const { 1.174 + BPLOG_IF(ERROR, !entry) << "RangeMap::RetrieveRangeAtIndex requires |entry|"; 1.175 + assert(entry); 1.176 + 1.177 + if (index >= GetCount()) { 1.178 + BPLOG(ERROR) << "Index out of range: " << index << "/" << GetCount(); 1.179 + return false; 1.180 + } 1.181 + 1.182 + // Walk through the map. Although it's ordered, it's not a vector, so it 1.183 + // can't be addressed directly by index. 1.184 + MapConstIterator iterator = map_.begin(); 1.185 + for (int this_index = 0; this_index < index; ++this_index) 1.186 + ++iterator; 1.187 + 1.188 + *entry = iterator->second.entry(); 1.189 + if (entry_base) 1.190 + *entry_base = iterator->second.base(); 1.191 + if (entry_size) 1.192 + *entry_size = iterator->first - iterator->second.base() + 1; 1.193 + 1.194 + return true; 1.195 +} 1.196 + 1.197 + 1.198 +template<typename AddressType, typename EntryType> 1.199 +int RangeMap<AddressType, EntryType>::GetCount() const { 1.200 + return map_.size(); 1.201 +} 1.202 + 1.203 + 1.204 +template<typename AddressType, typename EntryType> 1.205 +void RangeMap<AddressType, EntryType>::Clear() { 1.206 + map_.clear(); 1.207 +} 1.208 + 1.209 + 1.210 +} // namespace google_breakpad 1.211 + 1.212 + 1.213 +#endif // PROCESSOR_RANGE_MAP_INL_H__