| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/tracing/core/proto_zero_message.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "components/tracing/core/proto_utils.h" |
| 10 |
| 11 #if !defined(ARCH_CPU_LITTLE_ENDIAN) |
| 12 // The memcpy() for float and double below needs to be adjusted if we want to |
| 13 // support big endian CPUs. There doesn't seem to be a compelling need today. |
| 14 #error big-endian CPUs not supported by this translation unit. |
| 15 #endif |
| 16 |
| 17 namespace tracing { |
| 18 namespace v2 { |
| 19 |
| 20 ProtoZeroMessage::ProtoZeroMessage() { |
| 21 // Do NOT add any code here, use the Reset() method below instead. |
| 22 // Ctor and Dtor of ProtoZeroMessage are never called, with the exeception |
| 23 // of root (non-nested) messages. Nested messages are allocated in the |
| 24 // |nested_messages_arena_| and implictly destroyed when the arena of the |
| 25 // root message goes away. This is fine as long as all the fields are PODs, |
| 26 // hence the static_assert below. |
| 27 static_assert(base::is_trivially_destructible<ProtoZeroMessage>::value, |
| 28 "ProtoZeroMessage must be trivially destructible"); |
| 29 |
| 30 static_assert( |
| 31 sizeof(ProtoZeroMessage::nested_messages_arena_) >= |
| 32 kMaxNestingDepth * (sizeof(ProtoZeroMessage) - |
| 33 sizeof(ProtoZeroMessage::nested_messages_arena_)), |
| 34 "ProtoZeroMessage::nested_messages_arena_ is too small"); |
| 35 } |
| 36 |
| 37 // This method is called to initialize both root and nested messages. |
| 38 void ProtoZeroMessage::Reset(ScatteredStreamWriter* stream_writer) { |
| 39 stream_writer_ = stream_writer; |
| 40 size_ = 0; |
| 41 size_field_.reset(); |
| 42 size_already_written_ = 0; |
| 43 nested_message_ = nullptr; |
| 44 nesting_depth_ = 0; |
| 45 #if DCHECK_IS_ON() |
| 46 sealed_ = false; |
| 47 #endif |
| 48 } |
| 49 |
| 50 void ProtoZeroMessage::AppendVarIntU64(uint32_t field_id, uint64_t value) { |
| 51 if (nested_message_) |
| 52 EndNestedMessage(); |
| 53 |
| 54 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>() + |
| 55 proto::GetMaxVarIntEncodedSize<uint64_t>()]; |
| 56 uint8_t* data_end; |
| 57 data_end = proto::WriteVarIntU32(proto::MakeTagVarInt(field_id), data); |
| 58 data_end = proto::WriteVarIntU64(value, data_end); |
| 59 WriteToStream(data, data_end); |
| 60 } |
| 61 |
| 62 void ProtoZeroMessage::AppendVarIntU32(uint32_t field_id, uint32_t value) { |
| 63 // TODO(kraynov): this could be perf-optimized. See http://crbug.com/624311 . |
| 64 AppendVarIntU64(field_id, value); |
| 65 } |
| 66 |
| 67 void ProtoZeroMessage::AppendFloat(uint32_t field_id, float value) { |
| 68 if (nested_message_) |
| 69 EndNestedMessage(); |
| 70 |
| 71 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>() + sizeof(value)]; |
| 72 uint8_t* data_end; |
| 73 data_end = proto::WriteVarIntU32(proto::MakeTagFixed32(field_id), data); |
| 74 memcpy(data_end, &value, sizeof(value)); |
| 75 data_end += sizeof(value); |
| 76 WriteToStream(data, data_end); |
| 77 } |
| 78 |
| 79 void ProtoZeroMessage::AppendDouble(uint32_t field_id, double value) { |
| 80 if (nested_message_) |
| 81 EndNestedMessage(); |
| 82 |
| 83 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>() + sizeof(value)]; |
| 84 uint8_t* data_end; |
| 85 data_end = proto::WriteVarIntU32(proto::MakeTagFixed64(field_id), data); |
| 86 memcpy(data_end, &value, sizeof(value)); |
| 87 data_end += sizeof(value); |
| 88 WriteToStream(data, data_end); |
| 89 } |
| 90 |
| 91 void ProtoZeroMessage::AppendString(uint32_t field_id, const char* str) { |
| 92 AppendBytes(field_id, str, strlen(str)); |
| 93 } |
| 94 |
| 95 void ProtoZeroMessage::AppendBytes(uint32_t field_id, |
| 96 const void* src, |
| 97 size_t size) { |
| 98 if (nested_message_) |
| 99 EndNestedMessage(); |
| 100 |
| 101 // Write the proto preamble (field id, type and length of the buffer). |
| 102 uint8_t data[2 * proto::GetMaxVarIntEncodedSize<uint32_t>()]; |
| 103 uint8_t* data_end = |
| 104 proto::WriteVarIntU32(proto::MakeTagLengthDelimited(field_id), data); |
| 105 data_end = proto::WriteVarIntU32(static_cast<uint32_t>(size), data_end); |
| 106 WriteToStream(data, data_end); |
| 107 const uint8_t* src_u8 = reinterpret_cast<const uint8_t*>(src); |
| 108 WriteToStream(src_u8, src_u8 + size); |
| 109 } |
| 110 |
| 111 size_t ProtoZeroMessage::Finalize() { |
| 112 if (nested_message_) |
| 113 EndNestedMessage(); |
| 114 |
| 115 if (size_field_.is_valid()) { |
| 116 // Write the length of the nested message a posteriori, using a leading-zero |
| 117 // redundant varint encoding. |
| 118 #if DCHECK_IS_ON() |
| 119 DCHECK(!sealed_); |
| 120 #endif |
| 121 DCHECK_LT(size_, proto::kMaxMessageLength); |
| 122 DCHECK_EQ(proto::kMessageLengthFieldSize, size_field_.size()); |
| 123 proto::WriteRedundantVarIntU32<proto::kMessageLengthFieldSize>( |
| 124 static_cast<uint32_t>(size_ - size_already_written_), |
| 125 size_field_.begin); |
| 126 size_field_.reset(); |
| 127 } |
| 128 |
| 129 #if DCHECK_IS_ON() |
| 130 sealed_ = true; |
| 131 #endif |
| 132 |
| 133 return size_; |
| 134 } |
| 135 |
| 136 void ProtoZeroMessage::BeginNestedMessageInternal(uint32_t field_id, |
| 137 ProtoZeroMessage* message) { |
| 138 if (nested_message_) |
| 139 EndNestedMessage(); |
| 140 |
| 141 // Write the proto preamble for the nested message. |
| 142 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>()]; |
| 143 uint8_t* data_end = |
| 144 proto::WriteVarIntU32(proto::MakeTagLengthDelimited(field_id), data); |
| 145 WriteToStream(data, data_end); |
| 146 |
| 147 message->Reset(stream_writer_); |
| 148 CHECK_LT(nesting_depth_, kMaxNestingDepth); |
| 149 message->nesting_depth_ = nesting_depth_ + 1; |
| 150 |
| 151 // The length of the nested message cannot be known upfront. So right now |
| 152 // just reserve the bytes to encode the size after the nested message is done. |
| 153 message->set_size_field( |
| 154 stream_writer_->ReserveBytes(proto::kMessageLengthFieldSize)); |
| 155 size_ += proto::kMessageLengthFieldSize; |
| 156 nested_message_ = message; |
| 157 } |
| 158 |
| 159 void ProtoZeroMessage::EndNestedMessage() { |
| 160 size_ += nested_message_->Finalize(); |
| 161 nested_message_ = nullptr; |
| 162 } |
| 163 |
| 164 } // namespace v2 |
| 165 } // namespace tracing |
| OLD | NEW |