| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/tracing/core/proto_zero_message.h" | 5 #include "components/tracing/core/proto_zero_message.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "components/tracing/core/proto_utils.h" | |
| 10 #include "components/tracing/core/proto_zero_message_handle.h" | 9 #include "components/tracing/core/proto_zero_message_handle.h" |
| 11 | 10 |
| 12 #if !defined(ARCH_CPU_LITTLE_ENDIAN) | 11 #if !defined(ARCH_CPU_LITTLE_ENDIAN) |
| 13 // The memcpy() for float and double below needs to be adjusted if we want to | 12 // The memcpy() for float and double below needs to be adjusted if we want to |
| 14 // support big endian CPUs. There doesn't seem to be a compelling need today. | 13 // support big endian CPUs. There doesn't seem to be a compelling need today. |
| 15 #error big-endian CPUs not supported by this translation unit. | 14 #error big-endian CPUs not supported by this translation unit. |
| 16 #endif | 15 #endif |
| 17 | 16 |
| 18 namespace tracing { | 17 namespace tracing { |
| 19 namespace v2 { | 18 namespace v2 { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 42 size_field_.reset(); | 41 size_field_.reset(); |
| 43 size_already_written_ = 0; | 42 size_already_written_ = 0; |
| 44 nested_message_ = nullptr; | 43 nested_message_ = nullptr; |
| 45 nesting_depth_ = 0; | 44 nesting_depth_ = 0; |
| 46 #if DCHECK_IS_ON() | 45 #if DCHECK_IS_ON() |
| 47 sealed_ = false; | 46 sealed_ = false; |
| 48 handle_ = nullptr; | 47 handle_ = nullptr; |
| 49 #endif | 48 #endif |
| 50 } | 49 } |
| 51 | 50 |
| 52 void ProtoZeroMessage::AppendVarIntU64(uint32_t field_id, uint64_t value) { | |
| 53 if (nested_message_) | |
| 54 EndNestedMessage(); | |
| 55 | |
| 56 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>() + | |
| 57 proto::GetMaxVarIntEncodedSize<uint64_t>()]; | |
| 58 uint8_t* data_end; | |
| 59 data_end = proto::WriteVarIntU32(proto::MakeTagVarInt(field_id), data); | |
| 60 data_end = proto::WriteVarIntU64(value, data_end); | |
| 61 WriteToStream(data, data_end); | |
| 62 } | |
| 63 | |
| 64 void ProtoZeroMessage::AppendVarIntU32(uint32_t field_id, uint32_t value) { | |
| 65 // TODO(kraynov): this could be perf-optimized. See http://crbug.com/624311 . | |
| 66 AppendVarIntU64(field_id, value); | |
| 67 } | |
| 68 | |
| 69 void ProtoZeroMessage::AppendFloat(uint32_t field_id, float value) { | |
| 70 if (nested_message_) | |
| 71 EndNestedMessage(); | |
| 72 | |
| 73 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>() + sizeof(value)]; | |
| 74 uint8_t* data_end; | |
| 75 data_end = proto::WriteVarIntU32(proto::MakeTagFixed32(field_id), data); | |
| 76 memcpy(data_end, &value, sizeof(value)); | |
| 77 data_end += sizeof(value); | |
| 78 WriteToStream(data, data_end); | |
| 79 } | |
| 80 | |
| 81 void ProtoZeroMessage::AppendDouble(uint32_t field_id, double value) { | |
| 82 if (nested_message_) | |
| 83 EndNestedMessage(); | |
| 84 | |
| 85 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>() + sizeof(value)]; | |
| 86 uint8_t* data_end; | |
| 87 data_end = proto::WriteVarIntU32(proto::MakeTagFixed64(field_id), data); | |
| 88 memcpy(data_end, &value, sizeof(value)); | |
| 89 data_end += sizeof(value); | |
| 90 WriteToStream(data, data_end); | |
| 91 } | |
| 92 | |
| 93 void ProtoZeroMessage::AppendString(uint32_t field_id, const char* str) { | 51 void ProtoZeroMessage::AppendString(uint32_t field_id, const char* str) { |
| 94 AppendBytes(field_id, str, strlen(str)); | 52 AppendBytes(field_id, str, strlen(str)); |
| 95 } | 53 } |
| 96 | 54 |
| 97 void ProtoZeroMessage::AppendBytes(uint32_t field_id, | 55 void ProtoZeroMessage::AppendBytes(uint32_t field_id, |
| 98 const void* src, | 56 const void* src, |
| 99 size_t size) { | 57 size_t size) { |
| 100 if (nested_message_) | 58 if (nested_message_) |
| 101 EndNestedMessage(); | 59 EndNestedMessage(); |
| 102 | 60 |
| 103 // Write the proto preamble (field id, type and length of the buffer). | 61 DCHECK_LT(size, proto::kMaxMessageLength); |
| 104 uint8_t data[2 * proto::GetMaxVarIntEncodedSize<uint32_t>()]; | 62 // Write the proto preamble (field id, type and length of the field). |
| 105 uint8_t* data_end = | 63 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| 106 proto::WriteVarIntU32(proto::MakeTagLengthDelimited(field_id), data); | 64 uint8_t* pos = buffer; |
| 107 data_end = proto::WriteVarIntU32(static_cast<uint32_t>(size), data_end); | 65 pos = proto::WriteVarInt(proto::MakeTagLengthDelimited(field_id), pos); |
| 108 WriteToStream(data, data_end); | 66 pos = proto::WriteVarInt(static_cast<uint32_t>(size), pos); |
| 67 WriteToStream(buffer, pos); |
| 68 |
| 109 const uint8_t* src_u8 = reinterpret_cast<const uint8_t*>(src); | 69 const uint8_t* src_u8 = reinterpret_cast<const uint8_t*>(src); |
| 110 WriteToStream(src_u8, src_u8 + size); | 70 WriteToStream(src_u8, src_u8 + size); |
| 111 } | 71 } |
| 112 | 72 |
| 113 size_t ProtoZeroMessage::Finalize() { | 73 size_t ProtoZeroMessage::Finalize() { |
| 114 if (nested_message_) | 74 if (nested_message_) |
| 115 EndNestedMessage(); | 75 EndNestedMessage(); |
| 116 | 76 |
| 117 if (size_field_.is_valid()) { | 77 if (size_field_.is_valid()) { |
| 118 // Write the length of the nested message a posteriori, using a leading-zero | 78 // Write the length of the nested message a posteriori, using a leading-zero |
| 119 // redundant varint encoding. | 79 // redundant varint encoding. |
| 120 #if DCHECK_IS_ON() | 80 #if DCHECK_IS_ON() |
| 121 DCHECK(!sealed_); | 81 DCHECK(!sealed_); |
| 122 #endif | 82 #endif |
| 123 DCHECK_LT(size_, proto::kMaxMessageLength); | 83 DCHECK_LT(size_, proto::kMaxMessageLength); |
| 124 DCHECK_EQ(proto::kMessageLengthFieldSize, size_field_.size()); | 84 DCHECK_EQ(proto::kMessageLengthFieldSize, size_field_.size()); |
| 125 proto::WriteRedundantVarIntU32<proto::kMessageLengthFieldSize>( | 85 proto::WriteRedundantLength( |
| 126 static_cast<uint32_t>(size_ - size_already_written_), | 86 static_cast<uint32_t>(size_ - size_already_written_), |
| 127 size_field_.begin); | 87 size_field_.begin); |
| 128 size_field_.reset(); | 88 size_field_.reset(); |
| 129 } | 89 } |
| 130 | 90 |
| 131 #if DCHECK_IS_ON() | 91 #if DCHECK_IS_ON() |
| 132 sealed_ = true; | 92 sealed_ = true; |
| 133 if (handle_) | 93 if (handle_) |
| 134 handle_->reset_message(); | 94 handle_->reset_message(); |
| 135 #endif | 95 #endif |
| 136 | 96 |
| 137 return size_; | 97 return size_; |
| 138 } | 98 } |
| 139 | 99 |
| 140 void ProtoZeroMessage::BeginNestedMessageInternal(uint32_t field_id, | 100 void ProtoZeroMessage::BeginNestedMessageInternal(uint32_t field_id, |
| 141 ProtoZeroMessage* message) { | 101 ProtoZeroMessage* message) { |
| 142 if (nested_message_) | 102 if (nested_message_) |
| 143 EndNestedMessage(); | 103 EndNestedMessage(); |
| 144 | 104 |
| 145 // Write the proto preamble for the nested message. | 105 // Write the proto preamble for the nested message. |
| 146 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>()]; | 106 uint8_t data[proto::kMaxTagEncodedSize]; |
| 147 uint8_t* data_end = | 107 uint8_t* data_end = |
| 148 proto::WriteVarIntU32(proto::MakeTagLengthDelimited(field_id), data); | 108 proto::WriteVarInt(proto::MakeTagLengthDelimited(field_id), data); |
| 149 WriteToStream(data, data_end); | 109 WriteToStream(data, data_end); |
| 150 | 110 |
| 151 message->Reset(stream_writer_); | 111 message->Reset(stream_writer_); |
| 152 CHECK_LT(nesting_depth_, kMaxNestingDepth); | 112 CHECK_LT(nesting_depth_, kMaxNestingDepth); |
| 153 message->nesting_depth_ = nesting_depth_ + 1; | 113 message->nesting_depth_ = nesting_depth_ + 1; |
| 154 | 114 |
| 155 // The length of the nested message cannot be known upfront. So right now | 115 // The length of the nested message cannot be known upfront. So right now |
| 156 // just reserve the bytes to encode the size after the nested message is done. | 116 // just reserve the bytes to encode the size after the nested message is done. |
| 157 message->set_size_field( | 117 message->set_size_field( |
| 158 stream_writer_->ReserveBytes(proto::kMessageLengthFieldSize)); | 118 stream_writer_->ReserveBytes(proto::kMessageLengthFieldSize)); |
| 159 size_ += proto::kMessageLengthFieldSize; | 119 size_ += proto::kMessageLengthFieldSize; |
| 160 nested_message_ = message; | 120 nested_message_ = message; |
| 161 } | 121 } |
| 162 | 122 |
| 163 void ProtoZeroMessage::EndNestedMessage() { | 123 void ProtoZeroMessage::EndNestedMessage() { |
| 164 size_ += nested_message_->Finalize(); | 124 size_ += nested_message_->Finalize(); |
| 165 nested_message_ = nullptr; | 125 nested_message_ = nullptr; |
| 166 } | 126 } |
| 167 | 127 |
| 168 } // namespace v2 | 128 } // namespace v2 |
| 169 } // namespace tracing | 129 } // namespace tracing |
| OLD | NEW |