/thirdparty/breakpad/processor/map_serializers.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 168 lines · 60 code · 26 blank · 82 comment · 0 complexity · c5c1368b46d29cc3c75fece991eb8e27 MD5 · raw file

  1. // Copyright (c) 2010, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // map_serializers.h: defines templates for serializing std::map and its
  31. // wrappers: AddressMap, RangeMap, and ContainedRangeMap.
  32. //
  33. // Author: Siyang Xie (lambxsy@google.com)
  34. #ifndef PROCESSOR_MAP_SERIALIZERS_H__
  35. #define PROCESSOR_MAP_SERIALIZERS_H__
  36. #include <map>
  37. #include <string>
  38. #include "processor/simple_serializer.h"
  39. #include "processor/address_map-inl.h"
  40. #include "processor/range_map-inl.h"
  41. #include "processor/contained_range_map-inl.h"
  42. namespace google_breakpad {
  43. // StdMapSerializer allocates memory and serializes an std::map instance into a
  44. // chunk of memory data.
  45. template<typename Key, typename Value>
  46. class StdMapSerializer {
  47. public:
  48. // Calculate the memory size of serialized data.
  49. size_t SizeOf(const std::map<Key, Value> &m) const;
  50. // Writes the serialized data to memory with start address = dest,
  51. // and returns the "end" of data, i.e., return the address follow the final
  52. // byte of data.
  53. // NOTE: caller has to allocate enough memory before invoke Write() method.
  54. char* Write(const std::map<Key, Value> &m, char* dest) const;
  55. // Serializes a std::map object into a chunk of memory data with format
  56. // described in "StaticMap.h" comment.
  57. // Returns a pointer to the serialized data. If size != NULL, *size is set
  58. // to the size of serialized data, i.e., SizeOf(m).
  59. // Caller has the ownership of memory allocated as "new char[]".
  60. char* Serialize(const std::map<Key, Value> &m, unsigned int *size) const;
  61. private:
  62. SimpleSerializer<Key> key_serializer_;
  63. SimpleSerializer<Value> value_serializer_;
  64. };
  65. // AddressMapSerializer allocates memory and serializes an AddressMap into a
  66. // chunk of memory data.
  67. template<typename Addr, typename Entry>
  68. class AddressMapSerializer {
  69. public:
  70. // Calculate the memory size of serialized data.
  71. size_t SizeOf(const AddressMap<Addr, Entry> &m) const {
  72. return std_map_serializer_.SizeOf(m.map_);
  73. }
  74. // Write the serialized data to specified memory location. Return the "end"
  75. // of data, i.e., return the address after the final byte of data.
  76. // NOTE: caller has to allocate enough memory before invoke Write() method.
  77. char* Write(const AddressMap<Addr, Entry> &m, char *dest) const {
  78. return std_map_serializer_.Write(m.map_, dest);
  79. }
  80. // Serializes an AddressMap object into a chunk of memory data.
  81. // Returns a pointer to the serialized data. If size != NULL, *size is set
  82. // to the size of serialized data, i.e., SizeOf(m).
  83. // Caller has the ownership of memory allocated as "new char[]".
  84. char* Serialize(const AddressMap<Addr, Entry> &m, unsigned int *size) const {
  85. return std_map_serializer_.Serialize(m.map_, size);
  86. }
  87. private:
  88. // AddressMapSerializer is a simple wrapper of StdMapSerializer, just as
  89. // AddressMap is a simple wrapper of std::map.
  90. StdMapSerializer<Addr, Entry> std_map_serializer_;
  91. };
  92. // RangeMapSerializer allocates memory and serializes a RangeMap instance into a
  93. // chunk of memory data.
  94. template<typename Address, typename Entry>
  95. class RangeMapSerializer {
  96. public:
  97. // Calculate the memory size of serialized data.
  98. size_t SizeOf(const RangeMap<Address, Entry> &m) const;
  99. // Write the serialized data to specified memory location. Return the "end"
  100. // of data, i.e., return the address after the final byte of data.
  101. // NOTE: caller has to allocate enough memory before invoke Write() method.
  102. char* Write(const RangeMap<Address, Entry> &m, char* dest) const;
  103. // Serializes a RangeMap object into a chunk of memory data.
  104. // Returns a pointer to the serialized data. If size != NULL, *size is set
  105. // to the size of serialized data, i.e., SizeOf(m).
  106. // Caller has the ownership of memory allocated as "new char[]".
  107. char* Serialize(const RangeMap<Address, Entry> &m, unsigned int *size) const;
  108. private:
  109. // Convenient type name for Range.
  110. typedef typename RangeMap<Address, Entry>::Range Range;
  111. // Serializer for RangeMap's key and Range::base_.
  112. SimpleSerializer<Address> address_serializer_;
  113. // Serializer for RangeMap::Range::entry_.
  114. SimpleSerializer<Entry> entry_serializer_;
  115. };
  116. // ContainedRangeMapSerializer allocates memory and serializes a
  117. // ContainedRangeMap instance into a chunk of memory data.
  118. template<class AddrType, class EntryType>
  119. class ContainedRangeMapSerializer {
  120. public:
  121. // Calculate the memory size of serialized data.
  122. size_t SizeOf(const ContainedRangeMap<AddrType, EntryType> *m) const;
  123. // Write the serialized data to specified memory location. Return the "end"
  124. // of data, i.e., return the address after the final byte of data.
  125. // NOTE: caller has to allocate enough memory before invoke Write() method.
  126. char* Write(const ContainedRangeMap<AddrType, EntryType> *m,
  127. char* dest) const;
  128. // Serializes a ContainedRangeMap object into a chunk of memory data.
  129. // Returns a pointer to the serialized data. If size != NULL, *size is set
  130. // to the size of serialized data, i.e., SizeOf(m).
  131. // Caller has the ownership of memory allocated as "new char[]".
  132. char* Serialize(const ContainedRangeMap<AddrType, EntryType> *m,
  133. unsigned int *size) const;
  134. private:
  135. // Convenient type name for the underlying map type.
  136. typedef std::map<AddrType, ContainedRangeMap<AddrType, EntryType>*> Map;
  137. // Serializer for addresses and entries stored in ContainedRangeMap.
  138. SimpleSerializer<AddrType> addr_serializer_;
  139. SimpleSerializer<EntryType> entry_serializer_;
  140. };
  141. } // namespace google_breakpad
  142. #endif // PROCESSOR_MAP_SERIALIZERS_H__