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