/thirdparty/breakpad/processor/simple_serializer-inl.h

http://github.com/tomahawk-player/tomahawk · C Header · 255 lines · 186 code · 23 blank · 46 comment · 11 complexity · a37d774b62b3187c16746ef53a98624a 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. // simple_serializer-inl.h: template specializations for following types:
  31. // bool, const char *(C-string), string,
  32. // Line, Function, PublicSymbol, WindowsFrameInfo and their linked pointers.
  33. //
  34. // See simple_serializer.h for moredocumentation.
  35. //
  36. // Author: Siyang Xie (lambxsy@google.com)
  37. #ifndef PROCESSOR_SIMPLE_SERIALIZER_INL_H__
  38. #define PROCESSOR_SIMPLE_SERIALIZER_INL_H__
  39. #include <string>
  40. #include "processor/simple_serializer.h"
  41. #include "map_serializers-inl.h"
  42. #include "google_breakpad/processor/basic_source_line_resolver.h"
  43. #include "processor/basic_source_line_resolver_types.h"
  44. #include "processor/linked_ptr.h"
  45. #include "processor/windows_frame_info.h"
  46. namespace google_breakpad {
  47. // Specializations of SimpleSerializer: bool
  48. template<>
  49. class SimpleSerializer<bool> {
  50. public:
  51. static size_t SizeOf(bool boolean) { return 1; }
  52. static char *Write(bool boolean, char *dest) {
  53. *dest = static_cast<char>(boolean? 255 : 0);
  54. return ++dest;
  55. }
  56. };
  57. // Specializations of SimpleSerializer: string
  58. template<>
  59. class SimpleSerializer<string> {
  60. public:
  61. static size_t SizeOf(const string &str) { return str.size() + 1; }
  62. static char *Write(const string &str, char *dest) {
  63. strcpy(dest, str.c_str());
  64. return dest + SizeOf(str);
  65. }
  66. };
  67. // Specializations of SimpleSerializer: C-string
  68. template<>
  69. class SimpleSerializer<const char*> {
  70. public:
  71. static size_t SizeOf(const char *cstring) {
  72. return strlen(cstring) + 1;
  73. }
  74. static char *Write(const char *cstring, char *dest) {
  75. strcpy(dest, cstring);
  76. return dest + SizeOf(cstring);
  77. }
  78. };
  79. // Specializations of SimpleSerializer: Line
  80. template<>
  81. class SimpleSerializer<BasicSourceLineResolver::Line> {
  82. typedef BasicSourceLineResolver::Line Line;
  83. public:
  84. static size_t SizeOf(const Line &line) {
  85. return SimpleSerializer<MemAddr>::SizeOf(line.address)
  86. + SimpleSerializer<MemAddr>::SizeOf(line.size)
  87. + SimpleSerializer<int32_t>::SizeOf(line.source_file_id)
  88. + SimpleSerializer<int32_t>::SizeOf(line.line);
  89. }
  90. static char *Write(const Line &line, char *dest) {
  91. dest = SimpleSerializer<MemAddr>::Write(line.address, dest);
  92. dest = SimpleSerializer<MemAddr>::Write(line.size, dest);
  93. dest = SimpleSerializer<int32_t>::Write(line.source_file_id, dest);
  94. dest = SimpleSerializer<int32_t>::Write(line.line, dest);
  95. return dest;
  96. }
  97. };
  98. // Specializations of SimpleSerializer: PublicSymbol
  99. template<>
  100. class SimpleSerializer<BasicSourceLineResolver::PublicSymbol> {
  101. typedef BasicSourceLineResolver::PublicSymbol PublicSymbol;
  102. public:
  103. static size_t SizeOf(const PublicSymbol &pubsymbol) {
  104. return SimpleSerializer<string>::SizeOf(pubsymbol.name)
  105. + SimpleSerializer<MemAddr>::SizeOf(pubsymbol.address)
  106. + SimpleSerializer<int32_t>::SizeOf(pubsymbol.parameter_size);
  107. }
  108. static char *Write(const PublicSymbol &pubsymbol, char *dest) {
  109. dest = SimpleSerializer<string>::Write(pubsymbol.name, dest);
  110. dest = SimpleSerializer<MemAddr>::Write(pubsymbol.address, dest);
  111. dest = SimpleSerializer<int32_t>::Write(pubsymbol.parameter_size, dest);
  112. return dest;
  113. }
  114. };
  115. // Specializations of SimpleSerializer: WindowsFrameInfo
  116. template<>
  117. class SimpleSerializer<WindowsFrameInfo> {
  118. public:
  119. static size_t SizeOf(const WindowsFrameInfo &wfi) {
  120. unsigned int size = 0;
  121. size += sizeof(int32_t); // wfi.type_
  122. size += SimpleSerializer<int32_t>::SizeOf(wfi.valid);
  123. size += SimpleSerializer<u_int32_t>::SizeOf(wfi.prolog_size);
  124. size += SimpleSerializer<u_int32_t>::SizeOf(wfi.epilog_size);
  125. size += SimpleSerializer<u_int32_t>::SizeOf(wfi.parameter_size);
  126. size += SimpleSerializer<u_int32_t>::SizeOf(wfi.saved_register_size);
  127. size += SimpleSerializer<u_int32_t>::SizeOf(wfi.local_size);
  128. size += SimpleSerializer<u_int32_t>::SizeOf(wfi.max_stack_size);
  129. size += SimpleSerializer<bool>::SizeOf(wfi.allocates_base_pointer);
  130. size += SimpleSerializer<string>::SizeOf(wfi.program_string);
  131. return size;
  132. }
  133. static char *Write(const WindowsFrameInfo &wfi, char *dest) {
  134. dest = SimpleSerializer<int32_t>::Write(
  135. static_cast<const int32_t>(wfi.type_), dest);
  136. dest = SimpleSerializer<int32_t>::Write(wfi.valid, dest);
  137. dest = SimpleSerializer<u_int32_t>::Write(wfi.prolog_size, dest);
  138. dest = SimpleSerializer<u_int32_t>::Write(wfi.epilog_size, dest);
  139. dest = SimpleSerializer<u_int32_t>::Write(wfi.parameter_size, dest);
  140. dest = SimpleSerializer<u_int32_t>::Write(wfi.saved_register_size, dest);
  141. dest = SimpleSerializer<u_int32_t>::Write(wfi.local_size, dest);
  142. dest = SimpleSerializer<u_int32_t>::Write(wfi.max_stack_size, dest);
  143. dest = SimpleSerializer<bool>::Write(wfi.allocates_base_pointer, dest);
  144. return SimpleSerializer<string>::Write(wfi.program_string, dest);
  145. }
  146. };
  147. // Specializations of SimpleSerializer: Linked_ptr version of
  148. // Line, Function, PublicSymbol, WindowsFrameInfo.
  149. template<>
  150. class SimpleSerializer< linked_ptr<BasicSourceLineResolver::Line> > {
  151. typedef BasicSourceLineResolver::Line Line;
  152. public:
  153. static size_t SizeOf(const linked_ptr<Line> &lineptr) {
  154. if (lineptr.get() == NULL) return 0;
  155. return SimpleSerializer<Line>::SizeOf(*(lineptr.get()));
  156. }
  157. static char *Write(const linked_ptr<Line> &lineptr, char *dest) {
  158. if (lineptr.get())
  159. dest = SimpleSerializer<Line>::Write(*(lineptr.get()), dest);
  160. return dest;
  161. }
  162. };
  163. template<>
  164. class SimpleSerializer<BasicSourceLineResolver::Function> {
  165. // Convenient type names.
  166. typedef BasicSourceLineResolver::Function Function;
  167. typedef BasicSourceLineResolver::Line Line;
  168. public:
  169. static size_t SizeOf(const Function &func) {
  170. unsigned int size = 0;
  171. size += SimpleSerializer<string>::SizeOf(func.name);
  172. size += SimpleSerializer<MemAddr>::SizeOf(func.address);
  173. size += SimpleSerializer<MemAddr>::SizeOf(func.size);
  174. size += SimpleSerializer<int32_t>::SizeOf(func.parameter_size);
  175. size += range_map_serializer_.SizeOf(func.lines);
  176. return size;
  177. }
  178. static char *Write(const Function &func, char *dest) {
  179. dest = SimpleSerializer<string>::Write(func.name, dest);
  180. dest = SimpleSerializer<MemAddr>::Write(func.address, dest);
  181. dest = SimpleSerializer<MemAddr>::Write(func.size, dest);
  182. dest = SimpleSerializer<int32_t>::Write(func.parameter_size, dest);
  183. dest = range_map_serializer_.Write(func.lines, dest);
  184. return dest;
  185. }
  186. private:
  187. // This static member is defined in module_serializer.cc.
  188. static RangeMapSerializer< MemAddr, linked_ptr<Line> > range_map_serializer_;
  189. };
  190. template<>
  191. class SimpleSerializer< linked_ptr<BasicSourceLineResolver::Function> > {
  192. typedef BasicSourceLineResolver::Function Function;
  193. public:
  194. static size_t SizeOf(const linked_ptr<Function> &func) {
  195. if (!func.get()) return 0;
  196. return SimpleSerializer<Function>::SizeOf(*(func.get()));
  197. }
  198. static char *Write(const linked_ptr<Function> &func, char *dest) {
  199. if (func.get())
  200. dest = SimpleSerializer<Function>::Write(*(func.get()), dest);
  201. return dest;
  202. }
  203. };
  204. template<>
  205. class SimpleSerializer< linked_ptr<BasicSourceLineResolver::PublicSymbol> > {
  206. typedef BasicSourceLineResolver::PublicSymbol PublicSymbol;
  207. public:
  208. static size_t SizeOf(const linked_ptr<PublicSymbol> &pubsymbol) {
  209. if (pubsymbol.get() == NULL) return 0;
  210. return SimpleSerializer<PublicSymbol>::SizeOf(*(pubsymbol.get()));
  211. }
  212. static char *Write(const linked_ptr<PublicSymbol> &pubsymbol, char *dest) {
  213. if (pubsymbol.get())
  214. dest = SimpleSerializer<PublicSymbol>::Write(*(pubsymbol.get()), dest);
  215. return dest;
  216. }
  217. };
  218. template<>
  219. class SimpleSerializer< linked_ptr<WindowsFrameInfo> > {
  220. public:
  221. static size_t SizeOf(const linked_ptr<WindowsFrameInfo> &wfi) {
  222. if (wfi.get() == NULL) return 0;
  223. return SimpleSerializer<WindowsFrameInfo>::SizeOf(*(wfi.get()));
  224. }
  225. static char *Write(const linked_ptr<WindowsFrameInfo> &wfi, char *dest) {
  226. if (wfi.get())
  227. dest = SimpleSerializer<WindowsFrameInfo>::Write(*(wfi.get()), dest);
  228. return dest;
  229. }
  230. };
  231. } // namespace google_breakpad
  232. #endif // PROCESSOR_SIMPLE_SERIALIZER_INL_H__