1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/map_serializers.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +// Copyright (c) 2010, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// map_serializers.h: defines templates for serializing std::map and its 1.34 +// wrappers: AddressMap, RangeMap, and ContainedRangeMap. 1.35 +// 1.36 +// Author: Siyang Xie (lambxsy@google.com) 1.37 + 1.38 + 1.39 +#ifndef PROCESSOR_MAP_SERIALIZERS_H__ 1.40 +#define PROCESSOR_MAP_SERIALIZERS_H__ 1.41 + 1.42 +#include <map> 1.43 +#include <string> 1.44 + 1.45 +#include "processor/simple_serializer.h" 1.46 + 1.47 +#include "processor/address_map-inl.h" 1.48 +#include "processor/range_map-inl.h" 1.49 +#include "processor/contained_range_map-inl.h" 1.50 + 1.51 +namespace google_breakpad { 1.52 + 1.53 +// StdMapSerializer allocates memory and serializes an std::map instance into a 1.54 +// chunk of memory data. 1.55 +template<typename Key, typename Value> 1.56 +class StdMapSerializer { 1.57 + public: 1.58 + // Calculate the memory size of serialized data. 1.59 + size_t SizeOf(const std::map<Key, Value> &m) const; 1.60 + 1.61 + // Writes the serialized data to memory with start address = dest, 1.62 + // and returns the "end" of data, i.e., return the address follow the final 1.63 + // byte of data. 1.64 + // NOTE: caller has to allocate enough memory before invoke Write() method. 1.65 + char* Write(const std::map<Key, Value> &m, char* dest) const; 1.66 + 1.67 + // Serializes a std::map object into a chunk of memory data with format 1.68 + // described in "StaticMap.h" comment. 1.69 + // Returns a pointer to the serialized data. If size != NULL, *size is set 1.70 + // to the size of serialized data, i.e., SizeOf(m). 1.71 + // Caller has the ownership of memory allocated as "new char[]". 1.72 + char* Serialize(const std::map<Key, Value> &m, unsigned int *size) const; 1.73 + 1.74 + private: 1.75 + SimpleSerializer<Key> key_serializer_; 1.76 + SimpleSerializer<Value> value_serializer_; 1.77 +}; 1.78 + 1.79 +// AddressMapSerializer allocates memory and serializes an AddressMap into a 1.80 +// chunk of memory data. 1.81 +template<typename Addr, typename Entry> 1.82 +class AddressMapSerializer { 1.83 + public: 1.84 + // Calculate the memory size of serialized data. 1.85 + size_t SizeOf(const AddressMap<Addr, Entry> &m) const { 1.86 + return std_map_serializer_.SizeOf(m.map_); 1.87 + } 1.88 + 1.89 + // Write the serialized data to specified memory location. Return the "end" 1.90 + // of data, i.e., return the address after the final byte of data. 1.91 + // NOTE: caller has to allocate enough memory before invoke Write() method. 1.92 + char* Write(const AddressMap<Addr, Entry> &m, char *dest) const { 1.93 + return std_map_serializer_.Write(m.map_, dest); 1.94 + } 1.95 + 1.96 + // Serializes an AddressMap object into a chunk of memory data. 1.97 + // Returns a pointer to the serialized data. If size != NULL, *size is set 1.98 + // to the size of serialized data, i.e., SizeOf(m). 1.99 + // Caller has the ownership of memory allocated as "new char[]". 1.100 + char* Serialize(const AddressMap<Addr, Entry> &m, unsigned int *size) const { 1.101 + return std_map_serializer_.Serialize(m.map_, size); 1.102 + } 1.103 + 1.104 + private: 1.105 + // AddressMapSerializer is a simple wrapper of StdMapSerializer, just as 1.106 + // AddressMap is a simple wrapper of std::map. 1.107 + StdMapSerializer<Addr, Entry> std_map_serializer_; 1.108 +}; 1.109 + 1.110 +// RangeMapSerializer allocates memory and serializes a RangeMap instance into a 1.111 +// chunk of memory data. 1.112 +template<typename Address, typename Entry> 1.113 +class RangeMapSerializer { 1.114 + public: 1.115 + // Calculate the memory size of serialized data. 1.116 + size_t SizeOf(const RangeMap<Address, Entry> &m) const; 1.117 + 1.118 + // Write the serialized data to specified memory location. Return the "end" 1.119 + // of data, i.e., return the address after the final byte of data. 1.120 + // NOTE: caller has to allocate enough memory before invoke Write() method. 1.121 + char* Write(const RangeMap<Address, Entry> &m, char* dest) const; 1.122 + 1.123 + // Serializes a RangeMap object into a chunk of memory data. 1.124 + // Returns a pointer to the serialized data. If size != NULL, *size is set 1.125 + // to the size of serialized data, i.e., SizeOf(m). 1.126 + // Caller has the ownership of memory allocated as "new char[]". 1.127 + char* Serialize(const RangeMap<Address, Entry> &m, unsigned int *size) const; 1.128 + 1.129 + private: 1.130 + // Convenient type name for Range. 1.131 + typedef typename RangeMap<Address, Entry>::Range Range; 1.132 + 1.133 + // Serializer for RangeMap's key and Range::base_. 1.134 + SimpleSerializer<Address> address_serializer_; 1.135 + // Serializer for RangeMap::Range::entry_. 1.136 + SimpleSerializer<Entry> entry_serializer_; 1.137 +}; 1.138 + 1.139 +// ContainedRangeMapSerializer allocates memory and serializes a 1.140 +// ContainedRangeMap instance into a chunk of memory data. 1.141 +template<class AddrType, class EntryType> 1.142 +class ContainedRangeMapSerializer { 1.143 + public: 1.144 + // Calculate the memory size of serialized data. 1.145 + size_t SizeOf(const ContainedRangeMap<AddrType, EntryType> *m) const; 1.146 + 1.147 + // Write the serialized data to specified memory location. Return the "end" 1.148 + // of data, i.e., return the address after the final byte of data. 1.149 + // NOTE: caller has to allocate enough memory before invoke Write() method. 1.150 + char* Write(const ContainedRangeMap<AddrType, EntryType> *m, 1.151 + char* dest) const; 1.152 + 1.153 + // Serializes a ContainedRangeMap object into a chunk of memory data. 1.154 + // Returns a pointer to the serialized data. If size != NULL, *size is set 1.155 + // to the size of serialized data, i.e., SizeOf(m). 1.156 + // Caller has the ownership of memory allocated as "new char[]". 1.157 + char* Serialize(const ContainedRangeMap<AddrType, EntryType> *m, 1.158 + unsigned int *size) const; 1.159 + 1.160 + private: 1.161 + // Convenient type name for the underlying map type. 1.162 + typedef std::map<AddrType, ContainedRangeMap<AddrType, EntryType>*> Map; 1.163 + 1.164 + // Serializer for addresses and entries stored in ContainedRangeMap. 1.165 + SimpleSerializer<AddrType> addr_serializer_; 1.166 + SimpleSerializer<EntryType> entry_serializer_; 1.167 +}; 1.168 + 1.169 +} // namespace google_breakpad 1.170 + 1.171 +#endif // PROCESSOR_MAP_SERIALIZERS_H__