Chromium Code Reviews| 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, | |
|
oystein (OOO til 10th of July)
2016/07/12 21:03:29
Why are these static_asserts in the constructor? S
Primiano Tucci (use gerrit)
2016/07/13 10:25:07
Two things here:
1) If I add them to the .h that w
| |
| 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 DCHECK_LT(size, proto::kMaxMessageLength); | |
|
oystein (OOO til 10th of July)
2016/07/12 21:03:29
Feels like this should be a stronger check (CHECK_
Primiano Tucci (use gerrit)
2016/07/13 10:25:07
this cannot overrun and cause memory corruption. E
| |
| 103 uint8_t data[2 * proto::GetMaxVarIntEncodedSize<uint32_t>()]; | |
| 104 uint8_t* data_end = | |
| 105 proto::WriteVarIntU32(proto::MakeTagLengthDelimited(field_id), data); | |
| 106 data_end = proto::WriteVarIntU32(static_cast<uint32_t>(size), data_end); | |
| 107 WriteToStream(data, data_end); | |
| 108 const uint8_t* src_u8 = reinterpret_cast<const uint8_t*>(src); | |
| 109 WriteToStream(src_u8, src_u8 + size); | |
| 110 } | |
| 111 | |
| 112 size_t ProtoZeroMessage::Finalize() { | |
| 113 if (nested_message_) | |
| 114 EndNestedMessage(); | |
| 115 | |
| 116 if (size_field_.is_valid()) { | |
| 117 // Write the length of the nested message a posteriori, using a leading-zero | |
| 118 // redundant varint encoding. | |
| 119 DCHECK(!sealed_); | |
|
oystein (OOO til 10th of July)
2016/07/12 21:03:29
DCHECK_IS_ON() (at least that's used around the DC
Primiano Tucci (use gerrit)
2016/07/13 10:25:07
Oops yes. forgot this at last minute. I suppose th
| |
| 120 DCHECK_LT(size_, proto::kMaxMessageLength); | |
| 121 DCHECK_EQ(proto::kMessageLengthFieldSize, size_field_.size()); | |
| 122 proto::WriteRedundantVarIntU32<proto::kMessageLengthFieldSize>( | |
| 123 static_cast<uint32_t>(size_ - size_already_written_), | |
| 124 size_field_.begin); | |
| 125 size_field_.reset(); | |
| 126 } | |
| 127 | |
| 128 #if DCHECK_IS_ON() | |
| 129 sealed_ = true; | |
| 130 #endif | |
| 131 | |
| 132 return size_; | |
| 133 } | |
| 134 | |
| 135 void ProtoZeroMessage::BeginNestedMessageInternal(uint32_t field_id, | |
| 136 ProtoZeroMessage* message) { | |
| 137 if (nested_message_) | |
| 138 EndNestedMessage(); | |
| 139 | |
| 140 // Write the proto preamble for the nested message. | |
| 141 uint8_t data[proto::GetMaxVarIntEncodedSize<uint32_t>()]; | |
| 142 uint8_t* data_end = | |
| 143 proto::WriteVarIntU32(proto::MakeTagLengthDelimited(field_id), data); | |
| 144 WriteToStream(data, data_end); | |
| 145 | |
| 146 message->Reset(stream_writer_); | |
| 147 CHECK_LT(nesting_depth_, kMaxNestingDepth); | |
| 148 message->nesting_depth_ = nesting_depth_ + 1; | |
| 149 | |
| 150 // The length of the nested message cannot be known upfront. So right now | |
| 151 // just reserve the bytes to encode the size after the nested message is done. | |
| 152 message->set_size_field( | |
| 153 stream_writer_->ReserveBytes(proto::kMessageLengthFieldSize)); | |
| 154 size_ += proto::kMessageLengthFieldSize; | |
| 155 nested_message_ = message; | |
| 156 } | |
| 157 | |
| 158 void ProtoZeroMessage::EndNestedMessage() { | |
| 159 size_ += nested_message_->Finalize(); | |
| 160 nested_message_ = nullptr; | |
| 161 } | |
| 162 | |
| 163 } // namespace v2 | |
| 164 } // namespace tracing | |
| OLD | NEW |