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

http://github.com/tomahawk-player/tomahawk · C++ Header · 197 lines · 73 code · 37 blank · 87 comment · 27 complexity · 95c664b914b01996cc917fafb04c768a MD5 · raw file

  1. // Copyright (c) 2006, 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. // contained_range_map-inl.h: Hierarchically-organized range map implementation.
  30. //
  31. // See contained_range_map.h for documentation.
  32. //
  33. // Author: Mark Mentovai
  34. #ifndef PROCESSOR_CONTAINED_RANGE_MAP_INL_H__
  35. #define PROCESSOR_CONTAINED_RANGE_MAP_INL_H__
  36. #include "processor/contained_range_map.h"
  37. #include <assert.h>
  38. #include "processor/logging.h"
  39. namespace google_breakpad {
  40. template<typename AddressType, typename EntryType>
  41. ContainedRangeMap<AddressType, EntryType>::~ContainedRangeMap() {
  42. // Clear frees the children pointed to by the map, and frees the map itself.
  43. Clear();
  44. }
  45. template<typename AddressType, typename EntryType>
  46. bool ContainedRangeMap<AddressType, EntryType>::StoreRange(
  47. const AddressType &base, const AddressType &size, const EntryType &entry) {
  48. AddressType high = base + size - 1;
  49. // Check for undersize or overflow.
  50. if (size <= 0 || high < base) {
  51. //TODO(nealsid) We are commenting this out in order to prevent
  52. // excessive logging. We plan to move to better logging as this
  53. // failure happens quite often and is expected(see comment in
  54. // basic_source_line_resolver.cc:671).
  55. // BPLOG(INFO) << "StoreRange failed, " << HexString(base) << "+"
  56. // << HexString(size) << ", " << HexString(high);
  57. return false;
  58. }
  59. if (!map_)
  60. map_ = new AddressToRangeMap();
  61. MapIterator iterator_base = map_->lower_bound(base);
  62. MapIterator iterator_high = map_->lower_bound(high);
  63. MapIterator iterator_end = map_->end();
  64. if (iterator_base == iterator_high && iterator_base != iterator_end &&
  65. base >= iterator_base->second->base_) {
  66. // The new range is entirely within an existing child range.
  67. // If the new range's geometry is exactly equal to an existing child
  68. // range's, it violates the containment rules, and an attempt to store
  69. // it must fail. iterator_base->first contains the key, which was the
  70. // containing child's high address.
  71. if (iterator_base->second->base_ == base && iterator_base->first == high) {
  72. // TODO(nealsid): See the TODO above on why this is commented out.
  73. // BPLOG(INFO) << "StoreRange failed, identical range is already "
  74. // "present: " << HexString(base) << "+" << HexString(size);
  75. return false;
  76. }
  77. // Pass the new range on to the child to attempt to store.
  78. return iterator_base->second->StoreRange(base, size, entry);
  79. }
  80. // iterator_high might refer to an irrelevant range: one whose base address
  81. // is higher than the new range's high address. Set contains_high to true
  82. // only if iterator_high refers to a range that is at least partially
  83. // within the new range.
  84. bool contains_high = iterator_high != iterator_end &&
  85. high >= iterator_high->second->base_;
  86. // If the new range encompasses any existing child ranges, it must do so
  87. // fully. Partial containment isn't allowed.
  88. if ((iterator_base != iterator_end && base > iterator_base->second->base_) ||
  89. (contains_high && high < iterator_high->first)) {
  90. // TODO(mmentovai): Some symbol files will trip this check frequently
  91. // on STACK lines. Too many messages will be produced. These are more
  92. // suitable for a DEBUG channel than an INFO channel.
  93. // BPLOG(INFO) << "StoreRange failed, new range partially contains "
  94. // "existing range: " << HexString(base) << "+" <<
  95. // HexString(size);
  96. return false;
  97. }
  98. // When copying and erasing contained ranges, the "end" iterator needs to
  99. // point one past the last item of the range to copy. If contains_high is
  100. // false, the iterator's already in the right place; the increment is safe
  101. // because contains_high can't be true if iterator_high == iterator_end.
  102. if (contains_high)
  103. ++iterator_high;
  104. // Optimization: if the iterators are equal, no child ranges would be
  105. // moved. Create the new child range with a NULL map to conserve space
  106. // in leaf nodes, of which there will be many.
  107. AddressToRangeMap *child_map = NULL;
  108. if (iterator_base != iterator_high) {
  109. // The children of this range that are contained by the new range must
  110. // be transferred over to the new range. Create the new child range map
  111. // and copy the pointers to range maps it should contain into it.
  112. child_map = new AddressToRangeMap(iterator_base, iterator_high);
  113. // Remove the copied child pointers from this range's map of children.
  114. map_->erase(iterator_base, iterator_high);
  115. }
  116. // Store the new range in the map by its high address. Any children that
  117. // the new child range contains were formerly children of this range but
  118. // are now this range's grandchildren. Ownership of these is transferred
  119. // to the new child range.
  120. map_->insert(MapValue(high,
  121. new ContainedRangeMap(base, entry, child_map)));
  122. return true;
  123. }
  124. template<typename AddressType, typename EntryType>
  125. bool ContainedRangeMap<AddressType, EntryType>::RetrieveRange(
  126. const AddressType &address, EntryType *entry) const {
  127. BPLOG_IF(ERROR, !entry) << "ContainedRangeMap::RetrieveRange requires "
  128. "|entry|";
  129. assert(entry);
  130. // If nothing was ever stored, then there's nothing to retrieve.
  131. if (!map_)
  132. return false;
  133. // Get an iterator to the child range whose high address is equal to or
  134. // greater than the supplied address. If the supplied address is higher
  135. // than all of the high addresses in the range, then this range does not
  136. // contain a child at address, so return false. If the supplied address
  137. // is lower than the base address of the child range, then it is not within
  138. // the child range, so return false.
  139. MapConstIterator iterator = map_->lower_bound(address);
  140. if (iterator == map_->end() || address < iterator->second->base_)
  141. return false;
  142. // The child in iterator->second contains the specified address. Find out
  143. // if it has a more-specific descendant that also contains it. If it does,
  144. // it will set |entry| appropriately. If not, set |entry| to the child.
  145. if (!iterator->second->RetrieveRange(address, entry))
  146. *entry = iterator->second->entry_;
  147. return true;
  148. }
  149. template<typename AddressType, typename EntryType>
  150. void ContainedRangeMap<AddressType, EntryType>::Clear() {
  151. if (map_) {
  152. MapConstIterator end = map_->end();
  153. for (MapConstIterator child = map_->begin(); child != end; ++child)
  154. delete child->second;
  155. delete map_;
  156. map_ = NULL;
  157. }
  158. }
  159. } // namespace google_breakpad
  160. #endif // PROCESSOR_CONTAINED_RANGE_MAP_INL_H__