• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_TABLE_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_TABLE_H
17 
18 #include <grpc/support/port_platform.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include <limits>
23 #include <vector>
24 
25 #include "src/core/ext/transport/chttp2/transport/hpack_constants.h"
26 
27 namespace grpc_core {
28 
29 // Tracks the values available in the remote HPACK header table, and their
30 // sizes.
31 class HPackEncoderTable {
32  public:
33   using EntrySize = uint16_t;
34 
HPackEncoderTable()35   HPackEncoderTable() : elem_size_(hpack_constants::kInitialTableEntries) {}
36 
MaxEntrySize()37   static constexpr size_t MaxEntrySize() {
38     return std::numeric_limits<EntrySize>::max();
39   }
40 
41   // Reserve space in table for the new element, evict entries if needed.
42   // Return the new index of the element. Return 0 to indicate not adding to
43   // table.
44   uint32_t AllocateIndex(size_t element_size);
45   // Set the maximum table size. Return true if it changed.
46   bool SetMaxSize(uint32_t max_table_size);
47   // Get the current max table size
max_size()48   uint32_t max_size() const { return max_table_size_; }
49   // Get the current table size
test_only_table_size()50   uint32_t test_only_table_size() const { return table_size_; }
51   // Get the number of entries in the table
test_only_table_elems()52   uint32_t test_only_table_elems() const { return table_elems_; }
53 
54   // Convert an element index into a dynamic index
DynamicIndex(uint32_t index)55   uint32_t DynamicIndex(uint32_t index) const {
56     return 1 + hpack_constants::kLastStaticEntry + tail_remote_index_ +
57            table_elems_ - index;
58   }
59   // Check if an element index is convertible to a dynamic index
60   // Note that 0 is always not convertible
ConvertibleToDynamicIndex(uint32_t index)61   bool ConvertibleToDynamicIndex(uint32_t index) const {
62     return index > tail_remote_index_;
63   }
64 
65  private:
66   void EvictOne();
67   void Rebuild(uint32_t capacity);
68 
69   // one before the lowest usable table index
70   uint32_t tail_remote_index_ = 0;
71   uint32_t max_table_size_ = hpack_constants::kInitialTableSize;
72   uint32_t table_elems_ = 0;
73   uint32_t table_size_ = 0;
74   // The size of each element in the HPACK table.
75   std::vector<EntrySize> elem_size_;
76 };
77 
78 }  // namespace grpc_core
79 
80 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_TABLE_H
81