| 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 <set> |
| 10 #include <type_traits> | 11 #include <type_traits> |
| 11 | 12 |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/template_util.h" | 17 #include "base/template_util.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 #include "components/tracing/core/proto_utils.h" | 19 #include "components/tracing/core/proto_utils.h" |
| 19 #include "components/tracing/core/scattered_stream_writer.h" | 20 #include "components/tracing/core/scattered_stream_writer.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 void inc_size_already_written(size_t size) { size_already_written_ += size; } | 56 void inc_size_already_written(size_t size) { size_already_written_ += size; } |
| 56 | 57 |
| 57 #if DCHECK_IS_ON() | 58 #if DCHECK_IS_ON() |
| 58 // Only for ProtoZeroMessageHandleBase. | 59 // Only for ProtoZeroMessageHandleBase. |
| 59 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } | 60 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } |
| 60 #endif | 61 #endif |
| 61 | 62 |
| 62 protected: | 63 protected: |
| 63 ProtoZeroMessage(); | 64 ProtoZeroMessage(); |
| 64 | 65 |
| 65 // Proto types: uint64, uint32, int64, int32, bool, enum. | 66 // Proto types: uint64, uint32, int64, int32, enum (large). |
| 66 template <typename T> | 67 template <typename T> |
| 67 void AppendVarInt(uint32_t field_id, T value) { | 68 void AppendVarInt(uint32_t field_id, T value) { |
| 68 if (nested_message_) | 69 if (nested_message_) |
| 69 EndNestedMessage(); | 70 EndNestedMessage(); |
| 70 | 71 |
| 71 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; | 72 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| 72 uint8_t* pos = buffer; | 73 uint8_t* pos = buffer; |
| 73 | 74 |
| 74 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); | 75 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); |
| 75 // WriteVarInt encodes signed values in two's complement form. | 76 // WriteVarInt encodes signed values in two's complement form. |
| 76 pos = proto::WriteVarInt(value, pos); | 77 pos = proto::WriteVarInt(value, pos); |
| 77 WriteToStream(buffer, pos); | 78 WriteToStream(buffer, pos); |
| 78 } | 79 } |
| 79 | 80 |
| 80 // Proto types: sint64, sint32. | 81 // Proto types: sint64, sint32. |
| 81 template <typename T> | 82 template <typename T> |
| 82 void AppendSignedVarInt(uint32_t field_id, T value) { | 83 void AppendSignedVarInt(uint32_t field_id, T value) { |
| 83 AppendVarInt(field_id, proto::ZigZagEncode(value)); | 84 AppendVarInt(field_id, proto::ZigZagEncode(value)); |
| 84 } | 85 } |
| 85 | 86 |
| 87 // Proto types: bool, enum (small). |
| 88 // Faster version of AppendVarInt for tiny numbers. |
| 89 void AppendTinyVarInt(uint32_t field_id, int32_t value) { |
| 90 DCHECK(0 <= value && value < 0x80); |
| 91 if (nested_message_) |
| 92 EndNestedMessage(); |
| 93 |
| 94 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| 95 uint8_t* pos = buffer; |
| 96 // MakeTagVarInt gets super optimized here for constexpr. |
| 97 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); |
| 98 *pos++ = static_cast<uint8_t>(value); |
| 99 WriteToStream(buffer, pos); |
| 100 } |
| 101 |
| 86 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. | 102 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. |
| 87 template <typename T> | 103 template <typename T> |
| 88 void AppendFixed(uint32_t field_id, T value) { | 104 void AppendFixed(uint32_t field_id, T value) { |
| 89 if (nested_message_) | 105 if (nested_message_) |
| 90 EndNestedMessage(); | 106 EndNestedMessage(); |
| 91 | 107 |
| 92 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; | 108 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
| 93 uint8_t* pos = buffer; | 109 uint8_t* pos = buffer; |
| 94 | 110 |
| 95 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); | 111 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); |
| 96 memcpy(pos, &value, sizeof(T)); | 112 memcpy(pos, &value, sizeof(T)); |
| 97 pos += sizeof(T); | 113 pos += sizeof(T); |
| 98 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . | 114 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . |
| 99 WriteToStream(buffer, pos); | 115 WriteToStream(buffer, pos); |
| 100 } | 116 } |
| 101 | 117 |
| 102 // TODO(kraynov): Add AppendTinyInt. | |
| 103 void AppendString(uint32_t field_id, const char* str); | 118 void AppendString(uint32_t field_id, const char* str); |
| 104 void AppendBytes(uint32_t field_id, const void* value, size_t size); | 119 void AppendBytes(uint32_t field_id, const void* value, size_t size); |
| 105 | 120 |
| 106 // Begins a nested message, using the static storage provided by the parent | 121 // Begins a nested message, using the static storage provided by the parent |
| 107 // class (see comment in |nested_messages_arena_|). The nested message ends | 122 // 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 | 123 // either when Finalize() is called or when any other Append* method is called |
| 109 // in the parent class. | 124 // in the parent class. |
| 110 // The template argument T is supposed to be a stub class auto generated from | 125 // The template argument T is supposed to be a stub class auto generated from |
| 111 // a .proto, hence a subclass of ProtoZeroMessage. | 126 // a .proto, hence a subclass of ProtoZeroMessage. |
| 112 template <class T> | 127 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 | 205 // 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. | 206 // nested messages would overflow the storage allocated by the root message. |
| 192 | 207 |
| 193 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); | 208 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); |
| 194 }; | 209 }; |
| 195 | 210 |
| 196 } // namespace v2 | 211 } // namespace v2 |
| 197 } // namespace tracing | 212 } // namespace tracing |
| 198 | 213 |
| 199 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 214 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| OLD | NEW |