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_UTILS_H_ | |
| 6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | |
| 7 | |
| 8 #include <inttypes.h> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "components/tracing/tracing_export.h" | |
| 13 | |
| 14 namespace tracing { | |
| 15 namespace v2 { | |
| 16 namespace proto { | |
| 17 | |
| 18 // See https://developers.google.com/protocol-buffers/docs/encoding wire types. | |
| 19 | |
| 20 enum : uint32_t { | |
| 21 kFieldTypeVarInt = 0, | |
| 22 kFieldTypeFixed64 = 1, | |
| 23 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.
| |
| 24 kFieldTypeFixed32 = 5, | |
| 25 }; | |
| 26 | |
| 27 // Variable-length field types: (s)int32, (s)int64, bool, enum. | |
| 28 inline uint32_t MakeTagVarInt(uint32_t field_id) { | |
| 29 return (field_id << 3) | kFieldTypeVarInt; | |
| 30 } | |
| 31 | |
| 32 // Length-limited field types: string, bytes, embedded messages. | |
| 33 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.
| |
| 34 return (field_id << 3) | kFieldTypeLengthLimited; | |
| 35 } | |
| 36 | |
| 37 // 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.
| |
| 38 inline uint32_t MakeTagFixed32(uint32_t field_id) { | |
| 39 return (field_id << 3) | kFieldTypeFixed32; | |
| 40 } | |
| 41 | |
| 42 // 64-bit fixed-length field types: fixed64, sfixed64, double. | |
| 43 inline uint32_t MakeTagFixed64(uint32_t field_id) { | |
| 44 return (field_id << 3) | kFieldTypeFixed64; | |
| 45 } | |
| 46 | |
| 47 template <typename T> | |
| 48 inline uint8_t* WriteVarIntInternal(T value, uint8_t* target) { | |
| 49 while (value >= 0x80) { | |
| 50 *target = static_cast<uint8_t>(value | 0x80); | |
| 51 value >>= 7; | |
| 52 ++target; | |
| 53 } | |
| 54 *target = static_cast<uint8_t>(value); | |
| 55 return target + 1; | |
| 56 } | |
| 57 | |
| 58 inline uint8_t* WriteVarIntU32(uint32_t value, uint8_t* target) { | |
| 59 return WriteVarIntInternal<uint32_t>(value, target); | |
| 60 } | |
| 61 | |
| 62 inline uint8_t* WriteVarIntU64(uint64_t value, uint8_t* target) { | |
| 63 return WriteVarIntInternal<uint64_t>(value, target); | |
| 64 } | |
| 65 | |
| 66 // 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
| |
| 67 | |
| 68 // Writes a fixed-size redundant encoding of the given |value|. This is | |
| 69 // used to backfill fixed-size reservations for the length field using a | |
| 70 // non-canonical varint encoding (e.g. \x81\x80\x80\x00 instead of \x01). | |
| 71 // See https://github.com/google/protobuf/issues/1530. | |
| 72 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
| |
| 73 size_t length, | |
| 74 uint8_t* buf) { | |
| 75 for (size_t i = 0; i < length; ++i) { | |
| 76 const uint8_t msb = (i < length - 1) ? 0x80 : 0; | |
| 77 buf[i] = static_cast<uint8_t>((value & 0x7F) | msb); | |
| 78 value >>= 7; | |
| 79 } | |
| 80 DCHECK_EQ(0u, value) << "Buffer too short to encode the given value"; | |
| 81 } | |
| 82 | |
| 83 } // namespace proto | |
| 84 } // namespace v2 | |
| 85 } // namespace tracing | |
| 86 | |
| 87 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | |
| OLD | NEW |