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

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: Created 4 years, 6 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
« no previous file with comments | « components/tracing/BUILD.gn ('k') | components/tracing/core/proto_utils_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..06d1d9e81722ee581ed8979d8363533a04c27a52
--- /dev/null
+++ b/components/tracing/core/proto_utils.h
@@ -0,0 +1,87 @@
+// 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,
+ kFieldTypeLengthLimited = 2,
kraynov 2016/07/05 11:25:09 ...LengthDelimited For consistency with https://de
Primiano Tucci (use gerrit) 2016/07/06 16:52:55 Done.
+ 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) {
kraynov 2016/07/05 11:25:09 ditto Delimited
Primiano Tucci (use gerrit) 2016/07/06 16:52:55 Done.
+ return (field_id << 3) | kFieldTypeLengthLimited;
+}
+
+// 64-bit fixed-length field types: fixed32, sfixed32, float.
kraynov 2016/06/22 16:38:32 nit: 32
Primiano Tucci (use gerrit) 2016/07/06 16:52:54 Done.
+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) {
+ while (value >= 0x80) {
+ *target = static_cast<uint8_t>(value | 0x80);
+ 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.
kraynov 2016/06/22 16:38:32 WriteVarIntS32 (S for zig-zag) WriteVarIntS64 Writ
Primiano Tucci (use gerrit) 2016/07/06 16:52:54 Yup, correct. But no rush, we won't need them unti
+
+// 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.
+inline void WriteRedundantVarIntUnsigned(uint32_t value,
oystein (OOO til 10th of July) 2016/06/15 08:45:44 Can you expand a bit on why this is necessary? Fix
Primiano Tucci (use gerrit) 2016/06/15 09:02:12 Right. So the problem here is that when you have l
+ size_t length,
+ 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_
« no previous file with comments | « components/tracing/BUILD.gn ('k') | components/tracing/core/proto_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698