1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/static_map-inl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,176 @@ 1.4 +// Copyright 2010 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Redistribution and use in source and binary forms, with or without 1.7 +// modification, are permitted provided that the following conditions are 1.8 +// met: 1.9 +// 1.10 +// * Redistributions of source code must retain the above copyright 1.11 +// notice, this list of conditions and the following disclaimer. 1.12 +// * Redistributions in binary form must reproduce the above 1.13 +// copyright notice, this list of conditions and the following disclaimer 1.14 +// in the documentation and/or other materials provided with the 1.15 +// distribution. 1.16 +// * Neither the name of Google Inc. nor the names of its 1.17 +// contributors may be used to endorse or promote products derived from 1.18 +// this software without specific prior written permission. 1.19 +// 1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 + 1.32 +// static_map-inl.h: StaticMap implementation. 1.33 +// 1.34 +// See static_map.h for documentation. 1.35 +// 1.36 +// Author: Siyang Xie (lambxsy@google.com) 1.37 + 1.38 + 1.39 +#ifndef PROCESSOR_STATIC_MAP_INL_H__ 1.40 +#define PROCESSOR_STATIC_MAP_INL_H__ 1.41 + 1.42 +#include "processor/static_map.h" 1.43 +#include "processor/static_map_iterator-inl.h" 1.44 +#include "common/logging.h" 1.45 + 1.46 +namespace google_breakpad { 1.47 + 1.48 +template<typename Key, typename Value, typename Compare> 1.49 +StaticMap<Key, Value, Compare>::StaticMap(const char* raw_data) 1.50 + : raw_data_(raw_data), 1.51 + compare_() { 1.52 + // First 4 Bytes store the number of nodes. 1.53 + num_nodes_ = *(reinterpret_cast<const uint32_t*>(raw_data_)); 1.54 + 1.55 + offsets_ = reinterpret_cast<const uint32_t*>( 1.56 + raw_data_ + sizeof(num_nodes_)); 1.57 + 1.58 + keys_ = reinterpret_cast<const Key*>( 1.59 + raw_data_ + (1 + num_nodes_) * sizeof(uint32_t)); 1.60 +} 1.61 + 1.62 +// find(), lower_bound() and upper_bound() implement binary search algorithm. 1.63 +template<typename Key, typename Value, typename Compare> 1.64 +StaticMapIterator<Key, Value, Compare> 1.65 +StaticMap<Key, Value, Compare>::find(const Key &key) const { 1.66 + int begin = 0; 1.67 + int end = num_nodes_; 1.68 + int middle; 1.69 + int compare_result; 1.70 + while (begin < end) { 1.71 + middle = begin + (end - begin) / 2; 1.72 + compare_result = compare_(key, GetKeyAtIndex(middle)); 1.73 + if (compare_result == 0) 1.74 + return IteratorAtIndex(middle); 1.75 + if (compare_result < 0) { 1.76 + end = middle; 1.77 + } else { 1.78 + begin = middle + 1; 1.79 + } 1.80 + } 1.81 + return this->end(); 1.82 +} 1.83 + 1.84 +template<typename Key, typename Value, typename Compare> 1.85 +StaticMapIterator<Key, Value, Compare> 1.86 +StaticMap<Key, Value, Compare>::lower_bound(const Key &key) const { 1.87 + int begin = 0; 1.88 + int end = num_nodes_; 1.89 + int middle; 1.90 + int comp_result; 1.91 + while (begin < end) { 1.92 + middle = begin + (end - begin) / 2; 1.93 + comp_result = compare_(key, GetKeyAtIndex(middle)); 1.94 + if (comp_result == 0) 1.95 + return IteratorAtIndex(middle); 1.96 + if (comp_result < 0) { 1.97 + end = middle; 1.98 + } else { 1.99 + begin = middle + 1; 1.100 + } 1.101 + } 1.102 + return IteratorAtIndex(begin); 1.103 +} 1.104 + 1.105 +template<typename Key, typename Value, typename Compare> 1.106 +StaticMapIterator<Key, Value, Compare> 1.107 +StaticMap<Key, Value, Compare>::upper_bound(const Key &key) const { 1.108 + int begin = 0; 1.109 + int end = num_nodes_; 1.110 + int middle; 1.111 + int compare_result; 1.112 + while (begin < end) { 1.113 + middle = begin + (end - begin) / 2; 1.114 + compare_result = compare_(key, GetKeyAtIndex(middle)); 1.115 + if (compare_result == 0) 1.116 + return IteratorAtIndex(middle + 1); 1.117 + if (compare_result < 0) { 1.118 + end = middle; 1.119 + } else { 1.120 + begin = middle + 1; 1.121 + } 1.122 + } 1.123 + return IteratorAtIndex(begin); 1.124 +} 1.125 + 1.126 +template<typename Key, typename Value, typename Compare> 1.127 +bool StaticMap<Key, Value, Compare>::ValidateInMemoryStructure() const { 1.128 + // check the number of nodes is non-negative: 1.129 + if (!raw_data_) return false; 1.130 + int32_t num_nodes = *(reinterpret_cast<const int32_t*>(raw_data_)); 1.131 + if (num_nodes < 0) { 1.132 + BPLOG(INFO) << "StaticMap check failed: negative number of nodes"; 1.133 + return false; 1.134 + } 1.135 + 1.136 + int node_index = 0; 1.137 + if (num_nodes_) { 1.138 + uint64_t first_offset = sizeof(int32_t) * (num_nodes_ + 1) 1.139 + + sizeof(Key) * num_nodes_; 1.140 + // Num_nodes_ is too large. 1.141 + if (first_offset > 0xffffffffUL) { 1.142 + BPLOG(INFO) << "StaticMap check failed: size exceeds limit"; 1.143 + return false; 1.144 + } 1.145 + if (offsets_[node_index] != static_cast<uint32_t>(first_offset)) { 1.146 + BPLOG(INFO) << "StaticMap check failed: first node offset is incorrect"; 1.147 + return false; 1.148 + } 1.149 + } 1.150 + 1.151 + for (node_index = 1; node_index < num_nodes_; ++node_index) { 1.152 + // Check offsets[i] is strictly increasing: 1.153 + if (offsets_[node_index] <= offsets_[node_index - 1]) { 1.154 + BPLOG(INFO) << "StaticMap check failed: node offsets non-increasing"; 1.155 + return false; 1.156 + } 1.157 + // Check Key[i] is strictly increasing as no duplicate keys are allowed. 1.158 + if (compare_(GetKeyAtIndex(node_index), 1.159 + GetKeyAtIndex(node_index - 1)) <= 0) { 1.160 + BPLOG(INFO) << "StaticMap check failed: node keys non-increasing"; 1.161 + return false; 1.162 + } 1.163 + } 1.164 + return true; 1.165 +} 1.166 + 1.167 +template<typename Key, typename Value, typename Compare> 1.168 +const Key StaticMap<Key, Value, Compare>::GetKeyAtIndex(int index) const { 1.169 + if (index < 0 || index >= num_nodes_) { 1.170 + BPLOG(ERROR) << "Key index out of range error"; 1.171 + // Key type is required to be primitive type. Return 0 if index is invalid. 1.172 + return 0; 1.173 + } 1.174 + return keys_[index]; 1.175 +} 1.176 + 1.177 +} // namespace google_breakpad 1.178 + 1.179 +#endif // PROCESSOR_STATIC_MAP_INL_H__