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 2010 Google Inc. All Rights Reserved. |
michael@0 | 2 | // |
michael@0 | 3 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 4 | // modification, are permitted provided that the following conditions are |
michael@0 | 5 | // met: |
michael@0 | 6 | // |
michael@0 | 7 | // * Redistributions of source code must retain the above copyright |
michael@0 | 8 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | // * Redistributions in binary form must reproduce the above |
michael@0 | 10 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 11 | // in the documentation and/or other materials provided with the |
michael@0 | 12 | // distribution. |
michael@0 | 13 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 14 | // contributors may be used to endorse or promote products derived from |
michael@0 | 15 | // this software without specific prior written permission. |
michael@0 | 16 | // |
michael@0 | 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 28 | |
michael@0 | 29 | // static_map.h: StaticMap. |
michael@0 | 30 | // |
michael@0 | 31 | // StaticMap provides lookup interfaces and iterators similar as stl::map's. |
michael@0 | 32 | // These lookup operations are purely Read-Only, thus memory |
michael@0 | 33 | // allocation & deallocation is mostly avoided (intentionally). |
michael@0 | 34 | // |
michael@0 | 35 | // The chunk of memory should contain data with pre-defined pattern: |
michael@0 | 36 | // **************** header *************** |
michael@0 | 37 | // uint32 (4 bytes): number of nodes |
michael@0 | 38 | // uint32 (4 bytes): address offset of node1's mapped_value |
michael@0 | 39 | // uint32 (4 bytes): address offset of node2's mapped_value |
michael@0 | 40 | // ... |
michael@0 | 41 | // uint32 (4 bytes): address offset of nodeN's mapped_value |
michael@0 | 42 | // |
michael@0 | 43 | // ************* Key array ************ |
michael@0 | 44 | // (X bytes): node1's key |
michael@0 | 45 | // (X bytes): node2's key |
michael@0 | 46 | // ... |
michael@0 | 47 | // (X bytes): nodeN's key |
michael@0 | 48 | // |
michael@0 | 49 | // ************* Value array ********** |
michael@0 | 50 | // (? bytes): node1's mapped_value |
michael@0 | 51 | // (? bytes): node2's mapped_value |
michael@0 | 52 | // ... |
michael@0 | 53 | // (? bytes): nodeN's mapped_value |
michael@0 | 54 | // |
michael@0 | 55 | // REQUIREMENT: Key type MUST be primitive type or pointers so that: |
michael@0 | 56 | // X = sizeof(typename Key); |
michael@0 | 57 | // |
michael@0 | 58 | // Note: since address offset is stored as uint32, user should keep in mind that |
michael@0 | 59 | // StaticMap only supports up to 4GB size of memory data. |
michael@0 | 60 | |
michael@0 | 61 | // Author: Siyang Xie (lambxsy@google.com) |
michael@0 | 62 | |
michael@0 | 63 | |
michael@0 | 64 | #ifndef PROCESSOR_STATIC_MAP_H__ |
michael@0 | 65 | #define PROCESSOR_STATIC_MAP_H__ |
michael@0 | 66 | |
michael@0 | 67 | #include "processor/static_map_iterator-inl.h" |
michael@0 | 68 | |
michael@0 | 69 | namespace google_breakpad { |
michael@0 | 70 | |
michael@0 | 71 | // Default functor to compare keys. |
michael@0 | 72 | template<typename Key> |
michael@0 | 73 | class DefaultCompare { |
michael@0 | 74 | public: |
michael@0 | 75 | int operator()(const Key &k1, const Key &k2) const { |
michael@0 | 76 | if (k1 < k2) return -1; |
michael@0 | 77 | if (k1 == k2) return 0; |
michael@0 | 78 | return 1; |
michael@0 | 79 | } |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | template<typename Key, typename Value, typename Compare = DefaultCompare<Key> > |
michael@0 | 83 | class StaticMap { |
michael@0 | 84 | public: |
michael@0 | 85 | typedef StaticMapIterator<Key, Value, Compare> iterator; |
michael@0 | 86 | typedef StaticMapIterator<Key, Value, Compare> const_iterator; |
michael@0 | 87 | |
michael@0 | 88 | StaticMap() : raw_data_(0), |
michael@0 | 89 | num_nodes_(0), |
michael@0 | 90 | offsets_(0), |
michael@0 | 91 | compare_() { } |
michael@0 | 92 | |
michael@0 | 93 | explicit StaticMap(const char* raw_data); |
michael@0 | 94 | |
michael@0 | 95 | inline bool empty() const { return num_nodes_ == 0; } |
michael@0 | 96 | inline unsigned int size() const { return num_nodes_; } |
michael@0 | 97 | |
michael@0 | 98 | // Return iterators. |
michael@0 | 99 | inline iterator begin() const { return IteratorAtIndex(0); } |
michael@0 | 100 | inline iterator last() const { return IteratorAtIndex(num_nodes_ - 1); } |
michael@0 | 101 | inline iterator end() const { return IteratorAtIndex(num_nodes_); } |
michael@0 | 102 | inline iterator IteratorAtIndex(int index) const { |
michael@0 | 103 | return iterator(raw_data_, index); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | // Lookup operations. |
michael@0 | 107 | iterator find(const Key &k) const; |
michael@0 | 108 | |
michael@0 | 109 | // lower_bound(k) searches in a sorted range for the first element that has a |
michael@0 | 110 | // key not less than the argument k. |
michael@0 | 111 | iterator lower_bound(const Key &k) const; |
michael@0 | 112 | |
michael@0 | 113 | // upper_bound(k) searches in a sorted range for the first element that has a |
michael@0 | 114 | // key greater than the argument k. |
michael@0 | 115 | iterator upper_bound(const Key &k) const; |
michael@0 | 116 | |
michael@0 | 117 | // Checks if the underlying memory data conforms to the predefined pattern: |
michael@0 | 118 | // first check the number of nodes is non-negative, |
michael@0 | 119 | // then check both offsets and keys are strictly increasing (sorted). |
michael@0 | 120 | bool ValidateInMemoryStructure() const; |
michael@0 | 121 | |
michael@0 | 122 | private: |
michael@0 | 123 | const Key GetKeyAtIndex(int i) const; |
michael@0 | 124 | |
michael@0 | 125 | // Start address of a raw memory chunk with serialized data. |
michael@0 | 126 | const char* raw_data_; |
michael@0 | 127 | |
michael@0 | 128 | // Number of nodes in the static map. |
michael@0 | 129 | int32_t num_nodes_; |
michael@0 | 130 | |
michael@0 | 131 | // Array of offset addresses for stored values. |
michael@0 | 132 | // For example: |
michael@0 | 133 | // address_of_i-th_node_value = raw_data_ + offsets_[i] |
michael@0 | 134 | const uint32_t* offsets_; |
michael@0 | 135 | |
michael@0 | 136 | // keys_[i] = key of i_th node |
michael@0 | 137 | const Key* keys_; |
michael@0 | 138 | |
michael@0 | 139 | Compare compare_; |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | } // namespace google_breakpad |
michael@0 | 143 | |
michael@0 | 144 | #endif // PROCESSOR_STATIC_MAP_H__ |