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 kFieldTypeLengthDelimited = 2, | |
| 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) { | |
|
petrcermak
2016/07/07 09:46:59
Does it really make sense to define this for each
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
So I conceptually agree. In practice the only reas
| |
| 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) { | |
| 34 return (field_id << 3) | kFieldTypeLengthDelimited; | |
| 35 } | |
| 36 | |
| 37 // 32-bit fixed-length field types: fixed32, sfixed32, float. | |
| 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) { | |
|
petrcermak
2016/07/07 09:46:59
Again, does it really make sense to define these?
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
Hmm here the reality is that due to the TODO(krayn
| |
| 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. | |
| 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 // Concretely this is used when writing a nested message, where the size of | |
|
petrcermak
2016/07/07 09:46:59
nit: comma after "Concretely". I'd personally pref
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
Done.
| |
| 73 // nested message is not known until it is ended. In this case a fixed amount of | |
|
petrcermak
2016/07/07 09:46:59
nit: comma after "In this case"
petrcermak
2016/07/07 09:46:59
supernit: s/ended/finished/
petrcermak
2016/07/07 09:47:00
nit: s/nested/the nested/
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
not sure about this but... done.
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
Done.
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
reworded all this a bit.
| |
| 74 // bytes are reserved to encode its size field and backfilled at the end. | |
| 75 void WriteRedundantVarIntUnsigned(uint32_t value, size_t length, uint8_t* buf) { | |
|
petrcermak
2016/07/07 09:46:59
Should the name be "VarInt" whe it's fixed-size? A
Primiano Tucci (use gerrit)
2016/07/07 12:59:25
Yeah I know it's an odd concept, but this is truly
| |
| 76 for (size_t i = 0; i < length; ++i) { | |
| 77 const uint8_t msb = (i < length - 1) ? 0x80 : 0; | |
| 78 buf[i] = static_cast<uint8_t>((value & 0x7F) | msb); | |
| 79 value >>= 7; | |
| 80 } | |
| 81 DCHECK_EQ(0u, value) << "Buffer too short to encode the given value"; | |
| 82 } | |
| 83 | |
| 84 } // namespace proto | |
| 85 } // namespace v2 | |
| 86 } // namespace tracing | |
| 87 | |
| 88 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | |
| OLD | NEW |