QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 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 QUICHE_SPDY_CORE_SPDY_PINNABLE_BUFFER_PIECE_H_ |
| 6 | #define QUICHE_SPDY_CORE_SPDY_PINNABLE_BUFFER_PIECE_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
| 10 | #include <memory> |
| 11 | |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 12 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 13 | #include "common/platform/api/quiche_export.h" |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 14 | |
| 15 | namespace spdy { |
| 16 | |
| 17 | class SpdyPrefixedBufferReader; |
| 18 | |
| 19 | // Helper class of SpdyPrefixedBufferReader. |
| 20 | // Represents a piece of consumed buffer which may (or may not) own its |
| 21 | // underlying storage. Users may "pin" the buffer at a later time to ensure |
| 22 | // a SpdyPinnableBufferPiece owns and retains storage of the buffer. |
bnc | 45ccf4b | 2020-01-21 19:05:27 -0800 | [diff] [blame] | 23 | struct QUICHE_EXPORT_PRIVATE SpdyPinnableBufferPiece { |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 24 | public: |
| 25 | SpdyPinnableBufferPiece(); |
| 26 | ~SpdyPinnableBufferPiece(); |
| 27 | |
| 28 | const char* buffer() const { return buffer_; } |
| 29 | |
vasilvv | c8ccdb2 | 2020-10-12 16:42:34 -0700 | [diff] [blame] | 30 | explicit operator absl::string_view() const { |
| 31 | return absl::string_view(buffer_, length_); |
QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Allocates and copies the buffer to internal storage. |
| 35 | void Pin(); |
| 36 | |
| 37 | bool IsPinned() const { return storage_ != nullptr; } |
| 38 | |
| 39 | // Swaps buffers, including internal storage, with |other|. |
| 40 | void Swap(SpdyPinnableBufferPiece* other); |
| 41 | |
| 42 | private: |
| 43 | friend class SpdyPrefixedBufferReader; |
| 44 | |
| 45 | const char* buffer_; |
| 46 | size_t length_; |
| 47 | // Null iff |buffer_| isn't pinned. |
| 48 | std::unique_ptr<char[]> storage_; |
| 49 | }; |
| 50 | |
| 51 | } // namespace spdy |
| 52 | |
| 53 | #endif // QUICHE_SPDY_CORE_SPDY_PINNABLE_BUFFER_PIECE_H_ |