toolkit/crashreporter/google-breakpad/src/processor/map_serializers.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial