| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/spdy/hpack/hpack_output_stream.h" | 5 #include "net/spdy/hpack/hpack_output_stream.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" | 10 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 using std::string; | |
| 15 | |
| 16 HpackOutputStream::HpackOutputStream() : bit_offset_(0) {} | 14 HpackOutputStream::HpackOutputStream() : bit_offset_(0) {} |
| 17 | 15 |
| 18 HpackOutputStream::~HpackOutputStream() {} | 16 HpackOutputStream::~HpackOutputStream() {} |
| 19 | 17 |
| 20 void HpackOutputStream::AppendBits(uint8_t bits, size_t bit_size) { | 18 void HpackOutputStream::AppendBits(uint8_t bits, size_t bit_size) { |
| 21 DCHECK_GT(bit_size, 0u); | 19 DCHECK_GT(bit_size, 0u); |
| 22 DCHECK_LE(bit_size, 8u); | 20 DCHECK_LE(bit_size, 8u); |
| 23 DCHECK_EQ(bits >> bit_size, 0); | 21 DCHECK_EQ(bits >> bit_size, 0); |
| 24 size_t new_bit_offset = bit_offset_ + bit_size; | 22 size_t new_bit_offset = bit_offset_ + bit_size; |
| 25 if (bit_offset_ == 0) { | 23 if (bit_offset_ == 0) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 AppendBits(max_first_byte, N); | 56 AppendBits(max_first_byte, N); |
| 59 I -= max_first_byte; | 57 I -= max_first_byte; |
| 60 while ((I & ~0x7f) != 0) { | 58 while ((I & ~0x7f) != 0) { |
| 61 buffer_.append(1, (I & 0x7f) | 0x80); | 59 buffer_.append(1, (I & 0x7f) | 0x80); |
| 62 I >>= 7; | 60 I >>= 7; |
| 63 } | 61 } |
| 64 AppendBits(static_cast<uint8_t>(I), 8); | 62 AppendBits(static_cast<uint8_t>(I), 8); |
| 65 } | 63 } |
| 66 } | 64 } |
| 67 | 65 |
| 68 void HpackOutputStream::TakeString(string* output) { | 66 void HpackOutputStream::TakeString(SpdyString* output) { |
| 69 // This must hold, since all public functions cause the buffer to | 67 // This must hold, since all public functions cause the buffer to |
| 70 // end on a byte boundary. | 68 // end on a byte boundary. |
| 71 DCHECK_EQ(bit_offset_, 0u); | 69 DCHECK_EQ(bit_offset_, 0u); |
| 72 buffer_.swap(*output); | 70 buffer_.swap(*output); |
| 73 buffer_.clear(); | 71 buffer_.clear(); |
| 74 bit_offset_ = 0; | 72 bit_offset_ = 0; |
| 75 } | 73 } |
| 76 | 74 |
| 77 void HpackOutputStream::BoundedTakeString(size_t max_size, string* output) { | 75 void HpackOutputStream::BoundedTakeString(size_t max_size, SpdyString* output) { |
| 78 if (buffer_.size() > max_size) { | 76 if (buffer_.size() > max_size) { |
| 79 // Save off overflow bytes to temporary string (causes a copy). | 77 // Save off overflow bytes to temporary string (causes a copy). |
| 80 string overflow(buffer_.data() + max_size, buffer_.size() - max_size); | 78 SpdyString overflow(buffer_.data() + max_size, buffer_.size() - max_size); |
| 81 | 79 |
| 82 // Resize buffer down to the given limit. | 80 // Resize buffer down to the given limit. |
| 83 buffer_.resize(max_size); | 81 buffer_.resize(max_size); |
| 84 | 82 |
| 85 // Give buffer to output string. | 83 // Give buffer to output string. |
| 86 *output = std::move(buffer_); | 84 *output = std::move(buffer_); |
| 87 | 85 |
| 88 // Reset to contain overflow. | 86 // Reset to contain overflow. |
| 89 buffer_ = std::move(overflow); | 87 buffer_ = std::move(overflow); |
| 90 } else { | 88 } else { |
| 91 TakeString(output); | 89 TakeString(output); |
| 92 } | 90 } |
| 93 } | 91 } |
| 94 | 92 |
| 95 size_t HpackOutputStream::EstimateMemoryUsage() const { | 93 size_t HpackOutputStream::EstimateMemoryUsage() const { |
| 96 return SpdyEstimateMemoryUsage(buffer_); | 94 return SpdyEstimateMemoryUsage(buffer_); |
| 97 } | 95 } |
| 98 | 96 |
| 99 } // namespace net | 97 } // namespace net |
| OLD | NEW |