12fc823f5794737391e231c1dce6c2b0793213e53mmentovai// Copyright (c) 2006, Google Inc.
22fc823f5794737391e231c1dce6c2b0793213e53mmentovai// All rights reserved.
32fc823f5794737391e231c1dce6c2b0793213e53mmentovai//
42fc823f5794737391e231c1dce6c2b0793213e53mmentovai// Redistribution and use in source and binary forms, with or without
52fc823f5794737391e231c1dce6c2b0793213e53mmentovai// modification, are permitted provided that the following conditions are
62fc823f5794737391e231c1dce6c2b0793213e53mmentovai// met:
72fc823f5794737391e231c1dce6c2b0793213e53mmentovai//
82fc823f5794737391e231c1dce6c2b0793213e53mmentovai//     * Redistributions of source code must retain the above copyright
92fc823f5794737391e231c1dce6c2b0793213e53mmentovai// notice, this list of conditions and the following disclaimer.
102fc823f5794737391e231c1dce6c2b0793213e53mmentovai//     * Redistributions in binary form must reproduce the above
112fc823f5794737391e231c1dce6c2b0793213e53mmentovai// copyright notice, this list of conditions and the following disclaimer
122fc823f5794737391e231c1dce6c2b0793213e53mmentovai// in the documentation and/or other materials provided with the
132fc823f5794737391e231c1dce6c2b0793213e53mmentovai// distribution.
142fc823f5794737391e231c1dce6c2b0793213e53mmentovai//     * Neither the name of Google Inc. nor the names of its
152fc823f5794737391e231c1dce6c2b0793213e53mmentovai// contributors may be used to endorse or promote products derived from
162fc823f5794737391e231c1dce6c2b0793213e53mmentovai// this software without specific prior written permission.
172fc823f5794737391e231c1dce6c2b0793213e53mmentovai//
182fc823f5794737391e231c1dce6c2b0793213e53mmentovai// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
192fc823f5794737391e231c1dce6c2b0793213e53mmentovai// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
202fc823f5794737391e231c1dce6c2b0793213e53mmentovai// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
212fc823f5794737391e231c1dce6c2b0793213e53mmentovai// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
222fc823f5794737391e231c1dce6c2b0793213e53mmentovai// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
232fc823f5794737391e231c1dce6c2b0793213e53mmentovai// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
242fc823f5794737391e231c1dce6c2b0793213e53mmentovai// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252fc823f5794737391e231c1dce6c2b0793213e53mmentovai// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
262fc823f5794737391e231c1dce6c2b0793213e53mmentovai// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
272fc823f5794737391e231c1dce6c2b0793213e53mmentovai// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
282fc823f5794737391e231c1dce6c2b0793213e53mmentovai// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
292fc823f5794737391e231c1dce6c2b0793213e53mmentovai
302fc823f5794737391e231c1dce6c2b0793213e53mmentovai// address_map.h: Address maps.
312fc823f5794737391e231c1dce6c2b0793213e53mmentovai//
322fc823f5794737391e231c1dce6c2b0793213e53mmentovai// An address map contains a set of objects keyed by address.  Objects are
332fc823f5794737391e231c1dce6c2b0793213e53mmentovai// retrieved from the map by returning the object with the highest key less
342fc823f5794737391e231c1dce6c2b0793213e53mmentovai// than or equal to the lookup key.
352fc823f5794737391e231c1dce6c2b0793213e53mmentovai//
362fc823f5794737391e231c1dce6c2b0793213e53mmentovai// Author: Mark Mentovai
372fc823f5794737391e231c1dce6c2b0793213e53mmentovai
382fc823f5794737391e231c1dce6c2b0793213e53mmentovai#ifndef PROCESSOR_ADDRESS_MAP_H__
392fc823f5794737391e231c1dce6c2b0793213e53mmentovai#define PROCESSOR_ADDRESS_MAP_H__
402fc823f5794737391e231c1dce6c2b0793213e53mmentovai
412fc823f5794737391e231c1dce6c2b0793213e53mmentovai#include <map>
422fc823f5794737391e231c1dce6c2b0793213e53mmentovai
43e5dc60822e5938fea2ae892ccddb906641ba174emmentovainamespace google_breakpad {
442fc823f5794737391e231c1dce6c2b0793213e53mmentovai
4508730fc9a639e5b962f9a803ae8f5e91630e9484SiyangXie@gmail.com// Forward declarations (for later friend declarations).
4608730fc9a639e5b962f9a803ae8f5e91630e9484SiyangXie@gmail.comtemplate<class, class> class AddressMapSerializer;
4708730fc9a639e5b962f9a803ae8f5e91630e9484SiyangXie@gmail.com
482fc823f5794737391e231c1dce6c2b0793213e53mmentovaitemplate<typename AddressType, typename EntryType>
492fc823f5794737391e231c1dce6c2b0793213e53mmentovaiclass AddressMap {
502fc823f5794737391e231c1dce6c2b0793213e53mmentovai public:
512fc823f5794737391e231c1dce6c2b0793213e53mmentovai  AddressMap() : map_() {}
522fc823f5794737391e231c1dce6c2b0793213e53mmentovai
532fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // Inserts an entry into the map.  Returns false without storing the entry
542fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // if an entry is already stored in the map at the same address as specified
552fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // by the address argument.
562fc823f5794737391e231c1dce6c2b0793213e53mmentovai  bool Store(const AddressType &address, const EntryType &entry);
572fc823f5794737391e231c1dce6c2b0793213e53mmentovai
582fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // Locates the entry stored at the highest address less than or equal to
5965571f17edb82d122b5f6dc741bd7d4b9e315e1bmmentovai  // the address argument.  If there is no such range, returns false.  The
6065571f17edb82d122b5f6dc741bd7d4b9e315e1bmmentovai  // entry is returned in entry, which is a required argument.  If
612fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // entry_address is not NULL, it will be set to the address that the entry
622fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // was stored at.
632fc823f5794737391e231c1dce6c2b0793213e53mmentovai  bool Retrieve(const AddressType &address,
642fc823f5794737391e231c1dce6c2b0793213e53mmentovai                EntryType *entry, AddressType *entry_address) const;
652fc823f5794737391e231c1dce6c2b0793213e53mmentovai
662fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // Empties the address map, restoring it to the same state as when it was
672fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // initially created.
682fc823f5794737391e231c1dce6c2b0793213e53mmentovai  void Clear();
692fc823f5794737391e231c1dce6c2b0793213e53mmentovai
702fc823f5794737391e231c1dce6c2b0793213e53mmentovai private:
7108730fc9a639e5b962f9a803ae8f5e91630e9484SiyangXie@gmail.com  friend class AddressMapSerializer<AddressType, EntryType>;
7241f998fe5a0630506d6d2a1bae78b1be179fe850SiyangXie@gmail.com  friend class ModuleComparer;
7308730fc9a639e5b962f9a803ae8f5e91630e9484SiyangXie@gmail.com
742fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // Convenience types.
752fc823f5794737391e231c1dce6c2b0793213e53mmentovai  typedef std::map<AddressType, EntryType> AddressToEntryMap;
762fc823f5794737391e231c1dce6c2b0793213e53mmentovai  typedef typename AddressToEntryMap::const_iterator MapConstIterator;
772fc823f5794737391e231c1dce6c2b0793213e53mmentovai  typedef typename AddressToEntryMap::value_type MapValue;
782fc823f5794737391e231c1dce6c2b0793213e53mmentovai
792fc823f5794737391e231c1dce6c2b0793213e53mmentovai  // Maps the address of each entry to an EntryType.
802fc823f5794737391e231c1dce6c2b0793213e53mmentovai  AddressToEntryMap map_;
812fc823f5794737391e231c1dce6c2b0793213e53mmentovai};
822fc823f5794737391e231c1dce6c2b0793213e53mmentovai
83e5dc60822e5938fea2ae892ccddb906641ba174emmentovai}  // namespace google_breakpad
842fc823f5794737391e231c1dce6c2b0793213e53mmentovai
852fc823f5794737391e231c1dce6c2b0793213e53mmentovai#endif  // PROCESSOR_ADDRESS_MAP_H__
86