PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/thirdparty/breakpad/processor/map_serializers.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 168 lines | 60 code | 26 blank | 82 comment | 0 complexity | c5c1368b46d29cc3c75fece991eb8e27 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// 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)
 34
 35
 36#ifndef PROCESSOR_MAP_SERIALIZERS_H__
 37#define PROCESSOR_MAP_SERIALIZERS_H__
 38
 39#include <map>
 40#include <string>
 41
 42#include "processor/simple_serializer.h"
 43
 44#include "processor/address_map-inl.h"
 45#include "processor/range_map-inl.h"
 46#include "processor/contained_range_map-inl.h"
 47
 48namespace google_breakpad {
 49
 50// StdMapSerializer allocates memory and serializes an std::map instance into a
 51// chunk of memory data.
 52template<typename Key, typename Value>
 53class StdMapSerializer {
 54 public:
 55  // Calculate the memory size of serialized data.
 56  size_t SizeOf(const std::map<Key, Value> &m) const;
 57
 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;
 63
 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;
 70
 71 private:
 72  SimpleSerializer<Key> key_serializer_;
 73  SimpleSerializer<Value> value_serializer_;
 74};
 75
 76// AddressMapSerializer allocates memory and serializes an AddressMap into a
 77// chunk of memory data.
 78template<typename Addr, typename Entry>
 79class 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  }
 85
 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  }
 92
 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  }
100
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};
106
107// RangeMapSerializer allocates memory and serializes a RangeMap instance into a
108// chunk of memory data.
109template<typename Address, typename Entry>
110class RangeMapSerializer {
111 public:
112  // Calculate the memory size of serialized data.
113  size_t SizeOf(const RangeMap<Address, Entry> &m) const;
114
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;
119
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;
125
126 private:
127  // Convenient type name for Range.
128  typedef typename RangeMap<Address, Entry>::Range Range;
129
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};
135
136// ContainedRangeMapSerializer allocates memory and serializes a
137// ContainedRangeMap instance into a chunk of memory data.
138template<class AddrType, class EntryType>
139class ContainedRangeMapSerializer {
140 public:
141  // Calculate the memory size of serialized data.
142  size_t SizeOf(const ContainedRangeMap<AddrType, EntryType> *m) const;
143
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;
149
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;
156
157 private:
158  // Convenient type name for the underlying map type.
159  typedef std::map<AddrType, ContainedRangeMap<AddrType, EntryType>*> Map;
160
161  // Serializer for addresses and entries stored in ContainedRangeMap.
162  SimpleSerializer<AddrType> addr_serializer_;
163  SimpleSerializer<EntryType> entry_serializer_;
164};
165
166}  // namespace google_breakpad
167
168#endif  // PROCESSOR_MAP_SERIALIZERS_H__