| 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_header_table.h" | 5 #include "net/spdy/hpack_header_table.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "net/spdy/hpack_string_util.h" | 8 #include "net/spdy/hpack_string_util.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 HpackHeaderTable::HpackHeaderTable() : size_(0), max_size_(4096) {} | 12 HpackHeaderTable::HpackHeaderTable() : size_(0), max_size_(4096) {} |
| 13 | 13 |
| 14 HpackHeaderTable::~HpackHeaderTable() {} | 14 HpackHeaderTable::~HpackHeaderTable() {} |
| 15 | 15 |
| 16 uint32 HpackHeaderTable::GetEntryCount() const { | 16 uint32 HpackHeaderTable::GetEntryCount() const { |
| 17 size_t size = entries_.size(); | 17 size_t size = entries_.size(); |
| 18 DCHECK_LE(size, kuint32max); | 18 DCHECK_LE(size, kuint32max); |
| 19 return static_cast<uint32>(size); | 19 return static_cast<uint32>(size); |
| 20 } | 20 } |
| 21 | 21 |
| 22 const HpackEntry& HpackHeaderTable::GetEntry(uint32 index) const { | 22 const HpackEntry& HpackHeaderTable::GetEntry(uint32 index) const { |
| 23 CHECK_LT(index, GetEntryCount()); | 23 CHECK_GE(index, 1u); |
| 24 return entries_[index]; | 24 CHECK_LE(index, GetEntryCount()); |
| 25 return entries_[index-1]; |
| 25 } | 26 } |
| 26 | 27 |
| 27 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) { | 28 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) { |
| 28 CHECK_LT(index, GetEntryCount()); | 29 CHECK_GE(index, 1u); |
| 29 return &entries_[index]; | 30 CHECK_LE(index, GetEntryCount()); |
| 31 return &entries_[index-1]; |
| 30 } | 32 } |
| 31 | 33 |
| 32 void HpackHeaderTable::SetMaxSize(uint32 max_size) { | 34 void HpackHeaderTable::SetMaxSize(uint32 max_size) { |
| 33 max_size_ = max_size; | 35 max_size_ = max_size; |
| 34 while (size_ > max_size_) { | 36 while (size_ > max_size_) { |
| 35 CHECK(!entries_.empty()); | 37 CHECK(!entries_.empty()); |
| 36 size_ -= entries_.back().Size(); | 38 size_ -= entries_.back().Size(); |
| 37 entries_.pop_back(); | 39 entries_.pop_back(); |
| 38 } | 40 } |
| 39 } | 41 } |
| 40 | 42 |
| 41 void HpackHeaderTable::TryAddEntry( | 43 void HpackHeaderTable::TryAddEntry( |
| 42 const HpackEntry& entry, | 44 const HpackEntry& entry, |
| 43 int32* index, | 45 uint32* index, |
| 44 std::vector<uint32>* removed_referenced_indices) { | 46 std::vector<uint32>* removed_referenced_indices) { |
| 45 *index = -1; | 47 *index = 0; |
| 46 removed_referenced_indices->clear(); | 48 removed_referenced_indices->clear(); |
| 47 | 49 |
| 48 // The algorithm used here is described in 3.3.3. We're assuming | 50 // The algorithm used here is described in 3.3.3. We're assuming |
| 49 // that the given entry is caching the name/value. | 51 // that the given entry is caching the name/value. |
| 50 size_t target_size = 0; | 52 size_t target_size = 0; |
| 51 size_t size_t_max_size = static_cast<size_t>(max_size_); | 53 size_t size_t_max_size = static_cast<size_t>(max_size_); |
| 52 if (entry.Size() <= size_t_max_size) { | 54 if (entry.Size() <= size_t_max_size) { |
| 53 // The conditional implies the difference can fit in 32 bits. | 55 // The conditional implies the difference can fit in 32 bits. |
| 54 target_size = size_t_max_size - entry.Size(); | 56 target_size = size_t_max_size - entry.Size(); |
| 55 } | 57 } |
| 56 while ((static_cast<size_t>(size_) > target_size) && !entries_.empty()) { | 58 while (static_cast<size_t>(size_) > target_size) { |
| 59 DCHECK(!entries_.empty()); |
| 57 if (entries_.back().IsReferenced()) { | 60 if (entries_.back().IsReferenced()) { |
| 58 removed_referenced_indices->push_back(entries_.size() - 1); | 61 removed_referenced_indices->push_back(entries_.size()); |
| 59 } | 62 } |
| 60 size_ -= entries_.back().Size(); | 63 size_ -= entries_.back().Size(); |
| 61 entries_.pop_back(); | 64 entries_.pop_back(); |
| 62 } | 65 } |
| 63 | 66 |
| 64 if (entry.Size() <= size_t_max_size) { | 67 if (entry.Size() <= size_t_max_size) { |
| 65 // Implied by the exit condition of the while loop above and the | 68 // Implied by the exit condition of the while loop above and the |
| 66 // condition of the if. | 69 // condition of the if. |
| 67 DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size); | 70 DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size); |
| 68 size_ += entry.Size(); | 71 size_ += entry.Size(); |
| 69 *index = 0; | 72 *index = 1; |
| 70 entries_.push_front(entry); | 73 entries_.push_front(entry); |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 | 76 |
| 74 } // namespace net | 77 } // namespace net |
| OLD | NEW |