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
3 // found in the LICENSE file.
5 #include "net/spdy/hpack_header_table.h"
7 #include "base/logging.h"
8 #include "net/spdy/hpack_string_util.h"
12 HpackHeaderTable::HpackHeaderTable() : size_(0), max_size_(4096) {}
14 HpackHeaderTable::~HpackHeaderTable() {}
16 uint32 HpackHeaderTable::GetEntryCount() const {
17 size_t size = entries_.size();
18 DCHECK_LE(size, kuint32max);
19 return static_cast<uint32>(size);
22 const HpackEntry& HpackHeaderTable::GetEntry(uint32 index) const {
24 CHECK_LE(index, GetEntryCount());
25 return entries_[index-1];
28 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) {
30 CHECK_LE(index, GetEntryCount());
31 return &entries_[index-1];
34 void HpackHeaderTable::SetMaxSize(uint32 max_size) {
36 while (size_ > max_size_) {
37 CHECK(!entries_.empty());
38 size_ -= entries_.back().Size();
43 void HpackHeaderTable::TryAddEntry(
44 const HpackEntry& entry,
46 std::vector<uint32>* removed_referenced_indices) {
48 removed_referenced_indices->clear();
50 // The algorithm used here is described in 3.3.3. We're assuming
51 // that the given entry is caching the name/value.
52 size_t target_size = 0;
53 size_t size_t_max_size = static_cast<size_t>(max_size_);
54 if (entry.Size() <= size_t_max_size) {
55 // The conditional implies the difference can fit in 32 bits.
56 target_size = size_t_max_size - entry.Size();
58 while (static_cast<size_t>(size_) > target_size) {
59 DCHECK(!entries_.empty());
60 if (entries_.back().IsReferenced()) {
61 removed_referenced_indices->push_back(entries_.size());
63 size_ -= entries_.back().Size();
67 if (entry.Size() <= size_t_max_size) {
68 // Implied by the exit condition of the while loop above and the
69 // condition of the if.
70 DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size);
71 size_ += entry.Size();
73 entries_.push_front(entry);