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/scattered_stream_writer.h" | 18 #include "components/tracing/core/scattered_stream_writer.h" |
| 19 #include "components/tracing/tracing_export.h" | 19 #include "components/tracing/tracing_export.h" |
| 20 | 20 |
| 21 namespace tracing { | 21 namespace tracing { |
| 22 namespace v2 { | 22 namespace v2 { |
| 23 | 23 |
| 24 class ProtoZeroMessageHandleBase; | |
| 25 | |
| 24 // Base class extended by the proto C++ stubs generated by the ProtoZero | 26 // Base class extended by the proto C++ stubs generated by the ProtoZero |
| 25 // compiler (see //components/tracing/tools/). This class provides the minimal | 27 // compiler (see //components/tracing/tools/). This class provides the minimal |
| 26 // runtime required to support append-only operations and is desiged for | 28 // runtime required to support append-only operations and is desiged for |
| 27 // performance. None of the methods require any dynamic memory allocation. | 29 // performance. None of the methods require any dynamic memory allocation. |
| 28 class TRACING_EXPORT ProtoZeroMessage { | 30 class TRACING_EXPORT ProtoZeroMessage { |
| 29 public: | 31 public: |
| 32 // Clears up the state, allowing the message to be reused as a fresh one. | |
| 33 void Reset(ScatteredStreamWriter*); | |
| 34 | |
| 30 // Commits all the changes to the buffer (backfills the size field of this and | 35 // Commits all the changes to the buffer (backfills the size field of this and |
| 31 // all nested messages) and seals the message. Returns the size of the message | 36 // all nested messages) and seals the message. Returns the size of the message |
| 32 // (and all nested sub-messages), without taking into account any chunking. | 37 // (and all nested sub-messages), without taking into account any chunking. |
| 33 // Finalize is idempotent and can be called several times w/o side effects. | 38 // Finalize is idempotent and can be called several times w/o side effects. |
| 34 size_t Finalize(); | 39 size_t Finalize(); |
| 35 | 40 |
| 36 // Optional. If is_valid() == true, the corresponding memory region (its | 41 // Optional. If is_valid() == true, the corresponding memory region (its |
| 37 // length == proto::kMessageLengthFieldSize) is backfilled with the size of | 42 // length == proto::kMessageLengthFieldSize) is backfilled with the size of |
| 38 // this message (minus |size_already_written| below) when the message is | 43 // this message (minus |size_already_written| below) when the message is |
| 39 // finalized. This is the mechanism used by messages to backfill their | 44 // finalized. This is the mechanism used by messages to backfill their |
| 40 // corresponding size field in the parent message. | 45 // corresponding size field in the parent message. |
| 41 ContiguousMemoryRange size_field() const { return size_field_; } | 46 ContiguousMemoryRange size_field() const { return size_field_; } |
| 42 void set_size_field(const ContiguousMemoryRange& reserved_range) { | 47 void set_size_field(const ContiguousMemoryRange& reserved_range) { |
| 43 size_field_ = reserved_range; | 48 size_field_ = reserved_range; |
| 44 } | 49 } |
| 45 | 50 |
| 46 // This is to deal with case of backfilling the size of a root (non-nested) | 51 // This is to deal with case of backfilling the size of a root (non-nested) |
| 47 // message which is split into multiple chunks. Upon finalization only the | 52 // message which is split into multiple chunks. Upon finalization only the |
| 48 // partial size that lies in the last chunk has to be backfilled. | 53 // partial size that lies in the last chunk has to be backfilled. |
| 49 void inc_size_already_written(size_t size) { size_already_written_ += size; } | 54 void inc_size_already_written(size_t size) { size_already_written_ += size; } |
| 50 | 55 |
| 56 // Only for ProtoZeroMessageHandleBase. | |
| 57 void set_handle(ProtoZeroMessageHandleBase* handle) { | |
|
oystein (OOO til 10th of July)
2016/07/20 21:19:37
Maybe wrap the whole function in DCHECK_IS_ON(), s
Primiano Tucci (use gerrit)
2016/07/21 10:50:03
Done.
| |
| 58 #if DCHECK_IS_ON() | |
| 59 handle_ = handle; | |
| 60 #endif | |
| 61 } | |
| 62 | |
| 51 protected: | 63 protected: |
| 52 ProtoZeroMessage(); | 64 ProtoZeroMessage(); |
| 53 | 65 |
| 54 // Clears up the state, allowing the message to be reused as a fresh one. | |
| 55 void Reset(ScatteredStreamWriter*); | |
| 56 | |
| 57 void AppendVarIntU64(uint32_t field_id, uint64_t value); | 66 void AppendVarIntU64(uint32_t field_id, uint64_t value); |
| 58 void AppendVarIntU32(uint32_t field_id, uint32_t value); | 67 void AppendVarIntU32(uint32_t field_id, uint32_t value); |
| 59 void AppendFloat(uint32_t field_id, float value); | 68 void AppendFloat(uint32_t field_id, float value); |
| 60 void AppendDouble(uint32_t field_id, double value); | 69 void AppendDouble(uint32_t field_id, double value); |
| 61 void AppendString(uint32_t field_id, const char* str); | 70 void AppendString(uint32_t field_id, const char* str); |
| 62 void AppendBytes(uint32_t field_id, const void* value, size_t size); | 71 void AppendBytes(uint32_t field_id, const void* value, size_t size); |
| 63 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding. | 72 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding. |
| 64 | 73 |
| 65 // Begins a nested message, using the static storage provided by the parent | 74 // Begins a nested message, using the static storage provided by the parent |
| 66 // class (see comment in |nested_messages_arena_|). The nested message ends | 75 // class (see comment in |nested_messages_arena_|). The nested message ends |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 80 BeginNestedMessageInternal(field_id, message); | 89 BeginNestedMessageInternal(field_id, message); |
| 81 return message; | 90 return message; |
| 82 } | 91 } |
| 83 | 92 |
| 84 private: | 93 private: |
| 85 friend class ProtoZeroMessageTest; | 94 friend class ProtoZeroMessageTest; |
| 86 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); | 95 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); |
| 87 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); | 96 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); |
| 88 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); | 97 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); |
| 89 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); | 98 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); |
| 99 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, MessageHandle); | |
| 90 | 100 |
| 91 enum : uint32_t { kMaxNestingDepth = 8 }; | 101 enum : uint32_t { kMaxNestingDepth = 8 }; |
| 92 | 102 |
| 93 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); | 103 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); |
| 94 | 104 |
| 95 // Called by Finalize and Append* methods. | 105 // Called by Finalize and Append* methods. |
| 96 void EndNestedMessage(); | 106 void EndNestedMessage(); |
| 97 | 107 |
| 98 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { | 108 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { |
| 99 #if DCHECK_IS_ON() | 109 #if DCHECK_IS_ON() |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 118 size_t size_already_written_; | 128 size_t size_already_written_; |
| 119 | 129 |
| 120 // Used to detect attemps to create messages with a nesting level > | 130 // Used to detect attemps to create messages with a nesting level > |
| 121 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages. | 131 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages. |
| 122 uint32_t nesting_depth_; | 132 uint32_t nesting_depth_; |
| 123 | 133 |
| 124 #if DCHECK_IS_ON() | 134 #if DCHECK_IS_ON() |
| 125 // When true, no more changes to the message are allowed. This is to DCHECK | 135 // When true, no more changes to the message are allowed. This is to DCHECK |
| 126 // attempts of writing to a message which has been Finalize()-d. | 136 // attempts of writing to a message which has been Finalize()-d. |
| 127 bool sealed_; | 137 bool sealed_; |
| 138 | |
| 139 ProtoZeroMessageHandleBase* handle_; | |
| 128 #endif | 140 #endif |
| 129 | 141 |
| 130 // Pointer to the last child message created through BeginNestedMessage(), if | 142 // Pointer to the last child message created through BeginNestedMessage(), if |
| 131 // any. nullptr otherwise. There is no need to keep track of more than one | 143 // any. nullptr otherwise. There is no need to keep track of more than one |
| 132 // message per nesting level as the proto-zero API contract mandates that | 144 // message per nesting level as the proto-zero API contract mandates that |
| 133 // nested fields can be filled only in a stacked fashion. In other words, | 145 // nested fields can be filled only in a stacked fashion. In other words, |
| 134 // nested messages are finalized and sealed when any other field is set in the | 146 // nested messages are finalized and sealed when any other field is set in the |
| 135 // parent message (or the parent message itself is finalized) and cannot be | 147 // parent message (or the parent message itself is finalized) and cannot be |
| 136 // accessed anymore afterwards. | 148 // accessed anymore afterwards. |
| 137 ProtoZeroMessage* nested_message_; | 149 ProtoZeroMessage* nested_message_; |
| 138 | 150 |
| 139 // The root message owns the storage for all its nested messages, up to a max | 151 // The root message owns the storage for all its nested messages, up to a max |
| 140 // of kMaxNestingDepth levels (see the .cc file). Note that the boundaries of | 152 // of kMaxNestingDepth levels (see the .cc file). Note that the boundaries of |
| 141 // the arena are meaningful only for the root message. The static_assert in | 153 // the arena are meaningful only for the root message. The static_assert in |
| 142 // the .cc file guarantees that the sizeof(nested_messages_arena_) is enough | 154 // the .cc file guarantees that the sizeof(nested_messages_arena_) is enough |
| 143 // to contain up to kMaxNestingDepth messages. | 155 // to contain up to kMaxNestingDepth messages. |
| 144 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512]; | 156 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512]; |
| 145 | 157 |
| 146 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of | 158 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of |
| 147 // nested messages would overflow the storage allocated by the root message. | 159 // nested messages would overflow the storage allocated by the root message. |
| 148 | 160 |
| 149 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); | 161 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); |
| 150 }; | 162 }; |
| 151 | 163 |
| 152 } // namespace v2 | 164 } // namespace v2 |
| 153 } // namespace tracing | 165 } // namespace tracing |
| 154 | 166 |
| 155 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | 167 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| OLD | NEW |