1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // address_map-inl.h: Address map implementation.
30 //
31 // See address_map.h for documentation.
32 //
33 // Author: Mark Mentovai
34
35 #ifndef PROCESSOR_ADDRESS_MAP_INL_H__
36 #define PROCESSOR_ADDRESS_MAP_INL_H__
37
38 #include "processor/address_map.h"
39
40 #include <assert.h>
41
42 #include "processor/logging.h"
43
44 namespace google_breakpad {
45
46 template<typename AddressType, typename EntryType>
Store(const AddressType & address,const EntryType & entry)47 bool AddressMap<AddressType, EntryType>::Store(const AddressType& address,
48 const EntryType& entry) {
49 // Ensure that the specified address doesn't conflict with something already
50 // in the map.
51 if (map_.find(address) != map_.end()) {
52 BPLOG(INFO) << "Store failed, address " << HexString(address) <<
53 " is already present";
54 return false;
55 }
56
57 map_.insert(MapValue(address, entry));
58 return true;
59 }
60
61 template<typename AddressType, typename EntryType>
Retrieve(const AddressType & address,EntryType * entry,AddressType * entry_address)62 bool AddressMap<AddressType, EntryType>::Retrieve(
63 const AddressType& address,
64 EntryType* entry, AddressType* entry_address) const {
65 BPLOG_IF(ERROR, !entry) << "AddressMap::Retrieve requires |entry|";
66 assert(entry);
67
68 // upper_bound gives the first element whose key is greater than address,
69 // but we want the first element whose key is less than or equal to address.
70 // Decrement the iterator to get there, but not if the upper_bound already
71 // points to the beginning of the map - in that case, address is lower than
72 // the lowest stored key, so return false.
73 MapConstIterator iterator = map_.upper_bound(address);
74 if (iterator == map_.begin())
75 return false;
76 --iterator;
77
78 *entry = iterator->second;
79 if (entry_address)
80 *entry_address = iterator->first;
81
82 return true;
83 }
84
85 template<typename AddressType, typename EntryType>
Clear()86 void AddressMap<AddressType, EntryType>::Clear() {
87 map_.clear();
88 }
89
90 } // namespace google_breakpad
91
92 #endif // PROCESSOR_ADDRESS_MAP_INL_H__
93