/thirdparty/breakpad/processor/range_map-inl.h

http://github.com/tomahawk-player/tomahawk · C Header · 210 lines · 110 code · 40 blank · 60 comment · 22 complexity · d886a33bef03fd6d11c21e4f68eb0074 MD5 · raw file

  1. // Copyright (c) 2010 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // range_map-inl.h: Range map implementation.
  30. //
  31. // See range_map.h for documentation.
  32. //
  33. // Author: Mark Mentovai
  34. #ifndef PROCESSOR_RANGE_MAP_INL_H__
  35. #define PROCESSOR_RANGE_MAP_INL_H__
  36. #include <assert.h>
  37. #include "processor/range_map.h"
  38. #include "processor/logging.h"
  39. namespace google_breakpad {
  40. template<typename AddressType, typename EntryType>
  41. bool RangeMap<AddressType, EntryType>::StoreRange(const AddressType &base,
  42. const AddressType &size,
  43. const EntryType &entry) {
  44. AddressType high = base + size - 1;
  45. // Check for undersize or overflow.
  46. if (size <= 0 || high < base) {
  47. // The processor will hit this case too frequently with common symbol
  48. // files in the size == 0 case, which is more suited to a DEBUG channel.
  49. // Filter those out since there's no DEBUG channel at the moment.
  50. BPLOG_IF(INFO, size != 0) << "StoreRange failed, " << HexString(base) <<
  51. "+" << HexString(size) << ", " <<
  52. HexString(high);
  53. return false;
  54. }
  55. // Ensure that this range does not overlap with another one already in the
  56. // map.
  57. MapConstIterator iterator_base = map_.lower_bound(base);
  58. MapConstIterator iterator_high = map_.lower_bound(high);
  59. if (iterator_base != iterator_high) {
  60. // Some other range begins in the space used by this range. It may be
  61. // contained within the space used by this range, or it may extend lower.
  62. // Regardless, it is an error.
  63. AddressType other_base = iterator_base->second.base();
  64. AddressType other_size = iterator_base->first - other_base + 1;
  65. BPLOG(INFO) << "StoreRange failed, an existing range is contained by or "
  66. "extends lower than the new range: new " <<
  67. HexString(base) << "+" << HexString(size) << ", existing " <<
  68. HexString(other_base) << "+" << HexString(other_size);
  69. return false;
  70. }
  71. if (iterator_high != map_.end()) {
  72. if (iterator_high->second.base() <= high) {
  73. // The range above this one overlaps with this one. It may fully
  74. // contain this range, or it may begin within this range and extend
  75. // higher. Regardless, it's an error.
  76. AddressType other_base = iterator_high->second.base();
  77. AddressType other_size = iterator_high->first - other_base + 1;
  78. BPLOG(INFO) << "StoreRange failed, an existing range contains or "
  79. "extends higher than the new range: new " <<
  80. HexString(base) << "+" << HexString(size) <<
  81. ", existing " <<
  82. HexString(other_base) << "+" << HexString(other_size);
  83. return false;
  84. }
  85. }
  86. // Store the range in the map by its high address, so that lower_bound can
  87. // be used to quickly locate a range by address.
  88. map_.insert(MapValue(high, Range(base, entry)));
  89. return true;
  90. }
  91. template<typename AddressType, typename EntryType>
  92. bool RangeMap<AddressType, EntryType>::RetrieveRange(
  93. const AddressType &address, EntryType *entry,
  94. AddressType *entry_base, AddressType *entry_size) const {
  95. BPLOG_IF(ERROR, !entry) << "RangeMap::RetrieveRange requires |entry|";
  96. assert(entry);
  97. MapConstIterator iterator = map_.lower_bound(address);
  98. if (iterator == map_.end())
  99. return false;
  100. // The map is keyed by the high address of each range, so |address| is
  101. // guaranteed to be lower than the range's high address. If |range| is
  102. // not directly preceded by another range, it's possible for address to
  103. // be below the range's low address, though. When that happens, address
  104. // references something not within any range, so return false.
  105. if (address < iterator->second.base())
  106. return false;
  107. *entry = iterator->second.entry();
  108. if (entry_base)
  109. *entry_base = iterator->second.base();
  110. if (entry_size)
  111. *entry_size = iterator->first - iterator->second.base() + 1;
  112. return true;
  113. }
  114. template<typename AddressType, typename EntryType>
  115. bool RangeMap<AddressType, EntryType>::RetrieveNearestRange(
  116. const AddressType &address, EntryType *entry,
  117. AddressType *entry_base, AddressType *entry_size) const {
  118. BPLOG_IF(ERROR, !entry) << "RangeMap::RetrieveNearestRange requires |entry|";
  119. assert(entry);
  120. // If address is within a range, RetrieveRange can handle it.
  121. if (RetrieveRange(address, entry, entry_base, entry_size))
  122. return true;
  123. // upper_bound gives the first element whose key is greater than address,
  124. // but we want the first element whose key is less than or equal to address.
  125. // Decrement the iterator to get there, but not if the upper_bound already
  126. // points to the beginning of the map - in that case, address is lower than
  127. // the lowest stored key, so return false.
  128. MapConstIterator iterator = map_.upper_bound(address);
  129. if (iterator == map_.begin())
  130. return false;
  131. --iterator;
  132. *entry = iterator->second.entry();
  133. if (entry_base)
  134. *entry_base = iterator->second.base();
  135. if (entry_size)
  136. *entry_size = iterator->first - iterator->second.base() + 1;
  137. return true;
  138. }
  139. template<typename AddressType, typename EntryType>
  140. bool RangeMap<AddressType, EntryType>::RetrieveRangeAtIndex(
  141. int index, EntryType *entry,
  142. AddressType *entry_base, AddressType *entry_size) const {
  143. BPLOG_IF(ERROR, !entry) << "RangeMap::RetrieveRangeAtIndex requires |entry|";
  144. assert(entry);
  145. if (index >= GetCount()) {
  146. BPLOG(ERROR) << "Index out of range: " << index << "/" << GetCount();
  147. return false;
  148. }
  149. // Walk through the map. Although it's ordered, it's not a vector, so it
  150. // can't be addressed directly by index.
  151. MapConstIterator iterator = map_.begin();
  152. for (int this_index = 0; this_index < index; ++this_index)
  153. ++iterator;
  154. *entry = iterator->second.entry();
  155. if (entry_base)
  156. *entry_base = iterator->second.base();
  157. if (entry_size)
  158. *entry_size = iterator->first - iterator->second.base() + 1;
  159. return true;
  160. }
  161. template<typename AddressType, typename EntryType>
  162. int RangeMap<AddressType, EntryType>::GetCount() const {
  163. return map_.size();
  164. }
  165. template<typename AddressType, typename EntryType>
  166. void RangeMap<AddressType, EntryType>::Clear() {
  167. map_.clear();
  168. }
  169. } // namespace google_breakpad
  170. #endif // PROCESSOR_RANGE_MAP_INL_H__