| 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 <type_traits> |
| 11 |
| 12 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/template_util.h" |
| 17 #include "build/build_config.h" |
| 18 #include "components/tracing/core/scattered_stream_writer.h" |
| 19 #include "components/tracing/tracing_export.h" |
| 20 |
| 21 namespace tracing { |
| 22 namespace v2 { |
| 23 |
| 24 // Base class extended by the proto C++ stubs generated by the ProtoZero |
| 25 // compiler (see //components/tracing/tools/). This class provides the minimal |
| 26 // runtime required to support append-only operations and is desiged for |
| 27 // performance. None of the methods require any dynamic memory allocation. |
| 28 class TRACING_EXPORT ProtoZeroMessage { |
| 29 public: |
| 30 // 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 |
| 32 // (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. |
| 34 size_t Finalize(); |
| 35 |
| 36 // Optional. If is_valid() == true, the corresponding memory region (its |
| 37 // length == proto::kMessageLengthFieldSize) is backfilled with the size of |
| 38 // this message (minus |size_already_written| below) when the message is |
| 39 // finalized. This is the mechanism used by messages to backfill their |
| 40 // corresponding size field in the parent message. |
| 41 ContiguousMemoryRange size_field() const { return size_field_; } |
| 42 void set_size_field(const ContiguousMemoryRange& reserved_range) { |
| 43 size_field_ = reserved_range; |
| 44 } |
| 45 |
| 46 // 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 |
| 48 // 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; } |
| 50 |
| 51 protected: |
| 52 ProtoZeroMessage(); |
| 53 |
| 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); |
| 58 void AppendVarIntU32(uint32_t field_id, uint32_t value); |
| 59 void AppendFloat(uint32_t field_id, float value); |
| 60 void AppendDouble(uint32_t field_id, double value); |
| 61 void AppendString(uint32_t field_id, const char* str); |
| 62 void AppendBytes(uint32_t field_id, const void* value, size_t size); |
| 63 // TODO(kraynov): implement AppendVarIntS32/64(...) w/ zig-zag encoding. |
| 64 |
| 65 // Begins a nested message, using the static storage provided by the parent |
| 66 // class (see comment in |nested_messages_arena_|). The nested message ends |
| 67 // either when Finalize() is called or when any other Append* method is called |
| 68 // in the parent class. |
| 69 // The template argument T is supposed to be a stub class auto generated from |
| 70 // a .proto, hence a subclass of ProtoZeroMessage. |
| 71 template <class T> |
| 72 T* BeginNestedMessage(uint32_t field_id) { |
| 73 // This is to prevent subclasses (which should be autogenerated, though), to |
| 74 // introduce extra state fields (which wouldn't be initialized by Reset()). |
| 75 static_assert(std::is_base_of<ProtoZeroMessage, T>::value, |
| 76 "T must be a subclass of ProtoZeroMessage"); |
| 77 static_assert(sizeof(T) == sizeof(ProtoZeroMessage), |
| 78 "ProtoZeroMessage subclasses cannot introduce extra state."); |
| 79 T* message = reinterpret_cast<T*>(nested_messages_arena_); |
| 80 BeginNestedMessageInternal(field_id, message); |
| 81 return message; |
| 82 } |
| 83 |
| 84 private: |
| 85 friend class ProtoZeroMessageTest; |
| 86 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BasicTypesNoNesting); |
| 87 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, BackfillSizeOnFinalization); |
| 88 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, NestedMessagesSimple); |
| 89 FRIEND_TEST_ALL_PREFIXES(ProtoZeroMessageTest, StressTest); |
| 90 |
| 91 enum : uint32_t { kMaxNestingDepth = 8 }; |
| 92 |
| 93 void BeginNestedMessageInternal(uint32_t field_id, ProtoZeroMessage*); |
| 94 |
| 95 // Called by Finalize and Append* methods. |
| 96 void EndNestedMessage(); |
| 97 |
| 98 void WriteToStream(const uint8_t* src_begin, const uint8_t* src_end) { |
| 99 #if DCHECK_IS_ON() |
| 100 DCHECK(!sealed_); |
| 101 #endif |
| 102 DCHECK(src_begin < src_end); |
| 103 const size_t size = static_cast<size_t>(src_end - src_begin); |
| 104 stream_writer_->WriteBytes(src_begin, size); |
| 105 size_ += size; |
| 106 } |
| 107 |
| 108 // Only POD fields are allowed. This class's dtor is never called. |
| 109 // See the comment on the static_assert in the the corresponding .cc file. |
| 110 |
| 111 // The stream writer interface used for the serialization. |
| 112 ScatteredStreamWriter* stream_writer_; |
| 113 |
| 114 // Keeps track of the size of the current message. |
| 115 size_t size_; |
| 116 |
| 117 ContiguousMemoryRange size_field_; |
| 118 size_t size_already_written_; |
| 119 |
| 120 // Used to detect attemps to create messages with a nesting level > |
| 121 // kMaxNestingDepth. |nesting_depth_| == 0 for root (non-nested) messages. |
| 122 uint32_t nesting_depth_; |
| 123 |
| 124 #if DCHECK_IS_ON() |
| 125 // 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. |
| 127 bool sealed_; |
| 128 #endif |
| 129 |
| 130 // 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 |
| 132 // 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, |
| 134 // 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 |
| 136 // accessed anymore afterwards. |
| 137 ProtoZeroMessage* nested_message_; |
| 138 |
| 139 // 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 |
| 141 // 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 |
| 143 // to contain up to kMaxNestingDepth messages. |
| 144 ALIGNAS(sizeof(void*)) uint8_t nested_messages_arena_[512]; |
| 145 |
| 146 // 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. |
| 148 |
| 149 DISALLOW_COPY_AND_ASSIGN(ProtoZeroMessage); |
| 150 }; |
| 151 |
| 152 } // namespace v2 |
| 153 } // namespace tracing |
| 154 |
| 155 #endif // COMPONENTS_TRACING_CORE_PROTO_ZERO_MESSAGE_H_ |
| OLD | NEW |