Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | |
| 6 #define COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/template_util.h" | |
| 15 #include "components/tracing/core/scattered_stream_writer.h" | |
| 16 #include "components/tracing/tracing_export.h" | |
| 17 | |
| 18 namespace tracing { | |
| 19 namespace v2 { | |
| 20 | |
| 21 // Base class extended by the proto C++ stubs generated by the ProtoZero | |
| 22 // compiler (see //components/tracing/tools/). This class provides the minimal | |
| 23 // runtime required to support append-only operations and is desiged for | |
| 24 // performance. None of the methods require any dynamic memory allocation. | |
| 25 class TRACING_EXPORT ProtoZeroMessage { | |
| 26 public: | |
| 27 // Commits all the changes to the buffer (backfills the size field of this and | |
| 28 // all nested messages) and seals the message. Returns the size of the message | |
| 29 // (and all nested sub-messages), without taking into account any chunking. | |
| 30 // Finalize is idempotent and can be called several times w/o side effects. | |
| 31 size_t Finalize(); | |
| 32 | |
| 33 // Optional. If is_valid() the corresponding memory region (which length | |
|
petrcermak
2016/07/12 10:18:47
In what sense is it "Optional"? Is it always prese
petrcermak
2016/07/12 10:18:47
"which length" is very difficult to understand. "w
petrcermak
2016/07/12 10:18:47
nit: add a comma after "is_valid()"
Primiano Tucci (use gerrit)
2016/07/12 17:25:16
Done.
Primiano Tucci (use gerrit)
2016/07/12 17:25:16
Optional in the sense that you can not call set_si
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
its should make it clearer.
| |
| 34 // necessarily == proto::kMessageLengthFieldSize) is backfilled with the size | |
| 35 // of this message (minus |size_already_written| below) when the message is | |
| 36 // finalized. This is the mechanism used by messages to backfill their | |
| 37 // corresponding size field in the parent message. | |
| 38 ContiguousMemoryRange size_field() const { return size_field_; } | |
| 39 void set_size_field(const ContiguousMemoryRange& reserved_range) { | |
| 40 size_field_ = reserved_range; | |
| 41 } | |
| 42 | |
| 43 // This is to deal with case of backfilling the size of a root (non-nested) | |
| 44 // message which is split into multiple chunks. Upon finalization only the | |
| 45 // partial size that lies in the last chunk has to be backfilled. | |
| 46 void inc_size_already_written(size_t size) { size_already_written_ += size; } | |
| 47 | |
| 48 protected: | |
| 49 ProtoZeroMessage(); | |
| 50 | |
| 51 // Clears up the state, allowing the message to be reused as a fresh one. | |
| 52 void Reset(ScatteredStreamWriter*); | |
| 53 | |
| 54 void AppendVarIntU64(uint32_t field_id, uint64_t value); | |
| 55 void AppendVarIntU32(uint32_t field_id, uint32_t value); | |
| 56 void AppendFloat(uint32_t field_id, float value); | |
| 57 void AppendDouble(uint32_t field_id, double value); | |
| 58 void AppendString(uint32_t field_id, const char* str); | |
| 59 void AppendBytes(uint32_t field_id, const void* value, size_t size); | |
| 60 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding. | |
| 61 | |
| 62 // Begins a nested message, using the static storage provided by the parent | |
| 63 // class (see comment in |nested_messages_arena_|). The nested message ends | |
| 64 // either when Finalize() is called or when any other Append* method is called | |
| 65 // in the parent class. | |
| 66 // The template argument T is supposed to be a stub class auto generated from | |
| 67 // a .proto, hence a subclass of ProtoZeroMessage. | |
| 68 template <class T> | |
| 69 T* BeginNestedMessage(uint32_t field_id) { | |
| 70 T* message = reinterpret_cast<T*>(nested_messages_arena_); | |
|
alph
2016/07/12 00:20:42
How about adding a static assert here that sizeof(
Primiano Tucci (use gerrit)
2016/07/12 10:15:32
Oh yes definitely, we really need this to prevent
| |
| 71 BeginNestedMessageInternal(field_id, message); | |
| 72 return message; | |
| 73 } | |
| 74 | |
| 75 void EndNestedMessage(); | |
|
petrcermak
2016/07/12 10:18:47
Maybe add a comment saying that this is called by
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
Done. actually realized that this should be privat
| |
| 76 | |
| 77 private: | |
| 78 friend class ProtoZeroMessageTest; | |
| 79 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); | |
| 80 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); | |
| 81 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); | |
| 82 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); | |
| 83 | |
| 84 enum : uint32_t { kMaxNestingDepth = 8 }; | |
| 85 | |
| 86 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); | |
| 87 | |
| 88 inline void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { | |
| 89 const size_t size = static_cast<size_t>(src_end - src_begin); | |
| 90 stream_writer_->WriteBytes(src_begin, size); | |
| 91 size_ += size; | |
| 92 } | |
| 93 | |
| 94 // Only PODs fields are allowed. This class' dtor is never called. | |
|
petrcermak
2016/07/12 10:18:47
supernit: From what I could find, "class's" is pre
petrcermak
2016/07/12 10:18:47
nit: "POD fields" (no need for the double plural)
Primiano Tucci (use gerrit)
2016/07/12 17:25:16
Done.
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
"The Elements of Style and the Canadian Press Styl
petrcermak
2016/07/12 17:41:17
:-D :-D :-D
| |
| 95 // See the comment on the static_assert in the the corresponding .cc file. | |
| 96 | |
| 97 // The stream writer interface used for the serialization. | |
| 98 ScatteredStreamWriter* stream_writer_; | |
| 99 | |
| 100 // Keeps track of the size of the current message. | |
| 101 size_t size_; | |
| 102 | |
| 103 ContiguousMemoryRange size_field_; | |
| 104 size_t size_already_written_; | |
| 105 | |
| 106 // Used to detect attemps to create messages with a nesting level > | |
| 107 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages. | |
| 108 uint32_t nesting_depth_; | |
| 109 | |
| 110 #if DCHECK_IS_ON() | |
| 111 // When true no more changes to the message are allowed. This is to DCHECK | |
|
petrcermak
2016/07/12 10:18:47
nit: add comma after "true"
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
Done.
| |
| 112 // attempts of writing to a message which has been Finalize()-d. | |
| 113 bool sealed_; | |
| 114 #endif | |
| 115 | |
| 116 // Pointer to the last child message created through BeginNestedMessage(), if | |
| 117 // any. nullptr otherwise. There is no need to keep track of more than one | |
| 118 // message per nesting level as he proto-zero API contract mandates that | |
|
petrcermak
2016/07/12 10:18:47
s/as he/as the/
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
Done.
| |
| 119 // nested fields can be filled only in a stacked fashion. In othwe words, | |
|
petrcermak
2016/07/12 10:18:47
s/othwe/other/ (one key to the right :-D)
Primiano Tucci (use gerrit)
2016/07/12 17:25:16
Done.
| |
| 120 // nested messages are finalized and sealed when any other field is set in the | |
| 121 // parent messag (or the parent message itself is finalized) and cannot be | |
|
petrcermak
2016/07/12 10:18:47
nit: s/messag\b/message/
Primiano Tucci (use gerrit)
2016/07/12 17:25:16
Done.
| |
| 122 // accessed anymore after that point. | |
|
petrcermak
2016/07/12 10:18:47
nit: s/after that point/afterwards/
Primiano Tucci (use gerrit)
2016/07/12 17:25:16
Done.
| |
| 123 ProtoZeroMessage* nested_message_; | |
| 124 | |
| 125 // The root message owns the storage for all its nested messages, up to a max | |
| 126 // of kMaxNestingDepth levels (see the .cc file). Note that the boundaries of | |
| 127 // the arena are meaningful only for the root message. The static_assert in | |
| 128 // the .cc file guarantees that the sizeof(nested_messages_arena_) is enough | |
| 129 // to contain up to kMaxNestingDepth messages. | |
| 130 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512]; | |
| 131 | |
| 132 // DO NOT add any fields below nested_messages_arena_. The memory layout of | |
|
petrcermak
2016/07/12 10:18:47
shouldn't this be "|nested_message_arena|"?
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
Done.
| |
| 133 // nested messaged would overflow the storage allocated by the root message. | |
|
petrcermak
2016/07/12 10:18:47
s/messaged/messages/
Primiano Tucci (use gerrit)
2016/07/12 17:25:17
Done.
| |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); | |
| 136 }; | |
| 137 | |
| 138 } // namespace v2 | |
| 139 } // namespace tracing | |
| 140 | |
| 141 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ | |
| OLD | NEW |