Chromium Code Reviews| 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> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/template_util.h" | 16 #include "base/template_util.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "components/tracing/core/proto_utils.h" | |
| 18 #include "components/tracing/core/scattered_stream_writer.h" | 19 #include "components/tracing/core/scattered_stream_writer.h" |
| 19 #include "components/tracing/tracing_export.h" | 20 #include "components/tracing/tracing_export.h" |
| 20 | 21 |
| 21 namespace tracing { | 22 namespace tracing { |
| 22 namespace v2 { | 23 namespace v2 { |
| 23 | 24 |
| 24 class ProtoZeroMessageHandleBase; | 25 class ProtoZeroMessageHandleBase; |
| 25 | 26 |
| 26 // Base class extended by the proto C++ stubs generated by the ProtoZero | 27 // Base class extended by the proto C++ stubs generated by the ProtoZero |
| 27 // compiler (see //components/tracing/tools/). This class provides the minimal | 28 // compiler (see //components/tracing/tools/). This class provides the minimal |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 54 void inc_size_already_written(size_t size) { size_already_written_ += size; } | 55 void inc_size_already_written(size_t size) { size_already_written_ += size; } |
| 55 | 56 |
| 56 #if DCHECK_IS_ON() | 57 #if DCHECK_IS_ON() |
| 57 // Only for ProtoZeroMessageHandleBase. | 58 // Only for ProtoZeroMessageHandleBase. |
| 58 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } | 59 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; } |
| 59 #endif | 60 #endif |
| 60 | 61 |
| 61 protected: | 62 protected: |
| 62 ProtoZeroMessage(); | 63 ProtoZeroMessage(); |
| 63 | 64 |
| 64 void AppendVarIntU64(uint32_t field_id, uint64_t value); | 65 // Proto types: uint64, uint32, int64, int32, bool, enum. |
| 65 void AppendVarIntU32(uint32_t field_id, uint32_t value); | 66 template <typename T> |
| 66 void AppendFloat(uint32_t field_id, float value); | 67 void AppendVarInt(uint32_t field_id, T value) { |
| 67 void AppendDouble(uint32_t field_id, double value); | 68 if (nested_message_) |
| 69 EndNestedMessage(); | |
| 70 | |
| 71 uint8_t buffer[proto::kMaxSingilarFieldEncodedSize]; | |
| 72 uint8_t* pos = buffer; | |
| 73 | |
| 74 pos = proto::WriteVarInt(proto::MakeTagVarInt(field_id), pos); | |
| 75 // WriteVarInt treat signed values in two's complement form. | |
|
petrcermak
2016/08/09 09:55:53
nit: s/treat/treats/. Also I think "writes" or "en
kraynov
2016/08/09 12:32:05
Done.
| |
| 76 pos = proto::WriteVarInt(value, pos); | |
| 77 WriteToStream(buffer, pos); | |
| 78 } | |
| 79 | |
| 80 // Proto types: sint64, sint32. | |
| 81 template <typename T> | |
| 82 void AppendSignedVarInt(uint32_t field_id, T value) { | |
| 83 AppendVarInt(field_id, proto::ZigZagEncode(value)); | |
| 84 } | |
| 85 | |
| 86 // Proto types: fixed64, sfixed64, fixed32, sfixed32, double, float. | |
| 87 template <typename T> | |
| 88 void AppendFixed(uint32_t field_id, T value) { | |
| 89 if (nested_message_) | |
| 90 EndNestedMessage(); | |
| 91 | |
| 92 uint8_t buffer[proto::kMaxSingilarFieldEncodedSize]; | |
| 93 uint8_t* pos = buffer; | |
| 94 | |
| 95 pos = proto::WriteVarInt(proto::MakeTagFixed<T>(field_id), pos); | |
| 96 memcpy(pos, &value, sizeof(T)); | |
| 97 pos += sizeof(T); | |
| 98 // TODO(kraynov) Optimize memcpy performance, see http://crbug.com/624311 . | |
| 99 WriteToStream(buffer, pos); | |
| 100 } | |
| 101 | |
| 102 // TODO(kraynov) AppendTinyInt. | |
|
petrcermak
2016/08/09 09:55:53
nit: missing colon "):". Also, please include the
kraynov
2016/08/09 12:32:05
Done.
| |
| 68 void AppendString(uint32_t field_id, const char* str); | 103 void AppendString(uint32_t field_id, const char* str); |
| 69 void AppendBytes(uint32_t field_id, const void* value, size_t size); | 104 void AppendBytes(uint32_t field_id, const void* value, size_t size); |
| 70 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding. | |
| 71 | 105 |
| 72 // Begins a nested message, using the static storage provided by the parent | 106 // Begins a nested message, using the static storage provided by the parent |
| 73 // class (see comment in |nested_messages_arena_|). The nested message ends | 107 // class (see comment in |nested_messages_arena_|). The nested message ends |
| 74 // either when Finalize() is called or when any other Append* method is called | 108 // either when Finalize() is called or when any other Append* method is called |
| 75 // in the parent class. | 109 // in the parent class. |
| 76 // The template argument T is supposed to be a stub class auto generated from | 110 // The template argument T is supposed to be a stub class auto generated from |
| 77 // a .proto, hence a subclass of ProtoZeroMessage. | 111 // a .proto, hence a subclass of ProtoZeroMessage. |
| 78 template <class T> | 112 template <class T> |
| 79 T* BeginNestedMessage(uint32_t field_id) { | 113 T* BeginNestedMessage(uint32_t field_id) { |
| 80 // This is to prevent subclasses (which should be autogenerated, though), to | 114 // This is to prevent subclasses (which should be autogenerated, though), to |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of | 190 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of |
| 157 // nested messages would overflow the storage allocated by the root message. | 191 // nested messages would overflow the storage allocated by the root message. |
| 158 | 192 |
| 159 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); | 193 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); |
| 160 }; | 194 }; |
| 161 | 195 |
| 162 } // namespace v2 | 196 } // namespace v2 |
| 163 } // namespace tracing | 197 } // namespace tracing |
| 164 | 198 |
| 165 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 199 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| OLD | NEW |