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.

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

mercurial