OLD | NEW |
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 <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 | 13 |
14 using base::StringPiece; | |
15 using std::numeric_limits; | |
16 | |
17 namespace net { | 14 namespace net { |
18 | 15 |
19 QuicDataWriter::QuicDataWriter(size_t size) | 16 QuicDataWriter::QuicDataWriter(size_t size) |
20 : buffer_(new char[size]), | 17 : buffer_(new char[size]), |
21 capacity_(size), | 18 capacity_(size), |
22 length_(0) { | 19 length_(0) { |
23 } | 20 } |
24 | 21 |
25 QuicDataWriter::~QuicDataWriter() { | 22 QuicDataWriter::~QuicDataWriter() { |
26 delete[] buffer_; | 23 delete[] buffer_; |
(...skipping 13 matching lines...) Expand all Loading... |
40 | 37 |
41 bool QuicDataWriter::WriteUInt16(uint16 value) { | 38 bool QuicDataWriter::WriteUInt16(uint16 value) { |
42 return WriteBytes(&value, sizeof(value)); | 39 return WriteBytes(&value, sizeof(value)); |
43 } | 40 } |
44 | 41 |
45 bool QuicDataWriter::WriteUInt32(uint32 value) { | 42 bool QuicDataWriter::WriteUInt32(uint32 value) { |
46 return WriteBytes(&value, sizeof(value)); | 43 return WriteBytes(&value, sizeof(value)); |
47 } | 44 } |
48 | 45 |
49 bool QuicDataWriter::WriteUInt48(uint64 value) { | 46 bool QuicDataWriter::WriteUInt48(uint64 value) { |
50 uint32 hi = value >> 32; | 47 uint16 hi = static_cast<uint16>(value >> 32); |
51 uint32 lo = value & GG_UINT64_C(0x00000000FFFFFFFF); | 48 uint32 lo = static_cast<uint32>(value); |
52 return WriteUInt32(lo) && WriteUInt16(hi); | 49 return WriteUInt32(lo) && WriteUInt16(hi); |
53 } | 50 } |
54 | 51 |
55 bool QuicDataWriter::WriteUInt64(uint64 value) { | 52 bool QuicDataWriter::WriteUInt64(uint64 value) { |
56 return WriteBytes(&value, sizeof(value)); | 53 return WriteBytes(&value, sizeof(value)); |
57 } | 54 } |
58 | 55 |
59 bool QuicDataWriter::WriteUFloat16(uint64 value) { | 56 bool QuicDataWriter::WriteUFloat16(uint64 value) { |
60 uint16 result; | 57 uint16 result; |
61 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { | 58 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) { |
62 // Fast path: either the value is denormalized, or has exponent zero. | 59 // Fast path: either the value is denormalized, or has exponent zero. |
63 // Both cases are represented by the value itself. | 60 // Both cases are represented by the value itself. |
64 result = value; | 61 result = static_cast<uint16>(value); |
65 } else if (value >= kUFloat16MaxValue) { | 62 } else if (value >= kUFloat16MaxValue) { |
66 // Value is out of range; clamp it to the maximum representable. | 63 // Value is out of range; clamp it to the maximum representable. |
67 result = numeric_limits<uint16>::max(); | 64 result = std::numeric_limits<uint16>::max(); |
68 } else { | 65 } else { |
69 // The highest bit is between position 13 and 42 (zero-based), which | 66 // The highest bit is between position 13 and 42 (zero-based), which |
70 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, | 67 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10, |
71 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 | 68 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11 |
72 // and count the shifts. | 69 // and count the shifts. |
73 uint16 exponent = 0; | 70 uint16 exponent = 0; |
74 for (uint16 offset = 16; offset > 0; offset /= 2) { | 71 for (uint16 offset = 16; offset > 0; offset /= 2) { |
75 // Right-shift the value until the highest bit is in position 11. | 72 // Right-shift the value until the highest bit is in position 11. |
76 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), | 73 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30), |
77 // shift if the bit is at or above 11 + offset. | 74 // shift if the bit is at or above 11 + offset. |
78 if (value >= (GG_UINT64_C(1) << (kUFloat16MantissaBits + offset))) { | 75 if (value >= (GG_UINT64_C(1) << (kUFloat16MantissaBits + offset))) { |
79 exponent += offset; | 76 exponent += offset; |
80 value >>= offset; | 77 value >>= offset; |
81 } | 78 } |
82 } | 79 } |
83 | 80 |
84 DCHECK_GE(exponent, 1); | 81 DCHECK_GE(exponent, 1); |
85 DCHECK_LE(exponent, kUFloat16MaxExponent); | 82 DCHECK_LE(exponent, kUFloat16MaxExponent); |
86 DCHECK_GE(value, GG_UINT64_C(1) << kUFloat16MantissaBits); | 83 DCHECK_GE(value, GG_UINT64_C(1) << kUFloat16MantissaBits); |
87 DCHECK_LT(value, GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits); | 84 DCHECK_LT(value, GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits); |
88 | 85 |
89 // Hidden bit (position 11) is set. We should remove it and increment the | 86 // Hidden bit (position 11) is set. We should remove it and increment the |
90 // exponent. Equivalently, we just add it to the exponent. | 87 // exponent. Equivalently, we just add it to the exponent. |
91 // This hides the bit. | 88 // This hides the bit. |
92 result = value + (exponent << kUFloat16MantissaBits); | 89 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits)); |
93 } | 90 } |
94 | 91 |
95 return WriteBytes(&result, sizeof(result)); | 92 return WriteBytes(&result, sizeof(result)); |
96 } | 93 } |
97 | 94 |
98 bool QuicDataWriter::WriteStringPiece16(StringPiece val) { | 95 bool QuicDataWriter::WriteStringPiece16(base::StringPiece val) { |
99 if (val.length() > numeric_limits<uint16>::max()) { | 96 if (val.size() > std::numeric_limits<uint16>::max()) { |
100 return false; | 97 return false; |
101 } | 98 } |
102 if (!WriteUInt16(val.size())) { | 99 if (!WriteUInt16(static_cast<uint16>(val.size()))) { |
103 return false; | 100 return false; |
104 } | 101 } |
105 return WriteBytes(val.data(), val.size()); | 102 return WriteBytes(val.data(), val.size()); |
106 } | 103 } |
107 | 104 |
108 bool QuicDataWriter::WriteIOVector(const IOVector& data) { | 105 bool QuicDataWriter::WriteIOVector(const IOVector& data) { |
109 char *dest = BeginWrite(data.TotalBufferSize()); | 106 char *dest = BeginWrite(data.TotalBufferSize()); |
110 if (!dest) { | 107 if (!dest) { |
111 return false; | 108 return false; |
112 } | 109 } |
113 for (size_t i = 0; i < data.Size(); ++i) { | 110 for (size_t i = 0; i < data.Size(); ++i) { |
114 WriteBytes(data.iovec()[i].iov_base, data.iovec()[i].iov_len); | 111 WriteBytes(data.iovec()[i].iov_base, data.iovec()[i].iov_len); |
115 } | 112 } |
116 | 113 |
117 return true; | 114 return true; |
118 } | 115 } |
119 | 116 |
120 char* QuicDataWriter::BeginWrite(size_t length) { | 117 char* QuicDataWriter::BeginWrite(size_t length) { |
121 if (length_ > capacity_) { | 118 if (length_ > capacity_) { |
122 return nullptr; | 119 return nullptr; |
123 } | 120 } |
124 | 121 |
125 if (capacity_ - length_ < length) { | 122 if (capacity_ - length_ < length) { |
126 return nullptr; | 123 return nullptr; |
127 } | 124 } |
128 | 125 |
129 #ifdef ARCH_CPU_64_BITS | 126 #ifdef ARCH_CPU_64_BITS |
130 DCHECK_LE(length, numeric_limits<uint32>::max()); | 127 DCHECK_LE(length, std::numeric_limits<uint32>::max()); |
131 #endif | 128 #endif |
132 | 129 |
133 return buffer_ + length_; | 130 return buffer_ + length_; |
134 } | 131 } |
135 | 132 |
136 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) { | 133 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) { |
137 char* dest = BeginWrite(data_len); | 134 char* dest = BeginWrite(data_len); |
138 if (!dest) { | 135 if (!dest) { |
139 return false; | 136 return false; |
140 } | 137 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 DCHECK_LT(offset, capacity_); | 190 DCHECK_LT(offset, capacity_); |
194 size_t latched_length = length_; | 191 size_t latched_length = length_; |
195 length_ = offset; | 192 length_ = offset; |
196 bool success = WriteUInt48(value); | 193 bool success = WriteUInt48(value); |
197 DCHECK_LE(length_, latched_length); | 194 DCHECK_LE(length_, latched_length); |
198 length_ = latched_length; | 195 length_ = latched_length; |
199 return success; | 196 return success; |
200 } | 197 } |
201 | 198 |
202 } // namespace net | 199 } // namespace net |
OLD | NEW |