1378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// Copyright 2010 Google Inc. All Rights Reserved.
2378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
3378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// Redistribution and use in source and binary forms, with or without
4378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// modification, are permitted provided that the following conditions are
5378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// met:
6378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
7378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//     * Redistributions of source code must retain the above copyright
8378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// notice, this list of conditions and the following disclaimer.
9378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//     * Redistributions in binary form must reproduce the above
10378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// copyright notice, this list of conditions and the following disclaimer
11378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// in the documentation and/or other materials provided with the
12378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// distribution.
13378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//     * Neither the name of Google Inc. nor the names of its
14378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// contributors may be used to endorse or promote products derived from
15378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// this software without specific prior written permission.
16378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
17378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
29378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// static_map.h: StaticMap.
30378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
31378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// StaticMap provides lookup interfaces and iterators similar as stl::map's.
32378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// These lookup operations are purely Read-Only, thus memory
33378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// allocation & deallocation is mostly avoided (intentionally).
34378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
35378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// The chunk of memory should contain data with pre-defined pattern:
36378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// **************** header ***************
37378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// uint32 (4 bytes): number of nodes
38378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// uint32 (4 bytes): address offset of node1's mapped_value
39378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// uint32 (4 bytes): address offset of node2's mapped_value
40378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// ...
41378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// uint32 (4 bytes): address offset of nodeN's mapped_value
42378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
43378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// ************* Key array ************
44378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (X bytes): node1's key
45378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (X bytes): node2's key
46378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// ...
47378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (X bytes): nodeN's key
48378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
49378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// ************* Value array **********
50378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (? bytes): node1's mapped_value
51378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (? bytes): node2's mapped_value
52378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// ...
53378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// (? bytes): nodeN's mapped_value
54378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
55378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// REQUIREMENT: Key type MUST be primitive type or pointers so that:
56378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// X = sizeof(typename Key);
57378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com//
58378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// Note: since address offset is stored as uint32, user should keep in mind that
59378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// StaticMap only supports up to 4GB size of memory data.
60378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
61378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// Author: Siyang Xie (lambxsy@google.com)
62378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
63378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
64378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com#ifndef PROCESSOR_STATIC_MAP_H__
65378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com#define PROCESSOR_STATIC_MAP_H__
66378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
67378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com#include "processor/static_map_iterator-inl.h"
68378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
69378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.comnamespace google_breakpad {
70378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
71378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com// Default functor to compare keys.
72378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.comtemplate<typename Key>
73378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.comclass DefaultCompare {
74378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com public:
75378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  int operator()(const Key &k1, const Key &k2) const {
76378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com    if (k1 < k2) return -1;
77378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com    if (k1 == k2) return 0;
78378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com    return 1;
79378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  }
80378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com};
81378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
82378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.comtemplate<typename Key, typename Value, typename Compare = DefaultCompare<Key> >
83378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.comclass StaticMap {
84378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com public:
85378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  typedef StaticMapIterator<Key, Value, Compare> iterator;
86378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  typedef StaticMapIterator<Key, Value, Compare> const_iterator;
87378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
88378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  StaticMap() : raw_data_(0),
89378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com                num_nodes_(0),
90378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com                offsets_(0),
91378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com                compare_() { }
92378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
93378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  explicit StaticMap(const char* raw_data);
94378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
95378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  inline bool empty() const { return num_nodes_ == 0; }
96378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  inline unsigned int size() const { return num_nodes_; }
97378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
98378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // Return iterators.
99378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  inline iterator begin() const { return IteratorAtIndex(0); }
100378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  inline iterator last() const { return IteratorAtIndex(num_nodes_ - 1); }
101378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  inline iterator end() const { return IteratorAtIndex(num_nodes_); }
102378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  inline iterator IteratorAtIndex(int index) const {
103378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com    return iterator(raw_data_, index);
104378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  }
105378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
106378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // Lookup operations.
107378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  iterator find(const Key &k) const;
108378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
109378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // lower_bound(k) searches in a sorted range for the first element that has a
110378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // key not less than the argument k.
111378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  iterator lower_bound(const Key &k) const;
112378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
113378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // upper_bound(k) searches in a sorted range for the first element that has a
114378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // key greater than the argument k.
115378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  iterator upper_bound(const Key &k) const;
116378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
117378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // Checks if the underlying memory data conforms to the predefined pattern:
118378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // first check the number of nodes is non-negative,
119378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // then check both offsets and keys are strictly increasing (sorted).
120378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  bool ValidateInMemoryStructure() const;
121378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
122378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com private:
123378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  const Key GetKeyAtIndex(int i) const;
124378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
125378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // Start address of a raw memory chunk with serialized data.
126378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  const char* raw_data_;
127378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
128378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // Number of nodes in the static map.
1292971a050754f48aaa807db47a29e0d0beddbdcf7ivan.penkov@gmail.com  int32_t num_nodes_;
130378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
131378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // Array of offset addresses for stored values.
132378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // For example:
133378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // address_of_i-th_node_value = raw_data_ + offsets_[i]
1346162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.com  const uint32_t* offsets_;
135378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
136378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  // keys_[i] = key of i_th node
137378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  const Key* keys_;
138378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
139378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com  Compare compare_;
140378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com};
141378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
142378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com}  // namespace google_breakpad
143378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com
144378e28e30147a6baba275ecce3c8fda06cfd5849SiyangXie@gmail.com#endif  // PROCESSOR_STATIC_MAP_H__
145