• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef QUICHE_SPDY_CORE_HTTP2_HEADER_STORAGE_H_
2 #define QUICHE_SPDY_CORE_HTTP2_HEADER_STORAGE_H_
3 
4 #include "absl/strings/string_view.h"
5 #include "quiche/common/platform/api/quiche_export.h"
6 #include "quiche/spdy/core/spdy_simple_arena.h"
7 
8 namespace spdy {
9 
10 // This class provides a backing store for absl::string_views. It previously
11 // used custom allocation logic, but now uses an UnsafeArena instead. It has the
12 // property that absl::string_views that refer to data in Http2HeaderStorage are
13 // never invalidated until the Http2HeaderStorage is deleted or Clear() is
14 // called.
15 //
16 // Write operations always append to the last block. If there is not enough
17 // space to perform the write, a new block is allocated, and any unused space
18 // is wasted.
19 class QUICHE_EXPORT Http2HeaderStorage {
20  public:
21   Http2HeaderStorage();
22 
23   Http2HeaderStorage(const Http2HeaderStorage&) = delete;
24   Http2HeaderStorage& operator=(const Http2HeaderStorage&) = delete;
25 
26   Http2HeaderStorage(Http2HeaderStorage&& other) = default;
27   Http2HeaderStorage& operator=(Http2HeaderStorage&& other) = default;
28 
29   absl::string_view Write(absl::string_view s);
30 
31   // If |s| points to the most recent allocation from arena_, the arena will
32   // reclaim the memory. Otherwise, this method is a no-op.
33   void Rewind(absl::string_view s);
34 
Clear()35   void Clear() { arena_.Reset(); }
36 
37   // Given a list of fragments and a separator, writes the fragments joined by
38   // the separator to a contiguous region of memory. Returns a absl::string_view
39   // pointing to the region of memory.
40   absl::string_view WriteFragments(
41       const std::vector<absl::string_view>& fragments,
42       absl::string_view separator);
43 
bytes_allocated()44   size_t bytes_allocated() const { return arena_.status().bytes_allocated(); }
45 
46  private:
47   SpdySimpleArena arena_;
48 };
49 
50 // Writes |fragments| to |dst|, joined by |separator|. |dst| must be large
51 // enough to hold the result. Returns the number of bytes written.
52 QUICHE_EXPORT size_t Join(char* dst,
53                           const std::vector<absl::string_view>& fragments,
54                           absl::string_view separator);
55 
56 }  // namespace spdy
57 
58 #endif  // QUICHE_SPDY_CORE_HTTP2_HEADER_STORAGE_H_
59