-
Notifications
You must be signed in to change notification settings - Fork 1
/
hpack_encoder_table.cc
91 lines (77 loc) · 2.76 KB
/
hpack_encoder_table.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <grpc/support/port_platform.h>
#include "src/core/ext/transport/chttp2/transport/hpack_encoder_table.h"
#include <algorithm>
#include <grpc/support/log.h>
namespace grpc_core {
uint32_t HPackEncoderTable::AllocateIndex(size_t element_size) {
GPR_DEBUG_ASSERT(element_size >= 32);
uint32_t new_index = tail_remote_index_ + table_elems_ + 1;
GPR_DEBUG_ASSERT(element_size <= MaxEntrySize());
if (element_size > max_table_size_) {
while (table_size_ > 0) {
EvictOne();
}
return 0;
}
// Reserve space for this element in the remote table: if this overflows
// the current table, drop elements until it fits, matching the decompressor
// algorithm.
while (table_size_ + element_size > max_table_size_) {
EvictOne();
}
GPR_ASSERT(table_elems_ < elem_size_.size());
elem_size_[new_index % elem_size_.size()] =
static_cast<uint16_t>(element_size);
table_size_ += element_size;
table_elems_++;
return new_index;
}
bool HPackEncoderTable::SetMaxSize(uint32_t max_table_size) {
if (max_table_size == max_table_size_) {
return false;
}
while (table_size_ > 0 && table_size_ > max_table_size) {
EvictOne();
}
max_table_size_ = max_table_size;
const size_t max_table_elems =
hpack_constants::EntriesForBytes(max_table_size);
// TODO(ctiller): integrate with ResourceQuota to rebuild smaller when we can.
if (max_table_elems > elem_size_.size()) {
Rebuild(static_cast<uint32_t>(
std::max(max_table_elems, 2 * elem_size_.size())));
}
return true;
}
void HPackEncoderTable::EvictOne() {
tail_remote_index_++;
GPR_ASSERT(tail_remote_index_ > 0);
GPR_ASSERT(table_elems_ > 0);
auto removing_size = elem_size_[tail_remote_index_ % elem_size_.size()];
GPR_ASSERT(table_size_ >= removing_size);
table_size_ -= removing_size;
table_elems_--;
}
void HPackEncoderTable::Rebuild(uint32_t capacity) {
decltype(elem_size_) new_elem_size(capacity);
GPR_ASSERT(table_elems_ <= capacity);
for (uint32_t i = 0; i < table_elems_; i++) {
uint32_t ofs = tail_remote_index_ + i + 1;
new_elem_size[ofs % capacity] = elem_size_[ofs % elem_size_.size()];
}
elem_size_.swap(new_elem_size);
}
} // namespace grpc_core