| 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 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <type_traits> | 10 #include <type_traits> |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 pos = proto::WriteVarInt(value, pos); | 76 pos = proto::WriteVarInt(value, pos); |
| 77 WriteToStream(buffer, pos); | 77 WriteToStream(buffer, pos); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Proto types: sint64, sint32. | 80 // Proto types: sint64, sint32. |
| 81 template <typename T> | 81 template <typename T> |
| 82 void AppendSignedVarInt(uint32_t field_id, T value) { | 82 void AppendSignedVarInt(uint32_t field_id, T value) { |
| 83 AppendVarInt(field_id, proto::ZigZagEncode(value)); | 83 AppendVarInt(field_id, proto::ZigZagEncode(value)); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Proto types: bool, enum (small). |
| 87 // Faster version of AppendVarInt for tiny numbers. |
| 88 void AppendTinyVarInt(uint32_t field_id, int32_t value) { |
| 89 DCHECK(0 <= value && value < 0x80); |
| 90 if (nested_message_) |
| 91 EndNestedMessage(); |
| 92 |
| 93 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| 94 uint8_t* pos = buffer; |
| 95 // MakeTagVarInt gets super optimized here for constexpr. |
| 96 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); |
| 97 *pos++ = static_cast<uint8_t>(value); |
| 98 WriteToStream(buffer, pos); |
| 99 } |
| 100 |
| 86 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. | 101 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. |
| 87 template <typename T> | 102 template <typename T> |
| 88 void AppendFixed(uint32_t field_id, T value) { | 103 void AppendFixed(uint32_t field_id, T value) { |
| 89 if (nested_message_) | 104 if (nested_message_) |
| 90 EndNestedMessage(); | 105 EndNestedMessage(); |
| 91 | 106 |
| 92 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; | 107 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| 93 uint8_t* pos = buffer; | 108 uint8_t* pos = buffer; |
| 94 | 109 |
| 95 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); | 110 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); |
| 96 memcpy(pos, &value, sizeof(T)); | 111 memcpy(pos, &value, sizeof(T)); |
| 97 pos += sizeof(T); | 112 pos += sizeof(T); |
| 98 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . | 113 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . |
| 99 WriteToStream(buffer, pos); | 114 WriteToStream(buffer, pos); |
| 100 } | 115 } |
| 101 | 116 |
| 102 // TODO(kraynov): Add AppendTinyInt. | |
| 103 void AppendString(uint32_t field_id, const char* str); | 117 void AppendString(uint32_t field_id, const char* str); |
| 104 void AppendBytes(uint32_t field_id, const void* value, size_t size); | 118 void AppendBytes(uint32_t field_id, const void* value, size_t size); |
| 105 | 119 |
| 106 // Begins a nested message, using the static storage provided by the parent | 120 // Begins a nested message, using the static storage provided by the parent |
| 107 // class (see comment in |nested_messages_arena_|). The nested message ends | 121 // class (see comment in |nested_messages_arena_|). The nested message ends |
| 108 // either when Finalize() is called or when any other Append* method is called | 122 // either when Finalize() is called or when any other Append* method is called |
| 109 // in the parent class. | 123 // in the parent class. |
| 110 // The template argument T is supposed to be a stub class auto generated from | 124 // The template argument T is supposed to be a stub class auto generated from |
| 111 // a .proto, hence a subclass of ProtoZeroMessage. | 125 // a .proto, hence a subclass of ProtoZeroMessage. |
| 112 template <class T> | 126 template <class T> |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of | 204 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of |
| 191 // nested messages would overflow the storage allocated by the root message. | 205 // nested messages would overflow the storage allocated by the root message. |
| 192 | 206 |
| 193 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); | 207 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); |
| 194 }; | 208 }; |
| 195 | 209 |
| 196 } // namespace v2 | 210 } // namespace v2 |
| 197 } // namespace tracing | 211 } // namespace tracing |
| 198 | 212 |
| 199 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 213 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| OLD | NEW |