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_inl.h: implementation for serializing std::map and its michael@0: // wrapper classes. michael@0: // michael@0: // See map_serializers.h for documentation. michael@0: // michael@0: // Author: Siyang Xie (lambxsy@google.com) michael@0: michael@0: #ifndef PROCESSOR_MAP_SERIALIZERS_INL_H__ michael@0: #define PROCESSOR_MAP_SERIALIZERS_INL_H__ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "processor/map_serializers.h" 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: #include "processor/logging.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: template michael@0: size_t StdMapSerializer::SizeOf( michael@0: const std::map &m) const { michael@0: size_t size = 0; michael@0: size_t header_size = (1 + m.size()) * sizeof(uint32_t); michael@0: size += header_size; michael@0: michael@0: typename std::map::const_iterator iter; michael@0: for (iter = m.begin(); iter != m.end(); ++iter) { michael@0: size += key_serializer_.SizeOf(iter->first); michael@0: size += value_serializer_.SizeOf(iter->second); michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: template michael@0: char *StdMapSerializer::Write(const std::map &m, michael@0: char *dest) const { michael@0: if (!dest) { michael@0: BPLOG(ERROR) << "StdMapSerializer failed: write to NULL address."; michael@0: return NULL; michael@0: } michael@0: char *start_address = dest; michael@0: michael@0: // Write header: michael@0: // Number of nodes. michael@0: dest = SimpleSerializer::Write(m.size(), dest); michael@0: // Nodes offsets. michael@0: uint32_t *offsets = reinterpret_cast(dest); michael@0: dest += sizeof(uint32_t) * m.size(); michael@0: michael@0: char *key_address = dest; michael@0: dest += sizeof(Key) * m.size(); michael@0: michael@0: // Traverse map. michael@0: typename std::map::const_iterator iter; michael@0: int index = 0; michael@0: for (iter = m.begin(); iter != m.end(); ++iter, ++index) { michael@0: offsets[index] = static_cast(dest - start_address); michael@0: key_address = key_serializer_.Write(iter->first, key_address); michael@0: dest = value_serializer_.Write(iter->second, dest); michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: template michael@0: char *StdMapSerializer::Serialize( michael@0: const std::map &m, unsigned int *size) const { michael@0: // Compute size of memory to be allocated. michael@0: unsigned int size_to_alloc = SizeOf(m); michael@0: // Allocate memory. michael@0: char *serialized_data = new char[size_to_alloc]; michael@0: if (!serialized_data) { michael@0: BPLOG(INFO) << "StdMapSerializer memory allocation failed."; michael@0: if (size) *size = 0; michael@0: return NULL; michael@0: } michael@0: // Write serialized data into memory. michael@0: Write(m, serialized_data); michael@0: michael@0: if (size) *size = size_to_alloc; michael@0: return serialized_data; michael@0: } michael@0: michael@0: template michael@0: size_t RangeMapSerializer::SizeOf( michael@0: const RangeMap &m) const { michael@0: size_t size = 0; michael@0: size_t header_size = (1 + m.map_.size()) * sizeof(uint32_t); michael@0: size += header_size; michael@0: michael@0: typename std::map::const_iterator iter; michael@0: for (iter = m.map_.begin(); iter != m.map_.end(); ++iter) { michael@0: // Size of key (high address). michael@0: size += address_serializer_.SizeOf(iter->first); michael@0: // Size of base (low address). michael@0: size += address_serializer_.SizeOf(iter->second.base()); michael@0: // Size of entry. michael@0: size += entry_serializer_.SizeOf(iter->second.entry()); michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: template michael@0: char *RangeMapSerializer::Write( michael@0: const RangeMap &m, char *dest) const { michael@0: if (!dest) { michael@0: BPLOG(ERROR) << "RangeMapSerializer failed: write to NULL address."; michael@0: return NULL; michael@0: } michael@0: char *start_address = dest; michael@0: michael@0: // Write header: michael@0: // Number of nodes. michael@0: dest = SimpleSerializer::Write(m.map_.size(), dest); michael@0: // Nodes offsets. michael@0: uint32_t *offsets = reinterpret_cast(dest); michael@0: dest += sizeof(uint32_t) * m.map_.size(); michael@0: michael@0: char *key_address = dest; michael@0: dest += sizeof(Address) * m.map_.size(); michael@0: michael@0: // Traverse map. michael@0: typename std::map::const_iterator iter; michael@0: int index = 0; michael@0: for (iter = m.map_.begin(); iter != m.map_.end(); ++iter, ++index) { michael@0: offsets[index] = static_cast(dest - start_address); michael@0: key_address = address_serializer_.Write(iter->first, key_address); michael@0: dest = address_serializer_.Write(iter->second.base(), dest); michael@0: dest = entry_serializer_.Write(iter->second.entry(), dest); michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: template michael@0: char *RangeMapSerializer::Serialize( michael@0: const RangeMap &m, unsigned int *size) const { michael@0: // Compute size of memory to be allocated. michael@0: unsigned int size_to_alloc = SizeOf(m); michael@0: // Allocate memory. michael@0: char *serialized_data = new char[size_to_alloc]; michael@0: if (!serialized_data) { michael@0: BPLOG(INFO) << "RangeMapSerializer memory allocation failed."; michael@0: if (size) *size = 0; michael@0: return NULL; michael@0: } michael@0: michael@0: // Write serialized data into memory. michael@0: Write(m, serialized_data); michael@0: michael@0: if (size) *size = size_to_alloc; michael@0: return serialized_data; michael@0: } michael@0: michael@0: michael@0: template michael@0: size_t ContainedRangeMapSerializer::SizeOf( michael@0: const ContainedRangeMap *m) const { michael@0: size_t size = 0; michael@0: size_t header_size = addr_serializer_.SizeOf(m->base_) michael@0: + entry_serializer_.SizeOf(m->entry_) michael@0: + sizeof(uint32_t); michael@0: size += header_size; michael@0: // In case m.map_ == NULL, we treat it as an empty map: michael@0: size += sizeof(uint32_t); michael@0: if (m->map_) { michael@0: size += m->map_->size() * sizeof(uint32_t); michael@0: typename Map::const_iterator iter; michael@0: for (iter = m->map_->begin(); iter != m->map_->end(); ++iter) { michael@0: size += addr_serializer_.SizeOf(iter->first); michael@0: // Recursive calculation of size: michael@0: size += SizeOf(iter->second); michael@0: } michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: template michael@0: char *ContainedRangeMapSerializer::Write( michael@0: const ContainedRangeMap *m, char *dest) const { michael@0: if (!dest) { michael@0: BPLOG(ERROR) << "StdMapSerializer failed: write to NULL address."; michael@0: return NULL; michael@0: } michael@0: dest = addr_serializer_.Write(m->base_, dest); michael@0: dest = SimpleSerializer::Write(entry_serializer_.SizeOf(m->entry_), michael@0: dest); michael@0: dest = entry_serializer_.Write(m->entry_, dest); michael@0: michael@0: // Write map<: michael@0: char *map_address = dest; michael@0: if (m->map_ == NULL) { michael@0: dest = SimpleSerializer::Write(0, dest); michael@0: } else { michael@0: dest = SimpleSerializer::Write(m->map_->size(), dest); michael@0: uint32_t *offsets = reinterpret_cast(dest); michael@0: dest += sizeof(uint32_t) * m->map_->size(); michael@0: michael@0: char *key_address = dest; michael@0: dest += sizeof(AddrType) * m->map_->size(); michael@0: michael@0: // Traverse map. michael@0: typename Map::const_iterator iter; michael@0: int index = 0; michael@0: for (iter = m->map_->begin(); iter != m->map_->end(); ++iter, ++index) { michael@0: offsets[index] = static_cast(dest - map_address); michael@0: key_address = addr_serializer_.Write(iter->first, key_address); michael@0: // Recursively write. michael@0: dest = Write(iter->second, dest); michael@0: } michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: template michael@0: char *ContainedRangeMapSerializer::Serialize( michael@0: const ContainedRangeMap *m, unsigned int *size) const { michael@0: unsigned int size_to_alloc = SizeOf(m); michael@0: // Allocating memory. michael@0: char *serialized_data = new char[size_to_alloc]; michael@0: if (!serialized_data) { michael@0: BPLOG(INFO) << "ContainedRangeMapSerializer memory allocation failed."; michael@0: if (size) *size = 0; michael@0: return NULL; michael@0: } michael@0: Write(m, serialized_data); michael@0: if (size) *size = size_to_alloc; michael@0: return serialized_data; michael@0: } michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // PROCESSOR_MAP_SERIALIZERS_INL_H__