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

Unified Diff: components/tracing/core/proto_utils.h

Issue 2043913006: tracing v2: Add proto_utils.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@tv_ups2
Patch Set: address review comments 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 side-by-side diff with in-line comments
Download patch
Index: components/tracing/core/proto_utils.h
diff --git a/components/tracing/core/proto_utils.h b/components/tracing/core/proto_utils.h
new file mode 100644
index 0000000000000000000000000000000000000000..76854398d9a6c08056f12c6fba1607c148c67d72
--- /dev/null
+++ b/components/tracing/core/proto_utils.h
@@ -0,0 +1,90 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
+#define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
+
+#include <inttypes.h>
+
+#include "base/logging.h"
+#include "base/macros.h"
+#include "components/tracing/tracing_export.h"
+
+namespace tracing {
+namespace v2 {
+namespace proto {
+
+// See https://developers.google.com/protocol-buffers/docs/encoding wire types.
+
+enum : uint32_t {
+ kFieldTypeVarInt = 0,
+ kFieldTypeFixed64 = 1,
+ kFieldTypeLengthDelimited = 2,
+ kFieldTypeFixed32 = 5,
+};
+
+// Variable-length field types: (s)int32, (s)int64, bool, enum.
+inline uint32_t MakeTagVarInt(uint32_t field_id) {
+ return (field_id << 3) | kFieldTypeVarInt;
+}
+
+// Length-limited field types: string, bytes, embedded messages.
+inline uint32_t MakeTagLengthLimited(uint32_t field_id) {
+ return (field_id << 3) | kFieldTypeLengthDelimited;
+}
+
+// 32-bit fixed-length field types: fixed32, sfixed32, float.
+inline uint32_t MakeTagFixed32(uint32_t field_id) {
+ return (field_id << 3) | kFieldTypeFixed32;
+}
+
+// 64-bit fixed-length field types: fixed64, sfixed64, double.
+inline uint32_t MakeTagFixed64(uint32_t field_id) {
+ return (field_id << 3) | kFieldTypeFixed64;
+}
+
+template <typename T>
+inline uint8_t* WriteVarIntInternal(T value, uint8_t* target) {
alph 2016/07/06 17:29:52 Maybe add a static check that T is unsigned?
Primiano Tucci (use gerrit) 2016/07/07 12:59:25 Done.
+ while (value >= 0x80) {
+ *target = static_cast<uint8_t>(value | 0x80);
alph 2016/07/06 17:29:52 nit: *target++ =
Primiano Tucci (use gerrit) 2016/07/07 12:59:25 Done.
+ value >>= 7;
+ ++target;
+ }
+ *target = static_cast<uint8_t>(value);
+ return target + 1;
+}
+
+inline uint8_t* WriteVarIntU32(uint32_t value, uint8_t* target) {
+ return WriteVarIntInternal<uint32_t>(value, target);
+}
+
+inline uint8_t* WriteVarIntU64(uint64_t value, uint8_t* target) {
+ return WriteVarIntInternal<uint64_t>(value, target);
+}
+
+// TODO(kraynov): add support for signed integers and zig-zag encoding.
+
+// Writes a fixed-size redundant encoding of the given |value|. This is
+// used to backfill fixed-size reservations for the length field using a
+// non-canonical varint encoding (e.g. \x81\x80\x80\x00 instead of \x01).
+// See https://github.com/google/protobuf/issues/1530.
+// Concretely this is used when writing a nested message, where the size of
+// nested message is not known until it is ended. In this case a fixed amount of
+// bytes are reserved to encode its size field and backfilled at the end.
+inline void WriteRedundantVarIntUnsigned(uint32_t value,
+ size_t length,
alph 2016/07/06 17:29:52 As it seems to be a hot path, I'd better make it a
Primiano Tucci (use gerrit) 2016/07/07 12:59:25 I was planning to do a final perf-optimization pas
+ uint8_t* buf) {
+ for (size_t i = 0; i < length; ++i) {
+ const uint8_t msb = (i < length - 1) ? 0x80 : 0;
+ buf[i] = static_cast<uint8_t>((value & 0x7F) | msb);
+ value >>= 7;
+ }
+ DCHECK_EQ(0u, value) << "Buffer too short to encode the given value";
+}
+
+} // namespace proto
+} // namespace v2
+} // namespace tracing
+
+#endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_

Powered by Google App Engine
This is Rietveld 408576698