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) { | |
| 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) { | |
|
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.
| |
| 49 while (value >= 0x80) { | |
| 50 *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.
| |
| 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. | |
| 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 | |
| 73 // nested message is not known until it is ended. In this case a fixed amount of | |
| 74 // bytes are reserved to encode its size field and backfilled at the end. | |
| 75 inline void WriteRedundantVarIntUnsigned(uint32_t value, | |
| 76 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
| |
| 77 uint8_t* buf) { | |
| 78 for (size_t i = 0; i < length; ++i) { | |
| 79 const uint8_t msb = (i < length - 1) ? 0x80 : 0; | |
| 80 buf[i] = static_cast<uint8_t>((value & 0x7F) | msb); | |
| 81 value >>= 7; | |
| 82 } | |
| 83 DCHECK_EQ(0u, value) << "Buffer too short to encode the given value"; | |
| 84 } | |
| 85 | |
| 86 } // namespace proto | |
| 87 } // namespace v2 | |
| 88 } // namespace tracing | |
| 89 | |
| 90 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | |
| OLD | NEW |