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 {
23 CHECK_LT(index, GetEntryCount());
24 return entries_[index];
27 HpackEntry* HpackHeaderTable::GetMutableEntry(uint32 index) {
28 CHECK_LT(index, GetEntryCount());
29 return &entries_[index];
32 void HpackHeaderTable::SetMaxSize(uint32 max_size) {
34 while (size_ > max_size_) {
35 CHECK(!entries_.empty());
36 size_ -= entries_.back().Size();
41 void HpackHeaderTable::TryAddEntry(
42 const HpackEntry& entry,
44 std::vector<uint32>* removed_referenced_indices) {
46 removed_referenced_indices->clear();
48 // The algorithm used here is described in 3.3.3. We're assuming
49 // that the given entry is caching the name/value.
50 size_t target_size = 0;
51 size_t size_t_max_size = static_cast<size_t>(max_size_);
52 if (entry.Size() <= size_t_max_size) {
53 // The conditional implies the difference can fit in 32 bits.
54 target_size = size_t_max_size - entry.Size();
56 while ((static_cast<size_t>(size_) > target_size) && !entries_.empty()) {
57 if (entries_.back().IsReferenced()) {
58 removed_referenced_indices->push_back(entries_.size() - 1);
60 size_ -= entries_.back().Size();
64 if (entry.Size() <= size_t_max_size) {
65 // Implied by the exit condition of the while loop above and the
66 // condition of the if.
67 DCHECK_LE(static_cast<size_t>(size_) + entry.Size(), size_t_max_size);
68 size_ += entry.Size();
70 entries_.push_front(entry);