1 // Copyright 2010 Google LLC 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 LLC 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 // 29 // map_serializers.h: defines templates for serializing std::map and its 30 // wrappers: AddressMap, RangeMap, and ContainedRangeMap. 31 // 32 // Author: Siyang Xie ([email protected]) 33 34 35 #ifndef PROCESSOR_MAP_SERIALIZERS_H__ 36 #define PROCESSOR_MAP_SERIALIZERS_H__ 37 38 #include <map> 39 #include <string> 40 41 #include "processor/simple_serializer.h" 42 43 #include "processor/address_map-inl.h" 44 #include "processor/range_map-inl.h" 45 #include "processor/contained_range_map-inl.h" 46 47 namespace google_breakpad { 48 49 // StdMapSerializer allocates memory and serializes an std::map instance into a 50 // chunk of memory data. 51 template<typename Key, typename Value> 52 class StdMapSerializer { 53 public: 54 // Calculate the memory size of serialized data. 55 size_t SizeOf(const std::map<Key, Value>& m) const; 56 57 // Writes the serialized data to memory with start address = dest, 58 // and returns the "end" of data, i.e., return the address follow the final 59 // byte of data. 60 // NOTE: caller has to allocate enough memory before invoke Write() method. 61 char* Write(const std::map<Key, Value>& m, char* dest) const; 62 63 // Serializes a std::map object into a chunk of memory data with format 64 // described in "StaticMap.h" comment. 65 // Returns a pointer to the serialized data. If size != NULL, *size is set 66 // to the size of serialized data, i.e., SizeOf(m). 67 // Caller has the ownership of memory allocated as "new char[]". 68 char* Serialize(const std::map<Key, Value>& m, unsigned int* size) const; 69 70 private: 71 SimpleSerializer<Key> key_serializer_; 72 SimpleSerializer<Value> value_serializer_; 73 }; 74 75 // AddressMapSerializer allocates memory and serializes an AddressMap into a 76 // chunk of memory data. 77 template<typename Addr, typename Entry> 78 class AddressMapSerializer { 79 public: 80 // Calculate the memory size of serialized data. SizeOf(const AddressMap<Addr,Entry> & m)81 size_t SizeOf(const AddressMap<Addr, Entry>& m) const { 82 return std_map_serializer_.SizeOf(m.map_); 83 } 84 85 // Write the serialized data to specified memory location. Return the "end" 86 // of data, i.e., return the address after the final byte of data. 87 // NOTE: caller has to allocate enough memory before invoke Write() method. Write(const AddressMap<Addr,Entry> & m,char * dest)88 char* Write(const AddressMap<Addr, Entry>& m, char* dest) const { 89 return std_map_serializer_.Write(m.map_, dest); 90 } 91 92 // Serializes an AddressMap object into a chunk of memory data. 93 // Returns a pointer to the serialized data. If size != NULL, *size is set 94 // to the size of serialized data, i.e., SizeOf(m). 95 // Caller has the ownership of memory allocated as "new char[]". Serialize(const AddressMap<Addr,Entry> & m,unsigned int * size)96 char* Serialize(const AddressMap<Addr, Entry>& m, unsigned int* size) const { 97 return std_map_serializer_.Serialize(m.map_, size); 98 } 99 100 private: 101 // AddressMapSerializer is a simple wrapper of StdMapSerializer, just as 102 // AddressMap is a simple wrapper of std::map. 103 StdMapSerializer<Addr, Entry> std_map_serializer_; 104 }; 105 106 // RangeMapSerializer allocates memory and serializes a RangeMap instance into a 107 // chunk of memory data. 108 template<typename Address, typename Entry> 109 class RangeMapSerializer { 110 public: 111 // Calculate the memory size of serialized data. 112 size_t SizeOf(const RangeMap<Address, Entry>& m) const; 113 114 // Write the serialized data to specified memory location. Return the "end" 115 // of data, i.e., return the address after the final byte of data. 116 // NOTE: caller has to allocate enough memory before invoke Write() method. 117 char* Write(const RangeMap<Address, Entry>& m, char* dest) const; 118 119 // Serializes a RangeMap object into a chunk of memory data. 120 // Returns a pointer to the serialized data. If size != NULL, *size is set 121 // to the size of serialized data, i.e., SizeOf(m). 122 // Caller has the ownership of memory allocated as "new char[]". 123 char* Serialize(const RangeMap<Address, Entry>& m, unsigned int* size) const; 124 125 private: 126 // Convenient type name for Range. 127 typedef typename RangeMap<Address, Entry>::Range Range; 128 129 // Serializer for RangeMap's key and Range::base_. 130 SimpleSerializer<Address> address_serializer_; 131 // Serializer for RangeMap::Range::entry_. 132 SimpleSerializer<Entry> entry_serializer_; 133 }; 134 135 // ContainedRangeMapSerializer allocates memory and serializes a 136 // ContainedRangeMap instance into a chunk of memory data. 137 template<class AddrType, class EntryType> 138 class ContainedRangeMapSerializer { 139 public: 140 // Calculate the memory size of serialized data. 141 size_t SizeOf(const ContainedRangeMap<AddrType, EntryType>* m) const; 142 143 // Write the serialized data to specified memory location. Return the "end" 144 // of data, i.e., return the address after the final byte of data. 145 // NOTE: caller has to allocate enough memory before invoke Write() method. 146 char* Write(const ContainedRangeMap<AddrType, EntryType>* m, 147 char* dest) const; 148 149 // Serializes a ContainedRangeMap object into a chunk of memory data. 150 // Returns a pointer to the serialized data. If size != NULL, *size is set 151 // to the size of serialized data, i.e., SizeOf(m). 152 // Caller has the ownership of memory allocated as "new char[]". 153 char* Serialize(const ContainedRangeMap<AddrType, EntryType>* m, 154 unsigned int* size) const; 155 156 private: 157 // Convenient type name for the underlying map type. 158 typedef std::map<AddrType, ContainedRangeMap<AddrType, EntryType>*> Map; 159 160 // Serializer for addresses and entries stored in ContainedRangeMap. 161 SimpleSerializer<AddrType> addr_serializer_; 162 SimpleSerializer<EntryType> entry_serializer_; 163 }; 164 165 } // namespace google_breakpad 166 167 #endif // PROCESSOR_MAP_SERIALIZERS_H__ 168