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

Side by Side 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: alph+petr review 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_utils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_UTILS_H_
6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
7
8 #include <inttypes.h>
9
10 #include <type_traits>
11
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "components/tracing/tracing_export.h"
15
16 namespace tracing {
17 namespace v2 {
18 namespace proto {
19
20 // See https://developers.google.com/protocol-buffers/docs/encoding wire types.
21
22 enum : uint32_t {
23 kFieldTypeVarInt = 0,
24 kFieldTypeFixed64 = 1,
25 kFieldTypeLengthDelimited = 2,
26 kFieldTypeFixed32 = 5,
27 };
28
29 // Variable-length field types: (s)int32, (s)int64, bool, enum.
30 inline uint32_t MakeTagVarInt(uint32_t field_id) {
31 return (field_id << 3) | kFieldTypeVarInt;
32 }
33
34 // Length-limited field types: string, bytes, embedded messages.
35 inline uint32_t MakeTagLengthLimited(uint32_t field_id) {
36 return (field_id << 3) | kFieldTypeLengthDelimited;
37 }
38
39 // 32-bit fixed-length field types: fixed32, sfixed32, float.
40 inline uint32_t MakeTagFixed32(uint32_t field_id) {
41 return (field_id << 3) | kFieldTypeFixed32;
42 }
43
44 // 64-bit fixed-length field types: fixed64, sfixed64, double.
45 inline uint32_t MakeTagFixed64(uint32_t field_id) {
46 return (field_id << 3) | kFieldTypeFixed64;
47 }
48
49 template <typename T>
50 inline uint8_t* WriteVarIntInternal(T value, uint8_t* target) {
51 static_assert(std::is_unsigned<T>::value, "value must be unsigned");
52 while (value >= 0x80) {
53 *target++ = static_cast<uint8_t>(value | 0x80);
54 value >>= 7;
55 }
56 *target = static_cast<uint8_t>(value);
57 return target + 1;
58 }
59
60 inline uint8_t* WriteVarIntU32(uint32_t value, uint8_t* target) {
61 return WriteVarIntInternal<uint32_t>(value, target);
62 }
63
64 inline uint8_t* WriteVarIntU64(uint64_t value, uint8_t* target) {
65 return WriteVarIntInternal<uint64_t>(value, target);
66 }
67
68 // TODO(kraynov): add support for signed integers and zig-zag encoding.
69
70 // Writes a fixed-size redundant encoding of the given |value|. This is
71 // used to backfill fixed-size reservations for the length field using a
72 // non-canonical varint encoding (e.g. \x81\x80\x80\x00 instead of \x01).
73 // See https://github.com/google/protobuf/issues/1530.
74 // In particular, this is used for nested messages. The size of a nested message
75 // is not known until all its field have been written. A fixed amount of bytes
76 // are reserved to encode the size field and backfilled at the end.
petrcermak 2016/07/07 13:08:17 Is the semantics "reserved (...) and backfilled (.
petrcermak 2016/07/07 13:08:17 nit: "... amount ... IS reserved ... " (this is a
Primiano Tucci (use gerrit) 2016/07/07 15:48:38 The former: reserved (to encode the size field) an
Primiano Tucci (use gerrit) 2016/07/07 15:48:38 I knew you would have pointed this out. I am not s
77 template <size_t LENGTH>
78 void WriteRedundantVarIntU32(uint32_t value, uint8_t* buf) {
79 for (size_t i = 0; i < LENGTH; ++i) {
80 const uint8_t msb = (i < LENGTH - 1) ? 0x80 : 0;
81 buf[i] = static_cast<uint8_t>((value & 0x7F) | msb);
82 value >>= 7;
83 }
84 DCHECK_EQ(0u, value) << "Buffer too short to encode the given value";
85 }
86
87 } // namespace proto
88 } // namespace v2
89 } // namespace tracing
90
91 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_
OLDNEW
« 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