| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/core/quic_stream_sequencer_buffer.h" | 5 #include "net/quic/core/quic_stream_sequencer_buffer.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 QuicStreamSequencerBuffer::FrameInfo::FrameInfo(size_t length, | 43 QuicStreamSequencerBuffer::FrameInfo::FrameInfo(size_t length, |
| 44 QuicTime timestamp) | 44 QuicTime timestamp) |
| 45 : length(length), timestamp(timestamp) {} | 45 : length(length), timestamp(timestamp) {} |
| 46 | 46 |
| 47 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) | 47 QuicStreamSequencerBuffer::QuicStreamSequencerBuffer(size_t max_capacity_bytes) |
| 48 : max_buffer_capacity_bytes_(max_capacity_bytes), | 48 : max_buffer_capacity_bytes_(max_capacity_bytes), |
| 49 blocks_count_( | 49 blocks_count_( |
| 50 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), | 50 ceil(static_cast<double>(max_capacity_bytes) / kBlockSizeBytes)), |
| 51 total_bytes_read_(0), | 51 total_bytes_read_(0), |
| 52 blocks_(nullptr), | 52 reduce_sequencer_buffer_memory_life_time_( |
| 53 FLAGS_quic_reduce_sequencer_buffer_memory_life_time), // NOLINT |
| 54 blocks_(reduce_sequencer_buffer_memory_life_time_ |
| 55 ? nullptr |
| 56 : new BufferBlock*[blocks_count_]()), |
| 53 destruction_indicator_(123456) { | 57 destruction_indicator_(123456) { |
| 54 CHECK_GT(blocks_count_, 1u) | 58 CHECK_GT(blocks_count_, 1u) |
| 55 << "blocks_count_ = " << blocks_count_ | 59 << "blocks_count_ = " << blocks_count_ |
| 56 << ", max_buffer_capacity_bytes_ = " << max_buffer_capacity_bytes_; | 60 << ", max_buffer_capacity_bytes_ = " << max_buffer_capacity_bytes_; |
| 57 Clear(); | 61 Clear(); |
| 58 } | 62 } |
| 59 | 63 |
| 60 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { | 64 QuicStreamSequencerBuffer::~QuicStreamSequencerBuffer() { |
| 61 Clear(); | 65 Clear(); |
| 62 destruction_indicator_ = 654321; | 66 destruction_indicator_ = 654321; |
| 63 } | 67 } |
| 64 | 68 |
| 65 void QuicStreamSequencerBuffer::Clear() { | 69 void QuicStreamSequencerBuffer::Clear() { |
| 66 if (blocks_ != nullptr) { | 70 if (!reduce_sequencer_buffer_memory_life_time_ || blocks_ != nullptr) { |
| 67 for (size_t i = 0; i < blocks_count_; ++i) { | 71 for (size_t i = 0; i < blocks_count_; ++i) { |
| 68 if (blocks_[i] != nullptr) { | 72 if (blocks_[i] != nullptr) { |
| 69 RetireBlock(i); | 73 RetireBlock(i); |
| 70 } | 74 } |
| 71 } | 75 } |
| 72 } | 76 } |
| 73 num_bytes_buffered_ = 0; | 77 num_bytes_buffered_ = 0; |
| 74 // Reset gaps_ so that buffer is in a state as if all data before | 78 // Reset gaps_ so that buffer is in a state as if all data before |
| 75 // total_bytes_read_ has been consumed, and those after total_bytes_read_ | 79 // total_bytes_read_ has been consumed, and those after total_bytes_read_ |
| 76 // has never arrived. | 80 // has never arrived. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 179 |
| 176 size_t block_capacity = GetBlockCapacity(write_block_num); | 180 size_t block_capacity = GetBlockCapacity(write_block_num); |
| 177 size_t bytes_avail = block_capacity - write_block_offset; | 181 size_t bytes_avail = block_capacity - write_block_offset; |
| 178 | 182 |
| 179 // If this write meets the upper boundary of the buffer, | 183 // If this write meets the upper boundary of the buffer, |
| 180 // reduce the available free bytes. | 184 // reduce the available free bytes. |
| 181 if (offset + bytes_avail > total_bytes_read_ + max_buffer_capacity_bytes_) { | 185 if (offset + bytes_avail > total_bytes_read_ + max_buffer_capacity_bytes_) { |
| 182 bytes_avail = total_bytes_read_ + max_buffer_capacity_bytes_ - offset; | 186 bytes_avail = total_bytes_read_ + max_buffer_capacity_bytes_ - offset; |
| 183 } | 187 } |
| 184 | 188 |
| 185 if (blocks_ == nullptr) { | 189 if (reduce_sequencer_buffer_memory_life_time_ && blocks_ == nullptr) { |
| 186 blocks_.reset(new BufferBlock*[blocks_count_]()); | 190 blocks_.reset(new BufferBlock*[blocks_count_]()); |
| 187 for (size_t i = 0; i < blocks_count_; ++i) { | 191 for (size_t i = 0; i < blocks_count_; ++i) { |
| 188 blocks_[i] = nullptr; | 192 blocks_[i] = nullptr; |
| 189 } | 193 } |
| 190 } | 194 } |
| 191 | 195 |
| 192 if (write_block_num >= blocks_count_) { | 196 if (write_block_num >= blocks_count_) { |
| 193 *error_details = StringPrintf( | 197 *error_details = StringPrintf( |
| 194 "QuicStreamSequencerBuffer error: OnStreamData() exceed array bounds." | 198 "QuicStreamSequencerBuffer error: OnStreamData() exceed array bounds." |
| 195 "write offset = %" PRIu64 " write_block_num = %" PRIuS | 199 "write offset = %" PRIu64 " write_block_num = %" PRIuS |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 } | 470 } |
| 467 | 471 |
| 468 size_t QuicStreamSequencerBuffer::FlushBufferedFrames() { | 472 size_t QuicStreamSequencerBuffer::FlushBufferedFrames() { |
| 469 size_t prev_total_bytes_read = total_bytes_read_; | 473 size_t prev_total_bytes_read = total_bytes_read_; |
| 470 total_bytes_read_ = gaps_.back().begin_offset; | 474 total_bytes_read_ = gaps_.back().begin_offset; |
| 471 Clear(); | 475 Clear(); |
| 472 return total_bytes_read_ - prev_total_bytes_read; | 476 return total_bytes_read_ - prev_total_bytes_read; |
| 473 } | 477 } |
| 474 | 478 |
| 475 void QuicStreamSequencerBuffer::ReleaseWholeBuffer() { | 479 void QuicStreamSequencerBuffer::ReleaseWholeBuffer() { |
| 480 if (!reduce_sequencer_buffer_memory_life_time_) { |
| 481 // Don't release buffer if flag is off. |
| 482 return; |
| 483 } |
| 476 Clear(); | 484 Clear(); |
| 477 blocks_.reset(nullptr); | 485 blocks_.reset(nullptr); |
| 478 } | 486 } |
| 479 | 487 |
| 480 size_t QuicStreamSequencerBuffer::ReadableBytes() const { | 488 size_t QuicStreamSequencerBuffer::ReadableBytes() const { |
| 481 return gaps_.front().begin_offset - total_bytes_read_; | 489 return gaps_.front().begin_offset - total_bytes_read_; |
| 482 } | 490 } |
| 483 | 491 |
| 484 bool QuicStreamSequencerBuffer::HasBytesToRead() const { | 492 bool QuicStreamSequencerBuffer::HasBytesToRead() const { |
| 485 return ReadableBytes() > 0; | 493 return ReadableBytes() > 0; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 QuicStreamOffset current_frame_end_offset = | 605 QuicStreamOffset current_frame_end_offset = |
| 598 it.second.length + current_frame_begin_offset; | 606 it.second.length + current_frame_begin_offset; |
| 599 current_frames_string = string(StringPrintf( | 607 current_frames_string = string(StringPrintf( |
| 600 "%s[%" PRIu64 ", %" PRIu64 ") ", current_frames_string.c_str(), | 608 "%s[%" PRIu64 ", %" PRIu64 ") ", current_frames_string.c_str(), |
| 601 current_frame_begin_offset, current_frame_end_offset)); | 609 current_frame_begin_offset, current_frame_end_offset)); |
| 602 } | 610 } |
| 603 return current_frames_string; | 611 return current_frames_string; |
| 604 } | 612 } |
| 605 | 613 |
| 606 } // namespace net | 614 } // namespace net |
| OLD | NEW |