Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / ext / transport / chttp2 / transport / hpack_encoder_table.h
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_TABLE_H
16 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_TABLE_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include "absl/container/inlined_vector.h"
21
22 #include "src/core/ext/transport/chttp2/transport/hpack_constants.h"
23
24 namespace grpc_core {
25
26 // Tracks the values available in the remote HPACK header table, and their
27 // sizes.
28 class HPackEncoderTable {
29  public:
30   HPackEncoderTable() : elem_size_(hpack_constants::kInitialTableEntries) {}
31
32   // Reserve space in table for the new element, evict entries if needed.
33   // Return the new index of the element. Return 0 to indicate not adding to
34   // table.
35   uint32_t AllocateIndex(size_t element_size);
36   // Set the maximum table size. Return true if it changed.
37   bool SetMaxSize(uint32_t max_table_size);
38   // Get the current max table size
39   uint32_t max_size() const { return max_table_size_; }
40   // Get the current table size
41   uint32_t test_only_table_size() const { return table_size_; }
42
43   // Convert an element index into a dynamic index
44   uint32_t DynamicIndex(uint32_t index) const {
45     return 1 + hpack_constants::kLastStaticEntry + tail_remote_index_ +
46            table_elems_ - index;
47   }
48   // Check if an element index is convertable to a dynamic index
49   bool ConvertableToDynamicIndex(uint32_t index) const {
50     return index > tail_remote_index_;
51   }
52
53  private:
54   void EvictOne();
55   void Rebuild(uint32_t capacity);
56
57   // one before the lowest usable table index
58   uint32_t tail_remote_index_ = 0;
59   uint32_t max_table_size_ = hpack_constants::kInitialTableSize;
60   uint32_t table_elems_ = 0;
61   uint32_t table_size_ = 0;
62   // The size of each element in the HPACK table.
63   absl::InlinedVector<uint16_t, hpack_constants::kInitialTableEntries>
64       elem_size_;
65 };
66
67 }  // namespace grpc_core
68
69 #endif  // GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_TABLE_H