Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(673)

Side by Side Diff: components/tracing/core/proto_zero_message.h

Issue 2158323005: tracing v2: Introduce handles for safe usage of ProtoZeroMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final nits Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/tracing/BUILD.gn ('k') | components/tracing/core/proto_zero_message.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if DCHECK_IS_ON()
57 // Only for ProtoZeroMessageHandleBase.
58 void set_handle(ProtoZeroMessageHandleBase* handle) { handle_ = handle; }
59 #endif
60
51 protected: 61 protected:
52 ProtoZeroMessage(); 62 ProtoZeroMessage();
53 63
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); 64 void AppendVarIntU64(uint32_t field_id, uint64_t value);
58 void AppendVarIntU32(uint32_t field_id, uint32_t value); 65 void AppendVarIntU32(uint32_t field_id, uint32_t value);
59 void AppendFloat(uint32_t field_id, float value); 66 void AppendFloat(uint32_t field_id, float value);
60 void AppendDouble(uint32_t field_id, double value); 67 void AppendDouble(uint32_t field_id, double value);
61 void AppendString(uint32_t field_id, const char* str); 68 void AppendString(uint32_t field_id, const char* str);
62 void AppendBytes(uint32_t field_id, const void* value, size_t size); 69 void AppendBytes(uint32_t field_id, const void* value, size_t size);
63 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding. 70 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding.
64 71
65 // Begins a nested message, using the static storage provided by the parent 72 // Begins a nested message, using the static storage provided by the parent
66 // class (see comment in |nested_messages_arena_|). The nested message ends 73 // class (see comment in |nested_messages_arena_|). The nested message ends
(...skipping 13 matching lines...) Expand all
80 BeginNestedMessageInternal(field_id, message); 87 BeginNestedMessageInternal(field_id, message);
81 return message; 88 return message;
82 } 89 }
83 90
84 private: 91 private:
85 friend class ProtoZeroMessageTest; 92 friend class ProtoZeroMessageTest;
86 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); 93 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting);
87 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); 94 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization);
88 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); 95 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple);
89 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); 96 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest);
97 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, MessageHandle);
90 98
91 enum : uint32_t { kMaxNestingDepth = 8 }; 99 enum : uint32_t { kMaxNestingDepth = 8 };
92 100
93 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); 101 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*);
94 102
95 // Called by Finalize and Append* methods. 103 // Called by Finalize and Append* methods.
96 void EndNestedMessage(); 104 void EndNestedMessage();
97 105
98 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { 106 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) {
99 #if DCHECK_IS_ON() 107 #if DCHECK_IS_ON()
(...skipping 18 matching lines...) Expand all
118 size_t size_already_written_; 126 size_t size_already_written_;
119 127
120 // Used to detect attemps to create messages with a nesting level > 128 // Used to detect attemps to create messages with a nesting level >
121 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages. 129 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages.
122 uint32_t nesting_depth_; 130 uint32_t nesting_depth_;
123 131
124 #if DCHECK_IS_ON() 132 #if DCHECK_IS_ON()
125 // When true, no more changes to the message are allowed. This is to DCHECK 133 // 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. 134 // attempts of writing to a message which has been Finalize()-d.
127 bool sealed_; 135 bool sealed_;
136
137 ProtoZeroMessageHandleBase* handle_;
128 #endif 138 #endif
129 139
130 // Pointer to the last child message created through BeginNestedMessage(), if 140 // 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 141 // 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 142 // 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, 143 // 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 144 // 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 145 // parent message (or the parent message itself is finalized) and cannot be
136 // accessed anymore afterwards. 146 // accessed anymore afterwards.
137 ProtoZeroMessage* nested_message_; 147 ProtoZeroMessage* nested_message_;
138 148
139 // The root message owns the storage for all its nested messages, up to a max 149 // 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 150 // 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 151 // 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 152 // the .cc file guarantees that the sizeof(nested_messages_arena_) is enough
143 // to contain up to kMaxNestingDepth messages. 153 // to contain up to kMaxNestingDepth messages.
144 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512]; 154 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512];
145 155
146 // DO NOT add any fields below |nested_messages_arena_|. The memory layout of 156 // 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. 157 // nested messages would overflow the storage allocated by the root message.
148 158
149 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); 159 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage);
150 }; 160 };
151 161
152 } // namespace v2 162 } // namespace v2
153 } // namespace tracing 163 } // namespace tracing
154 164
155 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ 165 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_
OLDNEW
« no previous file with comments | « components/tracing/BUILD.gn ('k') | components/tracing/core/proto_zero_message.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698