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

Side by Side Diff: net/quic/quic_data_writer.cc

Issue 1535363003: Switch to standard integer types in net/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: stddef Created 5 years 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 | « net/quic/quic_data_writer.h ('k') | net/quic/quic_data_writer_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/quic_data_writer.h" 5 #include "net/quic/quic_data_writer.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <limits> 9 #include <limits>
10 10
11 using base::StringPiece; 11 using base::StringPiece;
12 using std::numeric_limits; 12 using std::numeric_limits;
13 13
14 namespace net { 14 namespace net {
15 15
16 QuicDataWriter::QuicDataWriter(size_t size, char* buffer) 16 QuicDataWriter::QuicDataWriter(size_t size, char* buffer)
17 : buffer_(buffer), capacity_(size), length_(0) {} 17 : buffer_(buffer), capacity_(size), length_(0) {}
18 18
19 QuicDataWriter::~QuicDataWriter() {} 19 QuicDataWriter::~QuicDataWriter() {}
20 20
21 char* QuicDataWriter::data() { 21 char* QuicDataWriter::data() {
22 return buffer_; 22 return buffer_;
23 } 23 }
24 24
25 bool QuicDataWriter::WriteUInt8(uint8 value) { 25 bool QuicDataWriter::WriteUInt8(uint8_t value) {
26 return WriteBytes(&value, sizeof(value)); 26 return WriteBytes(&value, sizeof(value));
27 } 27 }
28 28
29 bool QuicDataWriter::WriteUInt16(uint16 value) { 29 bool QuicDataWriter::WriteUInt16(uint16_t value) {
30 return WriteBytes(&value, sizeof(value)); 30 return WriteBytes(&value, sizeof(value));
31 } 31 }
32 32
33 bool QuicDataWriter::WriteUInt32(uint32 value) { 33 bool QuicDataWriter::WriteUInt32(uint32_t value) {
34 return WriteBytes(&value, sizeof(value)); 34 return WriteBytes(&value, sizeof(value));
35 } 35 }
36 36
37 bool QuicDataWriter::WriteUInt48(uint64 value) { 37 bool QuicDataWriter::WriteUInt48(uint64_t value) {
38 uint16 hi = static_cast<uint16>(value >> 32); 38 uint16_t hi = static_cast<uint16_t>(value >> 32);
39 uint32 lo = static_cast<uint32>(value); 39 uint32_t lo = static_cast<uint32_t>(value);
40 return WriteUInt32(lo) && WriteUInt16(hi); 40 return WriteUInt32(lo) && WriteUInt16(hi);
41 } 41 }
42 42
43 bool QuicDataWriter::WriteUInt64(uint64 value) { 43 bool QuicDataWriter::WriteUInt64(uint64_t value) {
44 return WriteBytes(&value, sizeof(value)); 44 return WriteBytes(&value, sizeof(value));
45 } 45 }
46 46
47 bool QuicDataWriter::WriteUFloat16(uint64 value) { 47 bool QuicDataWriter::WriteUFloat16(uint64_t value) {
48 uint16 result; 48 uint16_t result;
49 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { 49 if (value < (UINT64_C(1) << kUFloat16MantissaEffectiveBits)) {
50 // Fast path: either the value is denormalized, or has exponent zero. 50 // Fast path: either the value is denormalized, or has exponent zero.
51 // Both cases are represented by the value itself. 51 // Both cases are represented by the value itself.
52 result = static_cast<uint16>(value); 52 result = static_cast<uint16_t>(value);
53 } else if (value >= kUFloat16MaxValue) { 53 } else if (value >= kUFloat16MaxValue) {
54 // Value is out of range; clamp it to the maximum representable. 54 // Value is out of range; clamp it to the maximum representable.
55 result = numeric_limits<uint16>::max(); 55 result = numeric_limits<uint16_t>::max();
56 } else { 56 } else {
57 // The highest bit is between position 13 and 42 (zero-based), which 57 // The highest bit is between position 13 and 42 (zero-based), which
58 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, 58 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10,
59 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 59 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11
60 // and count the shifts. 60 // and count the shifts.
61 uint16 exponent = 0; 61 uint16_t exponent = 0;
62 for (uint16 offset = 16; offset > 0; offset /= 2) { 62 for (uint16_t offset = 16; offset > 0; offset /= 2) {
63 // Right-shift the value until the highest bit is in position 11. 63 // Right-shift the value until the highest bit is in position 11.
64 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), 64 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30),
65 // shift if the bit is at or above 11 + offset. 65 // shift if the bit is at or above 11 + offset.
66 if (value >= (UINT64_C(1) << (kUFloat16MantissaBits + offset))) { 66 if (value >= (UINT64_C(1) << (kUFloat16MantissaBits + offset))) {
67 exponent += offset; 67 exponent += offset;
68 value >>= offset; 68 value >>= offset;
69 } 69 }
70 } 70 }
71 71
72 DCHECK_GE(exponent, 1); 72 DCHECK_GE(exponent, 1);
73 DCHECK_LE(exponent, kUFloat16MaxExponent); 73 DCHECK_LE(exponent, kUFloat16MaxExponent);
74 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits); 74 DCHECK_GE(value, UINT64_C(1) << kUFloat16MantissaBits);
75 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits); 75 DCHECK_LT(value, UINT64_C(1) << kUFloat16MantissaEffectiveBits);
76 76
77 // Hidden bit (position 11) is set. We should remove it and increment the 77 // Hidden bit (position 11) is set. We should remove it and increment the
78 // exponent. Equivalently, we just add it to the exponent. 78 // exponent. Equivalently, we just add it to the exponent.
79 // This hides the bit. 79 // This hides the bit.
80 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); 80 result = static_cast<uint16_t>(value + (exponent << kUFloat16MantissaBits));
81 } 81 }
82 82
83 return WriteBytes(&result, sizeof(result)); 83 return WriteBytes(&result, sizeof(result));
84 } 84 }
85 85
86 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { 86 bool QuicDataWriter::WriteStringPiece16(StringPiece val) {
87 if (val.size() > numeric_limits<uint16>::max()) { 87 if (val.size() > numeric_limits<uint16_t>::max()) {
88 return false; 88 return false;
89 } 89 }
90 if (!WriteUInt16(static_cast<uint16>(val.size()))) { 90 if (!WriteUInt16(static_cast<uint16_t>(val.size()))) {
91 return false; 91 return false;
92 } 92 }
93 return WriteBytes(val.data(), val.size()); 93 return WriteBytes(val.data(), val.size());
94 } 94 }
95 95
96 char* QuicDataWriter::BeginWrite(size_t length) { 96 char* QuicDataWriter::BeginWrite(size_t length) {
97 if (length_ > capacity_) { 97 if (length_ > capacity_) {
98 return nullptr; 98 return nullptr;
99 } 99 }
100 100
101 if (capacity_ - length_ < length) { 101 if (capacity_ - length_ < length) {
102 return nullptr; 102 return nullptr;
103 } 103 }
104 104
105 #ifdef ARCH_CPU_64_BITS 105 #ifdef ARCH_CPU_64_BITS
106 DCHECK_LE(length, std::numeric_limits<uint32>::max()); 106 DCHECK_LE(length, std::numeric_limits<uint32_t>::max());
107 #endif 107 #endif
108 108
109 return buffer_ + length_; 109 return buffer_ + length_;
110 } 110 }
111 111
112 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) { 112 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) {
113 char* dest = BeginWrite(data_len); 113 char* dest = BeginWrite(data_len);
114 if (!dest) { 114 if (!dest) {
115 return false; 115 return false;
116 } 116 }
117 117
118 memcpy(dest, data, data_len); 118 memcpy(dest, data, data_len);
119 119
120 length_ += data_len; 120 length_ += data_len;
121 return true; 121 return true;
122 } 122 }
123 123
124 bool QuicDataWriter::WriteRepeatedByte(uint8 byte, size_t count) { 124 bool QuicDataWriter::WriteRepeatedByte(uint8_t byte, size_t count) {
125 char* dest = BeginWrite(count); 125 char* dest = BeginWrite(count);
126 if (!dest) { 126 if (!dest) {
127 return false; 127 return false;
128 } 128 }
129 129
130 memset(dest, byte, count); 130 memset(dest, byte, count);
131 131
132 length_ += count; 132 length_ += count;
133 return true; 133 return true;
134 } 134 }
135 135
136 void QuicDataWriter::WritePadding() { 136 void QuicDataWriter::WritePadding() {
137 DCHECK_LE(length_, capacity_); 137 DCHECK_LE(length_, capacity_);
138 if (length_ > capacity_) { 138 if (length_ > capacity_) {
139 return; 139 return;
140 } 140 }
141 memset(buffer_ + length_, 0x00, capacity_ - length_); 141 memset(buffer_ + length_, 0x00, capacity_ - length_);
142 length_ = capacity_; 142 length_ = capacity_;
143 } 143 }
144 144
145 } // namespace net 145 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_data_writer.h ('k') | net/quic/quic_data_writer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698