/thirdparty/breakpad/processor/static_map.h

http://github.com/tomahawk-player/tomahawk · C Header · 144 lines · 45 code · 24 blank · 75 comment · 4 complexity · 363d3a8e5286daaab1572ac08d1e14e4 MD5 · raw file

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