michael@0: // Copyright 2010 Google Inc. All Rights Reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // static_map.h: StaticMap. michael@0: // michael@0: // StaticMap provides lookup interfaces and iterators similar as stl::map's. michael@0: // These lookup operations are purely Read-Only, thus memory michael@0: // allocation & deallocation is mostly avoided (intentionally). michael@0: // michael@0: // The chunk of memory should contain data with pre-defined pattern: michael@0: // **************** header *************** michael@0: // uint32 (4 bytes): number of nodes michael@0: // uint32 (4 bytes): address offset of node1's mapped_value michael@0: // uint32 (4 bytes): address offset of node2's mapped_value michael@0: // ... michael@0: // uint32 (4 bytes): address offset of nodeN's mapped_value michael@0: // michael@0: // ************* Key array ************ michael@0: // (X bytes): node1's key michael@0: // (X bytes): node2's key michael@0: // ... michael@0: // (X bytes): nodeN's key michael@0: // michael@0: // ************* Value array ********** michael@0: // (? bytes): node1's mapped_value michael@0: // (? bytes): node2's mapped_value michael@0: // ... michael@0: // (? bytes): nodeN's mapped_value michael@0: // michael@0: // REQUIREMENT: Key type MUST be primitive type or pointers so that: michael@0: // X = sizeof(typename Key); michael@0: // michael@0: // Note: since address offset is stored as uint32, user should keep in mind that michael@0: // StaticMap only supports up to 4GB size of memory data. michael@0: michael@0: // Author: Siyang Xie (lambxsy@google.com) michael@0: michael@0: michael@0: #ifndef PROCESSOR_STATIC_MAP_H__ michael@0: #define PROCESSOR_STATIC_MAP_H__ michael@0: michael@0: #include "processor/static_map_iterator-inl.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // Default functor to compare keys. michael@0: template michael@0: class DefaultCompare { michael@0: public: michael@0: int operator()(const Key &k1, const Key &k2) const { michael@0: if (k1 < k2) return -1; michael@0: if (k1 == k2) return 0; michael@0: return 1; michael@0: } michael@0: }; michael@0: michael@0: template > michael@0: class StaticMap { michael@0: public: michael@0: typedef StaticMapIterator iterator; michael@0: typedef StaticMapIterator const_iterator; michael@0: michael@0: StaticMap() : raw_data_(0), michael@0: num_nodes_(0), michael@0: offsets_(0), michael@0: compare_() { } michael@0: michael@0: explicit StaticMap(const char* raw_data); michael@0: michael@0: inline bool empty() const { return num_nodes_ == 0; } michael@0: inline unsigned int size() const { return num_nodes_; } michael@0: michael@0: // Return iterators. michael@0: inline iterator begin() const { return IteratorAtIndex(0); } michael@0: inline iterator last() const { return IteratorAtIndex(num_nodes_ - 1); } michael@0: inline iterator end() const { return IteratorAtIndex(num_nodes_); } michael@0: inline iterator IteratorAtIndex(int index) const { michael@0: return iterator(raw_data_, index); michael@0: } michael@0: michael@0: // Lookup operations. michael@0: iterator find(const Key &k) const; michael@0: michael@0: // lower_bound(k) searches in a sorted range for the first element that has a michael@0: // key not less than the argument k. michael@0: iterator lower_bound(const Key &k) const; michael@0: michael@0: // upper_bound(k) searches in a sorted range for the first element that has a michael@0: // key greater than the argument k. michael@0: iterator upper_bound(const Key &k) const; michael@0: michael@0: // Checks if the underlying memory data conforms to the predefined pattern: michael@0: // first check the number of nodes is non-negative, michael@0: // then check both offsets and keys are strictly increasing (sorted). michael@0: bool ValidateInMemoryStructure() const; michael@0: michael@0: private: michael@0: const Key GetKeyAtIndex(int i) const; michael@0: michael@0: // Start address of a raw memory chunk with serialized data. michael@0: const char* raw_data_; michael@0: michael@0: // Number of nodes in the static map. michael@0: int32_t num_nodes_; michael@0: michael@0: // Array of offset addresses for stored values. michael@0: // For example: michael@0: // address_of_i-th_node_value = raw_data_ + offsets_[i] michael@0: const uint32_t* offsets_; michael@0: michael@0: // keys_[i] = key of i_th node michael@0: const Key* keys_; michael@0: michael@0: Compare compare_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // PROCESSOR_STATIC_MAP_H__