OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef NET_SPDY_HPACK_HPACK_HEADER_TABLE_H_ | |
6 #define NET_SPDY_HPACK_HPACK_HEADER_TABLE_H_ | |
7 | |
8 #include <cstddef> | |
9 #include <deque> | |
10 #include <memory> | |
11 #include <unordered_map> | |
12 #include <unordered_set> | |
13 | |
14 #include "base/macros.h" | |
15 #include "net/base/net_export.h" | |
16 #include "net/spdy/hpack/hpack_entry.h" | |
17 #include "net/spdy/platform/api/spdy_string_piece.h" | |
18 | |
19 // All section references below are to http://tools.ietf.org/html/rfc7541. | |
20 | |
21 namespace net { | |
22 | |
23 namespace test { | |
24 class HpackHeaderTablePeer; | |
25 } // namespace test | |
26 | |
27 // A data structure for the static table (2.3.1) and the dynamic table (2.3.2). | |
28 class NET_EXPORT_PRIVATE HpackHeaderTable { | |
29 public: | |
30 friend class test::HpackHeaderTablePeer; | |
31 | |
32 // Debug visitor my be used to extract debug/internal information | |
33 // about the HpackHeaderTable as it operates. | |
34 // | |
35 // Most HpackHeaderTable implementations do not need to bother with | |
36 // this interface at all. | |
37 class DebugVisitorInterface { | |
38 public: | |
39 virtual ~DebugVisitorInterface() {} | |
40 | |
41 // |OnNewEntry()| and |OnUseEntry()| can be used together to | |
42 // gather data about the distribution of time intervals between | |
43 // creation and reference of entries in the dynamic table. The | |
44 // data is desired to sanity check a proposed extension to HPACK | |
45 // for QUIC that would eliminate inter-stream head of line | |
46 // blocking (due to standard HPACK). The visitor should return | |
47 // the current time from |OnNewEntry()|, which will be passed | |
48 // to |OnUseEntry()| each time that particular entry is used to | |
49 // emit an indexed representation. | |
50 virtual int64_t OnNewEntry(const HpackEntry& entry) = 0; | |
51 virtual void OnUseEntry(const HpackEntry& entry) = 0; | |
52 }; | |
53 | |
54 // HpackHeaderTable takes advantage of the deque property that references | |
55 // remain valid, so long as insertions & deletions are at the head & tail. | |
56 // If this changes (eg we start to drop entries from the middle of the table), | |
57 // this needs to be a std::list, in which case |*_index_| can be trivially | |
58 // extended to map to list iterators. | |
59 typedef std::deque<HpackEntry> EntryTable; | |
60 | |
61 struct NET_EXPORT_PRIVATE EntryHasher { | |
62 size_t operator()(const HpackEntry* entry) const; | |
63 }; | |
64 struct NET_EXPORT_PRIVATE EntriesEq { | |
65 bool operator()(const HpackEntry* lhs, const HpackEntry* rhs) const; | |
66 }; | |
67 | |
68 using UnorderedEntrySet = | |
69 std::unordered_set<HpackEntry*, EntryHasher, EntriesEq>; | |
70 using NameToEntryMap = std:: | |
71 unordered_map<SpdyStringPiece, const HpackEntry*, base::StringPieceHash>; | |
72 | |
73 HpackHeaderTable(); | |
74 | |
75 ~HpackHeaderTable(); | |
76 | |
77 // Last-acknowledged value of SETTINGS_HEADER_TABLE_SIZE. | |
78 size_t settings_size_bound() const { return settings_size_bound_; } | |
79 | |
80 // Current and maximum estimated byte size of the table, as described in | |
81 // 4.1. Notably, this is /not/ the number of entries in the table. | |
82 size_t size() const { return size_; } | |
83 size_t max_size() const { return max_size_; } | |
84 | |
85 // Returns the entry matching the index, or NULL. | |
86 const HpackEntry* GetByIndex(size_t index); | |
87 | |
88 // Returns the lowest-value entry having |name|, or NULL. | |
89 const HpackEntry* GetByName(SpdyStringPiece name); | |
90 | |
91 // Returns the lowest-index matching entry, or NULL. | |
92 const HpackEntry* GetByNameAndValue(SpdyStringPiece name, | |
93 SpdyStringPiece value); | |
94 | |
95 // Returns the index of an entry within this header table. | |
96 size_t IndexOf(const HpackEntry* entry) const; | |
97 | |
98 // Sets the maximum size of the header table, evicting entries if | |
99 // necessary as described in 5.2. | |
100 void SetMaxSize(size_t max_size); | |
101 | |
102 // Sets the SETTINGS_HEADER_TABLE_SIZE bound of the table. Will call | |
103 // SetMaxSize() as needed to preserve max_size() <= settings_size_bound(). | |
104 void SetSettingsHeaderTableSize(size_t settings_size); | |
105 | |
106 // Determine the set of entries which would be evicted by the insertion | |
107 // of |name| & |value| into the table, as per section 4.4. No eviction | |
108 // actually occurs. The set is returned via range [begin_out, end_out). | |
109 void EvictionSet(SpdyStringPiece name, | |
110 SpdyStringPiece value, | |
111 EntryTable::iterator* begin_out, | |
112 EntryTable::iterator* end_out); | |
113 | |
114 // Adds an entry for the representation, evicting entries as needed. |name| | |
115 // and |value| must not be owned by an entry which could be evicted. The | |
116 // added HpackEntry is returned, or NULL is returned if all entries were | |
117 // evicted and the empty table is of insufficent size for the representation. | |
118 const HpackEntry* TryAddEntry(SpdyStringPiece name, SpdyStringPiece value); | |
119 | |
120 void DebugLogTableState() const; | |
121 | |
122 void set_debug_visitor(std::unique_ptr<DebugVisitorInterface> visitor) { | |
123 debug_visitor_ = std::move(visitor); | |
124 } | |
125 | |
126 // Returns the estimate of dynamically allocated memory in bytes. | |
127 size_t EstimateMemoryUsage() const; | |
128 | |
129 private: | |
130 // Returns number of evictions required to enter |name| & |value|. | |
131 size_t EvictionCountForEntry(SpdyStringPiece name, | |
132 SpdyStringPiece value) const; | |
133 | |
134 // Returns number of evictions required to reclaim |reclaim_size| table size. | |
135 size_t EvictionCountToReclaim(size_t reclaim_size) const; | |
136 | |
137 // Evicts |count| oldest entries from the table. | |
138 void Evict(size_t count); | |
139 | |
140 // |static_entries_| and |static_index_| are owned by HpackStaticTable | |
141 // singleton. | |
142 const EntryTable& static_entries_; | |
143 EntryTable dynamic_entries_; | |
144 | |
145 // Tracks the unique HpackEntry for a given header name and value. | |
146 const UnorderedEntrySet& static_index_; | |
147 | |
148 // Tracks the first static entry for each name in the static table. | |
149 const NameToEntryMap& static_name_index_; | |
150 | |
151 // Tracks the most recently inserted HpackEntry for a given header name and | |
152 // value. | |
153 UnorderedEntrySet dynamic_index_; | |
154 | |
155 // Tracks the most recently inserted HpackEntry for a given header name. | |
156 NameToEntryMap dynamic_name_index_; | |
157 | |
158 // Last acknowledged value for SETTINGS_HEADER_TABLE_SIZE. | |
159 size_t settings_size_bound_; | |
160 | |
161 // Estimated current and maximum byte size of the table. | |
162 // |max_size_| <= |settings_size_bound_| | |
163 size_t size_; | |
164 size_t max_size_; | |
165 | |
166 // Total number of table insertions which have occurred. Referenced by | |
167 // IndexOf() for determination of an HpackEntry's table index. | |
168 size_t total_insertions_; | |
169 | |
170 std::unique_ptr<DebugVisitorInterface> debug_visitor_; | |
171 | |
172 DISALLOW_COPY_AND_ASSIGN(HpackHeaderTable); | |
173 }; | |
174 | |
175 } // namespace net | |
176 | |
177 #endif // NET_SPDY_HPACK_HPACK_HEADER_TABLE_H_ | |
OLD | NEW |