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" |
20 #include "components/tracing/tracing_export.h" | 21 #include "components/tracing/tracing_export.h" |
21 | 22 |
22 namespace tracing { | 23 namespace tracing { |
23 namespace v2 { | 24 namespace v2 { |
24 | 25 |
26 #if DCHECK_IS_ON() | |
25 class ProtoZeroMessageHandleBase; | 27 class ProtoZeroMessageHandleBase; |
28 #endif | |
26 | 29 |
27 // Base class extended by the proto C++ stubs generated by the ProtoZero | 30 // Base class extended by the proto C++ stubs generated by the ProtoZero |
28 // compiler (see //components/tracing/tools/). This class provides the minimal | 31 // compiler (see //components/tracing/tools/). This class provides the minimal |
29 // runtime required to support append-only operations and is desiged for | 32 // runtime required to support append-only operations and is desiged for |
30 // performance. None of the methods require any dynamic memory allocation. | 33 // performance. None of the methods require any dynamic memory allocation. |
31 class TRACING_EXPORT ProtoZeroMessage { | 34 class TRACING_EXPORT ProtoZeroMessage { |
32 public: | 35 public: |
33 // Clears up the state, allowing the message to be reused as a fresh one. | 36 // Clears up the state, allowing the message to be reused as a fresh one. |
34 void Reset(ScatteredStreamWriter*); | 37 void Reset(ScatteredStreamWriter*); |
35 | 38 |
(...skipping 19 matching lines...) Expand all Loading... | |
55 void inc_size_already_written(size_t size) { size_already_written_ += size; } | 58 void inc_size_already_written(size_t size) { size_already_written_ += size; } |
56 | 59 |
57 #if DCHECK_IS_ON() | 60 #if DCHECK_IS_ON() |
58 // Only for ProtoZeroMessageHandleBase. | 61 // Only for ProtoZeroMessageHandleBase. |
59 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } | 62 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } |
60 #endif | 63 #endif |
61 | 64 |
62 protected: | 65 protected: |
63 ProtoZeroMessage(); | 66 ProtoZeroMessage(); |
64 | 67 |
65 // Proto types: uint64, uint32, int64, int32, bool, enum. | 68 // Proto types: uint64, uint32, int64, int32, enum (large). |
66 template <typename T> | 69 template <typename T> |
67 void AppendVarInt(uint32_t field_id, T value) { | 70 void AppendVarInt(uint32_t field_id, T value) { |
68 if (nested_message_) | 71 if (nested_message_) |
69 EndNestedMessage(); | 72 EndNestedMessage(); |
70 | 73 |
71 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; | 74 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
72 uint8_t* pos = buffer; | 75 uint8_t* pos = buffer; |
73 | 76 |
74 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); | 77 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); |
75 // WriteVarInt encodes signed values in two's complement form. | 78 // WriteVarInt encodes signed values in two's complement form. |
76 pos = proto::WriteVarInt(value, pos); | 79 pos = proto::WriteVarInt(value, pos); |
77 WriteToStream(buffer, pos); | 80 WriteToStream(buffer, pos); |
78 } | 81 } |
79 | 82 |
80 // Proto types: sint64, sint32. | 83 // Proto types: sint64, sint32. |
81 template <typename T> | 84 template <typename T> |
82 void AppendSignedVarInt(uint32_t field_id, T value) { | 85 void AppendSignedVarInt(uint32_t field_id, T value) { |
83 AppendVarInt(field_id, proto::ZigZagEncode(value)); | 86 AppendVarInt(field_id, proto::ZigZagEncode(value)); |
84 } | 87 } |
85 | 88 |
89 // Proto types: bool, enum (small). | |
90 // It could be looking weird not to optimize writing field_id, but it's | |
91 // unnecessary because compiler already optimize WriteVarInt for constexpr. | |
92 void AppendTinyVarInt(uint32_t field_id, int32_t value) { | |
93 DCHECK(0 <= value && value < 0x80); | |
94 if (nested_message_) | |
95 EndNestedMessage(); | |
96 | |
97 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; | |
98 uint8_t* pos = buffer; | |
99 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); | |
100 *pos = static_cast<uint8_t>(value); | |
alph
2016/08/22 22:48:35
nit: *pos++ =
kraynov
2016/08/23 12:43:49
Done.
| |
101 pos++; | |
102 WriteToStream(buffer, pos); | |
103 } | |
104 | |
86 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. | 105 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. |
87 template <typename T> | 106 template <typename T> |
88 void AppendFixed(uint32_t field_id, T value) { | 107 void AppendFixed(uint32_t field_id, T value) { |
89 if (nested_message_) | 108 if (nested_message_) |
90 EndNestedMessage(); | 109 EndNestedMessage(); |
91 | 110 |
92 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; | 111 uint8_t buffer[proto::kMaxSimpleFieldEncodedSize]; |
93 uint8_t* pos = buffer; | 112 uint8_t* pos = buffer; |
94 | 113 |
95 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); | 114 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); |
96 memcpy(pos, &value, sizeof(T)); | 115 memcpy(pos, &value, sizeof(T)); |
97 pos += sizeof(T); | 116 pos += sizeof(T); |
98 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . | 117 // TODO(kraynov): Optimize memcpy performance, see http://crbug.com/624311 . |
99 WriteToStream(buffer, pos); | 118 WriteToStream(buffer, pos); |
100 } | 119 } |
101 | 120 |
102 // TODO(kraynov): Add AppendTinyInt. | |
103 void AppendString(uint32_t field_id, const char* str); | 121 void AppendString(uint32_t field_id, const char* str); |
104 void AppendBytes(uint32_t field_id, const void* value, size_t size); | 122 void AppendBytes(uint32_t field_id, const void* value, size_t size); |
105 | 123 |
106 // Begins a nested message, using the static storage provided by the parent | 124 // Begins a nested message, using the static storage provided by the parent |
107 // class (see comment in |nested_messages_arena_|). The nested message ends | 125 // 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 | 126 // either when Finalize() is called or when any other Append* method is called |
109 // in the parent class. | 127 // in the parent class. |
110 // The template argument T is supposed to be a stub class auto generated from | 128 // The template argument T is supposed to be a stub class auto generated from |
111 // a .proto, hence a subclass of ProtoZeroMessage. | 129 // a .proto, hence a subclass of ProtoZeroMessage. |
112 template <class T> | 130 template <class T> |
113 T* BeginNestedMessage(uint32_t field_id) { | 131 T* BeginNestedMessage(uint32_t field_id) { |
114 // This is to prevent subclasses (which should be autogenerated, though), to | 132 // This is to prevent subclasses (which should be autogenerated, though), to |
115 // introduce extra state fields (which wouldn't be initialized by Reset()). | 133 // introduce extra state fields (which wouldn't be initialized by Reset()). |
116 static_assert(std::is_base_of<ProtoZeroMessage, T>::value, | 134 static_assert(std::is_base_of<ProtoZeroMessage, T>::value, |
117 "T must be a subclass of ProtoZeroMessage"); | 135 "T must be a subclass of ProtoZeroMessage"); |
118 static_assert(sizeof(T) == sizeof(ProtoZeroMessage), | 136 static_assert(sizeof(T) == sizeof(ProtoZeroMessage), |
119 "ProtoZeroMessage subclasses cannot introduce extra state."); | 137 "ProtoZeroMessage subclasses cannot introduce extra state."); |
120 T* message = reinterpret_cast<T*>(nested_messages_arena_); | 138 T* message = reinterpret_cast<T*>(nested_messages_arena_); |
121 BeginNestedMessageInternal(field_id, message); | 139 BeginNestedMessageInternal(field_id, message); |
122 return message; | 140 return message; |
123 } | 141 } |
124 | 142 |
143 // Prevent multiple appends for non-repeated fields. | |
144 inline void DCheckSealField(uint32_t field_id) { | |
145 #if DCHECK_IS_ON() | |
146 DCHECK(field_id > 0); | |
147 SealField(field_id); | |
148 #endif | |
149 } | |
150 | |
125 private: | 151 private: |
126 friend class ProtoZeroMessageTest; | 152 friend class ProtoZeroMessageTest; |
127 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); | 153 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); |
128 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); | 154 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); |
129 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); | 155 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); |
130 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); | 156 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); |
131 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, MessageHandle); | 157 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, MessageHandle); |
132 | 158 |
133 enum : uint32_t { kMaxNestingDepth = 8 }; | 159 enum : uint32_t { kMaxNestingDepth = 8 }; |
134 | 160 |
135 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); | 161 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); |
136 | 162 |
137 // Called by Finalize and Append* methods. | 163 // Called by Finalize and Append* methods. |
138 void EndNestedMessage(); | 164 void EndNestedMessage(); |
139 | 165 |
140 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { | 166 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { |
141 #if DCHECK_IS_ON() | 167 #if DCHECK_IS_ON() |
142 DCHECK(!sealed_); | 168 DCHECK(!sealed_); |
143 #endif | 169 #endif |
144 DCHECK(src_begin < src_end); | 170 DCHECK(src_begin < src_end); |
145 const size_t size = static_cast<size_t>(src_end - src_begin); | 171 const size_t size = static_cast<size_t>(src_end - src_begin); |
146 stream_writer_->WriteBytes(src_begin, size); | 172 stream_writer_->WriteBytes(src_begin, size); |
147 size_ += size; | 173 size_ += size; |
148 } | 174 } |
149 | 175 |
176 #if DCHECK_IS_ON() | |
177 void SealField(uint32_t field_id); | |
178 #endif | |
179 | |
150 // Only POD fields are allowed. This class's dtor is never called. | 180 // Only POD fields are allowed. This class's dtor is never called. |
151 // See the comment on the static_assert in the the corresponding .cc file. | 181 // See the comment on the static_assert in the the corresponding .cc file. |
152 | 182 |
153 // The stream writer interface used for the serialization. | 183 // The stream writer interface used for the serialization. |
154 ScatteredStreamWriter* stream_writer_; | 184 ScatteredStreamWriter* stream_writer_; |
155 | 185 |
156 // Keeps track of the size of the current message. | 186 // Keeps track of the size of the current message. |
157 size_t size_; | 187 size_t size_; |
158 | 188 |
159 ContiguousMemoryRange size_field_; | 189 ContiguousMemoryRange size_field_; |
(...skipping 30 matching lines...) Expand all Loading... | |
190 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of | 220 // 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. | 221 // nested messages would overflow the storage allocated by the root message. |
192 | 222 |
193 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); | 223 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); |
194 }; | 224 }; |
195 | 225 |
196 } // namespace v2 | 226 } // namespace v2 |
197 } // namespace tracing | 227 } // namespace tracing |
198 | 228 |
199 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 229 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
OLD | NEW |