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 #include "components/tracing/core/proto_utils.h" | |
petrcermak
2016/09/08 16:13:25
Is there a particular reason for this import order
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
yes:
the first include is the corresponding .h fil
| |
6 | |
7 #include <limits> | |
8 | |
9 #include "base/sys_byteorder.h" | |
10 | |
11 #define CHECK_PTR_LE(a,b) CHECK_LE(reinterpret_cast<const void*>(a), \ | |
12 reinterpret_cast<const void*>(b)) | |
13 namespace tracing { | |
14 namespace v2 { | |
15 namespace proto { | |
16 | |
17 const uint8_t* ParseVarInt(const uint8_t* start, | |
18 const uint8_t* end, | |
19 uint64_t* value) { | |
20 const uint8_t* pos = start; | |
21 uint64_t shift = 0; | |
22 *value = 0; | |
23 do { | |
24 CHECK_PTR_LE(pos, end - 1); | |
25 DCHECK_LT(shift, 64ull); | |
26 *value |= static_cast<uint64_t>(*pos & 0x7f) << shift; | |
27 shift += 7; | |
28 } while (*pos++ & 0x80); | |
petrcermak
2016/09/08 16:13:25
for readability, maybe add parentheses: *(pos++)
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
There are 260 cases of *xx++ [1] vs 56 of *(xx++)
| |
29 return pos; | |
30 } | |
31 | |
32 const uint8_t* ParseField(const uint8_t* start, | |
33 const uint8_t* end, | |
34 uint32_t* field_id, | |
35 FieldType* field_type, | |
36 uint64_t* field_intvalue) { | |
37 const uint8_t* pos = start; | |
38 CHECK_LT(pos, end); | |
petrcermak
2016/09/08 16:13:25
I know that you can use CHECK_LT here, but maybe u
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
Done.
| |
39 *field_type = static_cast<FieldType>(*pos & 7); | |
oystein (OOO til 10th of July)
2016/09/08 21:26:04
Hmm, the code embeds a lot of magic numbers here.
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
ok done and added some comments
| |
40 | |
41 uint64_t raw_field_id; | |
42 pos = ParseVarInt(pos, end, &raw_field_id); | |
43 raw_field_id >>= 3; | |
petrcermak
2016/09/08 16:13:25
It took me a while to understand what this is for.
oystein (OOO til 10th of July)
2016/09/08 21:26:04
Maybe use the same named constant in both cases to
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
ok ok used more names. Just Stockholm syndrome aft
| |
44 | |
45 DCHECK_LE(raw_field_id, std::numeric_limits<uint32_t>::max()); | |
46 *field_id = static_cast<uint32_t>(raw_field_id); | |
47 | |
48 switch (*field_type) { | |
49 case kFieldTypeFixed64: { | |
50 CHECK_PTR_LE(pos + 8, end); | |
51 memcpy(field_intvalue, pos, 8); | |
oystein (OOO til 10th of July)
2016/09/08 21:26:04
sizeof(uint64_t) for clarity?
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
Done.
| |
52 *field_intvalue = base::ByteSwapToLE64(*field_intvalue); | |
53 pos += 8; | |
oystein (OOO til 10th of July)
2016/09/08 21:26:04
Ditto
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
Done.
| |
54 break; | |
55 } | |
56 case kFieldTypeFixed32: { | |
57 CHECK_PTR_LE(pos + 4, end); | |
oystein (OOO til 10th of July)
2016/09/08 21:26:04
Ditto for uint32_t
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
Done.
| |
58 uint32_t tmp; | |
59 memcpy(&tmp, pos, 4); | |
60 *field_intvalue = base::ByteSwapToLE32(tmp); | |
61 pos += 4; | |
62 break; | |
63 } | |
64 case kFieldTypeVarInt: | |
65 case kFieldTypeLengthDelimited: { | |
66 pos = ParseVarInt(pos, end, field_intvalue); | |
67 if (*field_type == kFieldTypeLengthDelimited) { | |
petrcermak
2016/09/08 16:13:25
I think that readability might be worth the one du
Primiano Tucci (use gerrit)
2016/09/09 14:15:48
right makes sense. after various edits didn't rali
| |
68 pos += *field_intvalue; | |
69 CHECK_PTR_LE(pos, end); | |
70 } | |
71 break; | |
72 } | |
73 default: | |
74 NOTREACHED() << "Unsupported proto field type " << *field_type; | |
75 } | |
76 return pos; | |
77 } | |
78 | |
79 } // namespace proto | |
80 } // namespace v2 | |
81 } // namespace tracing | |
OLD | NEW |