Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // contained_range_map.h: Hierarchically-organized range maps. |
michael@0 | 31 | // |
michael@0 | 32 | // A contained range map is similar to a standard range map, except it allows |
michael@0 | 33 | // objects to be organized hierarchically. A contained range map allows |
michael@0 | 34 | // objects to contain other objects. It is not sensitive to the order that |
michael@0 | 35 | // objects are added to the map: larger, more general, containing objects |
michael@0 | 36 | // may be added either before or after smaller, more specific, contained |
michael@0 | 37 | // ones. |
michael@0 | 38 | // |
michael@0 | 39 | // Contained range maps guarantee that each object may only contain smaller |
michael@0 | 40 | // objects than itself, and that a parent object may only contain child |
michael@0 | 41 | // objects located entirely within the parent's address space. Attempts |
michael@0 | 42 | // to introduce objects (via StoreRange) that violate these rules will fail. |
michael@0 | 43 | // Retrieval (via RetrieveRange) always returns the most specific (smallest) |
michael@0 | 44 | // object that contains the address being queried. Note that while it is |
michael@0 | 45 | // not possible to insert two objects into a map that have exactly the same |
michael@0 | 46 | // geometry (base address and size), it is possible to completely mask a |
michael@0 | 47 | // larger object by inserting smaller objects that entirely fill the larger |
michael@0 | 48 | // object's address space. |
michael@0 | 49 | // |
michael@0 | 50 | // Internally, contained range maps are implemented as a tree. Each tree |
michael@0 | 51 | // node except for the root node describes an object in the map. Each node |
michael@0 | 52 | // maintains its list of children in a map similar to a standard range map, |
michael@0 | 53 | // keyed by the highest address that each child occupies. Each node's |
michael@0 | 54 | // children occupy address ranges entirely within the node. The root node |
michael@0 | 55 | // is the only node directly accessible to the user, and represents the |
michael@0 | 56 | // entire address space. |
michael@0 | 57 | // |
michael@0 | 58 | // Author: Mark Mentovai |
michael@0 | 59 | |
michael@0 | 60 | #ifndef PROCESSOR_CONTAINED_RANGE_MAP_H__ |
michael@0 | 61 | #define PROCESSOR_CONTAINED_RANGE_MAP_H__ |
michael@0 | 62 | |
michael@0 | 63 | |
michael@0 | 64 | #include <map> |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | namespace google_breakpad { |
michael@0 | 68 | |
michael@0 | 69 | // Forward declarations (for later friend declarations of specialized template). |
michael@0 | 70 | template<class, class> class ContainedRangeMapSerializer; |
michael@0 | 71 | |
michael@0 | 72 | template<typename AddressType, typename EntryType> |
michael@0 | 73 | class ContainedRangeMap { |
michael@0 | 74 | public: |
michael@0 | 75 | // The default constructor creates a ContainedRangeMap with no geometry |
michael@0 | 76 | // and no entry, and as such is only suitable for the root node of a |
michael@0 | 77 | // ContainedRangeMap tree. |
michael@0 | 78 | ContainedRangeMap() : base_(), entry_(), map_(NULL) {} |
michael@0 | 79 | |
michael@0 | 80 | ~ContainedRangeMap(); |
michael@0 | 81 | |
michael@0 | 82 | // Inserts a range into the map. If the new range is encompassed by |
michael@0 | 83 | // an existing child range, the new range is passed into the child range's |
michael@0 | 84 | // StoreRange method. If the new range encompasses any existing child |
michael@0 | 85 | // ranges, those child ranges are moved to the new range, becoming |
michael@0 | 86 | // grandchildren of this ContainedRangeMap. Returns false for a |
michael@0 | 87 | // parameter error, or if the ContainedRangeMap hierarchy guarantees |
michael@0 | 88 | // would be violated. |
michael@0 | 89 | bool StoreRange(const AddressType &base, |
michael@0 | 90 | const AddressType &size, |
michael@0 | 91 | const EntryType &entry); |
michael@0 | 92 | |
michael@0 | 93 | // Retrieves the most specific (smallest) descendant range encompassing |
michael@0 | 94 | // the specified address. This method will only return entries held by |
michael@0 | 95 | // child ranges, and not the entry contained by |this|. This is necessary |
michael@0 | 96 | // to support a sparsely-populated root range. If no descendant range |
michael@0 | 97 | // encompasses the address, returns false. |
michael@0 | 98 | bool RetrieveRange(const AddressType &address, EntryType *entry) const; |
michael@0 | 99 | |
michael@0 | 100 | // Removes all children. Note that Clear only removes descendants, |
michael@0 | 101 | // leaving the node on which it is called intact. Because the only |
michael@0 | 102 | // meaningful things contained by a root node are descendants, this |
michael@0 | 103 | // is sufficient to restore an entire ContainedRangeMap to its initial |
michael@0 | 104 | // empty state when called on the root node. |
michael@0 | 105 | void Clear(); |
michael@0 | 106 | |
michael@0 | 107 | private: |
michael@0 | 108 | friend class ContainedRangeMapSerializer<AddressType, EntryType>; |
michael@0 | 109 | friend class ModuleComparer; |
michael@0 | 110 | |
michael@0 | 111 | // AddressToRangeMap stores pointers. This makes reparenting simpler in |
michael@0 | 112 | // StoreRange, because it doesn't need to copy entire objects. |
michael@0 | 113 | typedef std::map<AddressType, ContainedRangeMap *> AddressToRangeMap; |
michael@0 | 114 | typedef typename AddressToRangeMap::const_iterator MapConstIterator; |
michael@0 | 115 | typedef typename AddressToRangeMap::iterator MapIterator; |
michael@0 | 116 | typedef typename AddressToRangeMap::value_type MapValue; |
michael@0 | 117 | |
michael@0 | 118 | // Creates a new ContainedRangeMap with the specified base address, entry, |
michael@0 | 119 | // and initial child map, which may be NULL. This is only used internally |
michael@0 | 120 | // by ContainedRangeMap when it creates a new child. |
michael@0 | 121 | ContainedRangeMap(const AddressType &base, const EntryType &entry, |
michael@0 | 122 | AddressToRangeMap *map) |
michael@0 | 123 | : base_(base), entry_(entry), map_(map) {} |
michael@0 | 124 | |
michael@0 | 125 | // The base address of this range. The high address does not need to |
michael@0 | 126 | // be stored, because it is used as the key to an object in its parent's |
michael@0 | 127 | // map, and all ContainedRangeMaps except for the root range are contained |
michael@0 | 128 | // within maps. The root range does not actually contain an entry, so its |
michael@0 | 129 | // base_ field is meaningless, and the fact that it has no parent and thus |
michael@0 | 130 | // no key is unimportant. For this reason, the base_ field should only be |
michael@0 | 131 | // is accessed on child ContainedRangeMap objects, and never on |this|. |
michael@0 | 132 | const AddressType base_; |
michael@0 | 133 | |
michael@0 | 134 | // The entry corresponding to this range. The root range does not |
michael@0 | 135 | // actually contain an entry, so its entry_ field is meaningless. For |
michael@0 | 136 | // this reason, the entry_ field should only be accessed on child |
michael@0 | 137 | // ContainedRangeMap objects, and never on |this|. |
michael@0 | 138 | const EntryType entry_; |
michael@0 | 139 | |
michael@0 | 140 | // The map containing child ranges, keyed by each child range's high |
michael@0 | 141 | // address. This is a pointer to avoid allocating map structures for |
michael@0 | 142 | // leaf nodes, where they are not needed. |
michael@0 | 143 | AddressToRangeMap *map_; |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | |
michael@0 | 147 | } // namespace google_breakpad |
michael@0 | 148 | |
michael@0 | 149 | |
michael@0 | 150 | #endif // PROCESSOR_CONTAINED_RANGE_MAP_H__ |