michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // map_serializers.h: defines templates for serializing std::map and its michael@0: // wrappers: AddressMap, RangeMap, and ContainedRangeMap. michael@0: // michael@0: // Author: Siyang Xie (lambxsy@google.com) michael@0: michael@0: michael@0: #ifndef PROCESSOR_MAP_SERIALIZERS_H__ michael@0: #define PROCESSOR_MAP_SERIALIZERS_H__ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "processor/simple_serializer.h" michael@0: michael@0: #include "processor/address_map-inl.h" michael@0: #include "processor/range_map-inl.h" michael@0: #include "processor/contained_range_map-inl.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // StdMapSerializer allocates memory and serializes an std::map instance into a michael@0: // chunk of memory data. michael@0: template michael@0: class StdMapSerializer { michael@0: public: michael@0: // Calculate the memory size of serialized data. michael@0: size_t SizeOf(const std::map &m) const; michael@0: michael@0: // Writes the serialized data to memory with start address = dest, michael@0: // and returns the "end" of data, i.e., return the address follow the final michael@0: // byte of data. michael@0: // NOTE: caller has to allocate enough memory before invoke Write() method. michael@0: char* Write(const std::map &m, char* dest) const; michael@0: michael@0: // Serializes a std::map object into a chunk of memory data with format michael@0: // described in "StaticMap.h" comment. michael@0: // Returns a pointer to the serialized data. If size != NULL, *size is set michael@0: // to the size of serialized data, i.e., SizeOf(m). michael@0: // Caller has the ownership of memory allocated as "new char[]". michael@0: char* Serialize(const std::map &m, unsigned int *size) const; michael@0: michael@0: private: michael@0: SimpleSerializer key_serializer_; michael@0: SimpleSerializer value_serializer_; michael@0: }; michael@0: michael@0: // AddressMapSerializer allocates memory and serializes an AddressMap into a michael@0: // chunk of memory data. michael@0: template michael@0: class AddressMapSerializer { michael@0: public: michael@0: // Calculate the memory size of serialized data. michael@0: size_t SizeOf(const AddressMap &m) const { michael@0: return std_map_serializer_.SizeOf(m.map_); michael@0: } michael@0: michael@0: // Write the serialized data to specified memory location. Return the "end" michael@0: // of data, i.e., return the address after the final byte of data. michael@0: // NOTE: caller has to allocate enough memory before invoke Write() method. michael@0: char* Write(const AddressMap &m, char *dest) const { michael@0: return std_map_serializer_.Write(m.map_, dest); michael@0: } michael@0: michael@0: // Serializes an AddressMap object into a chunk of memory data. michael@0: // Returns a pointer to the serialized data. If size != NULL, *size is set michael@0: // to the size of serialized data, i.e., SizeOf(m). michael@0: // Caller has the ownership of memory allocated as "new char[]". michael@0: char* Serialize(const AddressMap &m, unsigned int *size) const { michael@0: return std_map_serializer_.Serialize(m.map_, size); michael@0: } michael@0: michael@0: private: michael@0: // AddressMapSerializer is a simple wrapper of StdMapSerializer, just as michael@0: // AddressMap is a simple wrapper of std::map. michael@0: StdMapSerializer std_map_serializer_; michael@0: }; michael@0: michael@0: // RangeMapSerializer allocates memory and serializes a RangeMap instance into a michael@0: // chunk of memory data. michael@0: template michael@0: class RangeMapSerializer { michael@0: public: michael@0: // Calculate the memory size of serialized data. michael@0: size_t SizeOf(const RangeMap &m) const; michael@0: michael@0: // Write the serialized data to specified memory location. Return the "end" michael@0: // of data, i.e., return the address after the final byte of data. michael@0: // NOTE: caller has to allocate enough memory before invoke Write() method. michael@0: char* Write(const RangeMap &m, char* dest) const; michael@0: michael@0: // Serializes a RangeMap object into a chunk of memory data. michael@0: // Returns a pointer to the serialized data. If size != NULL, *size is set michael@0: // to the size of serialized data, i.e., SizeOf(m). michael@0: // Caller has the ownership of memory allocated as "new char[]". michael@0: char* Serialize(const RangeMap &m, unsigned int *size) const; michael@0: michael@0: private: michael@0: // Convenient type name for Range. michael@0: typedef typename RangeMap::Range Range; michael@0: michael@0: // Serializer for RangeMap's key and Range::base_. michael@0: SimpleSerializer
address_serializer_; michael@0: // Serializer for RangeMap::Range::entry_. michael@0: SimpleSerializer entry_serializer_; michael@0: }; michael@0: michael@0: // ContainedRangeMapSerializer allocates memory and serializes a michael@0: // ContainedRangeMap instance into a chunk of memory data. michael@0: template michael@0: class ContainedRangeMapSerializer { michael@0: public: michael@0: // Calculate the memory size of serialized data. michael@0: size_t SizeOf(const ContainedRangeMap *m) const; michael@0: michael@0: // Write the serialized data to specified memory location. Return the "end" michael@0: // of data, i.e., return the address after the final byte of data. michael@0: // NOTE: caller has to allocate enough memory before invoke Write() method. michael@0: char* Write(const ContainedRangeMap *m, michael@0: char* dest) const; michael@0: michael@0: // Serializes a ContainedRangeMap object into a chunk of memory data. michael@0: // Returns a pointer to the serialized data. If size != NULL, *size is set michael@0: // to the size of serialized data, i.e., SizeOf(m). michael@0: // Caller has the ownership of memory allocated as "new char[]". michael@0: char* Serialize(const ContainedRangeMap *m, michael@0: unsigned int *size) const; michael@0: michael@0: private: michael@0: // Convenient type name for the underlying map type. michael@0: typedef std::map*> Map; michael@0: michael@0: // Serializer for addresses and entries stored in ContainedRangeMap. michael@0: SimpleSerializer addr_serializer_; michael@0: SimpleSerializer entry_serializer_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // PROCESSOR_MAP_SERIALIZERS_H__