Re-land: C++ readability review
[chromium-blink-merge.git] / net / quic / quic_data_writer.cc
blob05bc044b616f49aafd9f9060fbc041c1d9cefb9d
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
3 // found in the LICENSE file.
5 #include "net/quic/quic_data_writer.h"
7 #include <algorithm>
8 #include <limits>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
14 using base::StringPiece;
15 using std::numeric_limits;
17 namespace net {
19 QuicDataWriter::QuicDataWriter(size_t size, char* buffer)
20 : buffer_(buffer), capacity_(size), length_(0) {
23 QuicDataWriter::~QuicDataWriter() {
26 char* QuicDataWriter::data() {
27 return buffer_;
30 bool QuicDataWriter::WriteUInt8(uint8 value) {
31 return WriteBytes(&value, sizeof(value));
34 bool QuicDataWriter::WriteUInt16(uint16 value) {
35 return WriteBytes(&value, sizeof(value));
38 bool QuicDataWriter::WriteUInt32(uint32 value) {
39 return WriteBytes(&value, sizeof(value));
42 bool QuicDataWriter::WriteUInt48(uint64 value) {
43 uint16 hi = static_cast<uint16>(value >> 32);
44 uint32 lo = static_cast<uint32>(value);
45 return WriteUInt32(lo) && WriteUInt16(hi);
48 bool QuicDataWriter::WriteUInt64(uint64 value) {
49 return WriteBytes(&value, sizeof(value));
52 bool QuicDataWriter::WriteUFloat16(uint64 value) {
53 uint16 result;
54 if (value < (GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits)) {
55 // Fast path: either the value is denormalized, or has exponent zero.
56 // Both cases are represented by the value itself.
57 result = static_cast<uint16>(value);
58 } else if (value >= kUFloat16MaxValue) {
59 // Value is out of range; clamp it to the maximum representable.
60 result = numeric_limits<uint16>::max();
61 } else {
62 // The highest bit is between position 13 and 42 (zero-based), which
63 // corresponds to exponent 1-30. In the output, mantissa is from 0 to 10,
64 // hidden bit is 11 and exponent is 11 to 15. Shift the highest bit to 11
65 // and count the shifts.
66 uint16 exponent = 0;
67 for (uint16 offset = 16; offset > 0; offset /= 2) {
68 // Right-shift the value until the highest bit is in position 11.
69 // For offset of 16, 8, 4, 2 and 1 (binary search over 1-30),
70 // shift if the bit is at or above 11 + offset.
71 if (value >= (GG_UINT64_C(1) << (kUFloat16MantissaBits + offset))) {
72 exponent += offset;
73 value >>= offset;
77 DCHECK_GE(exponent, 1);
78 DCHECK_LE(exponent, kUFloat16MaxExponent);
79 DCHECK_GE(value, GG_UINT64_C(1) << kUFloat16MantissaBits);
80 DCHECK_LT(value, GG_UINT64_C(1) << kUFloat16MantissaEffectiveBits);
82 // Hidden bit (position 11) is set. We should remove it and increment the
83 // exponent. Equivalently, we just add it to the exponent.
84 // This hides the bit.
85 result = static_cast<uint16>(value + (exponent << kUFloat16MantissaBits));
88 return WriteBytes(&result, sizeof(result));
91 bool QuicDataWriter::WriteStringPiece16(StringPiece val) {
92 if (val.size() > numeric_limits<uint16>::max()) {
93 return false;
95 if (!WriteUInt16(static_cast<uint16>(val.size()))) {
96 return false;
98 return WriteBytes(val.data(), val.size());
101 bool QuicDataWriter::WriteIOVector(const IOVector& data) {
102 char *dest = BeginWrite(data.TotalBufferSize());
103 if (!dest) {
104 return false;
106 for (size_t i = 0; i < data.Size(); ++i) {
107 WriteBytes(data.iovec()[i].iov_base, data.iovec()[i].iov_len);
110 return true;
113 char* QuicDataWriter::BeginWrite(size_t length) {
114 if (length_ > capacity_) {
115 return nullptr;
118 if (capacity_ - length_ < length) {
119 return nullptr;
122 #ifdef ARCH_CPU_64_BITS
123 DCHECK_LE(length, std::numeric_limits<uint32>::max());
124 #endif
126 return buffer_ + length_;
129 bool QuicDataWriter::WriteBytes(const void* data, size_t data_len) {
130 char* dest = BeginWrite(data_len);
131 if (!dest) {
132 return false;
135 memcpy(dest, data, data_len);
137 length_ += data_len;
138 return true;
141 bool QuicDataWriter::WriteRepeatedByte(uint8 byte, size_t count) {
142 char* dest = BeginWrite(count);
143 if (!dest) {
144 return false;
147 memset(dest, byte, count);
149 length_ += count;
150 return true;
153 void QuicDataWriter::WritePadding() {
154 DCHECK_LE(length_, capacity_);
155 if (length_ > capacity_) {
156 return;
158 memset(buffer_ + length_, 0x00, capacity_ - length_);
159 length_ = capacity_;
163 } // namespace net