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// address_map.h: Address maps.
31//
32// An address map contains a set of objects keyed by address. Objects are
33// retrieved from the map by returning the object with the highest key less
34// than or equal to the lookup key.
35//
36// Author: Mark Mentovai
37
38#ifndef PROCESSOR_ADDRESS_MAP_H__
39#define PROCESSOR_ADDRESS_MAP_H__
40
41#include <map>
42
43namespace google_breakpad {
44
45// Forward declarations (for later friend declarations).
46template<class, class> class AddressMapSerializer;
47
48template<typename AddressType, typename EntryType>
49class AddressMap {
50 public:
51 AddressMap() : map_() {}
52
53 // Inserts an entry into the map. Returns false without storing the entry
54 // if an entry is already stored in the map at the same address as specified
55 // by the address argument.
56 bool Store(const AddressType& address, const EntryType& entry);
57
58 // Locates the entry stored at the highest address less than or equal to
59 // the address argument. If there is no such range, returns false. The
60 // entry is returned in entry, which is a required argument. If
61 // entry_address is not NULL, it will be set to the address that the entry
62 // was stored at.
63 bool Retrieve(const AddressType& address,
64 EntryType* entry, AddressType* entry_address) const;
65
66 // Empties the address map, restoring it to the same state as when it was
67 // initially created.
68 void Clear();
69
70 private:
71 friend class AddressMapSerializer<AddressType, EntryType>;
72 friend class ModuleComparer;
73
74 // Convenience types.
75 typedef std::map<AddressType, EntryType> AddressToEntryMap;
76 typedef typename AddressToEntryMap::const_iterator MapConstIterator;
77 typedef typename AddressToEntryMap::value_type MapValue;
78
79 // Maps the address of each entry to an EntryType.
80 AddressToEntryMap map_;
81};
82
83} // namespace google_breakpad
84
85#endif // PROCESSOR_ADDRESS_MAP_H__
86