QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2017 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_QUIC_PLATFORM_API_QUIC_MEM_SLICE_H_ |
| 6 | #define QUICHE_QUIC_PLATFORM_API_QUIC_MEM_SLICE_H_ |
| 7 | |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 9 | #include "net/quic/platform/impl/quic_mem_slice_impl.h" |
| 10 | |
danzh | 48ba61a | 2019-04-30 12:05:42 -0700 | [diff] [blame] | 11 | /* API_DESCRIPTION |
| 12 | QuicMemSlice is used to wrap application data and pass to QUIC stream's write |
| 13 | interface. It refers to a memory block of data which should be around till |
| 14 | QuicMemSlice::Reset() is called. It's upto each platform, to implement it as |
| 15 | reference counted or not. |
| 16 | API-DESCRIPTION */ |
| 17 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | namespace quic { |
| 19 | |
| 20 | // QuicMemSlice is an internally reference counted data buffer used as the |
| 21 | // source buffers for write operations. QuicMemSlice implicitly maintains a |
| 22 | // reference count and will free the underlying data buffer when the reference |
| 23 | // count reaches zero. |
| 24 | class QUIC_EXPORT_PRIVATE QuicMemSlice { |
| 25 | public: |
| 26 | // Constructs a empty QuicMemSlice with no underlying data and 0 reference |
| 27 | // count. |
| 28 | QuicMemSlice() = default; |
| 29 | // Let |allocator| allocate a data buffer of |length|, then construct |
| 30 | // QuicMemSlice with reference count 1 from the allocated data buffer. |
| 31 | // Once all of the references to the allocated data buffer are released, |
| 32 | // |allocator| is responsible to free the memory. |allocator| must |
| 33 | // not be null, and |length| must not be 0. To construct an empty |
| 34 | // QuicMemSlice, use the zero-argument constructor instead. |
| 35 | QuicMemSlice(QuicBufferAllocator* allocator, size_t length) |
| 36 | : impl_(allocator, length) {} |
| 37 | |
| 38 | // Constructs QuicMemSlice from |impl|. It takes the reference away from |
| 39 | // |impl|. |
| 40 | explicit QuicMemSlice(QuicMemSliceImpl impl) : impl_(std::move(impl)) {} |
| 41 | |
| 42 | QuicMemSlice(const QuicMemSlice& other) = delete; |
| 43 | QuicMemSlice& operator=(const QuicMemSlice& other) = delete; |
| 44 | |
| 45 | // Move constructors. |other| will not hold a reference to the data buffer |
| 46 | // after this call completes. |
| 47 | QuicMemSlice(QuicMemSlice&& other) = default; |
| 48 | QuicMemSlice& operator=(QuicMemSlice&& other) = default; |
| 49 | |
| 50 | ~QuicMemSlice() = default; |
| 51 | |
| 52 | // Release the underlying reference. Further access the memory will result in |
| 53 | // undefined behavior. |
| 54 | void Reset() { impl_.Reset(); } |
| 55 | |
| 56 | // Returns a const char pointer to underlying data buffer. |
| 57 | const char* data() const { return impl_.data(); } |
| 58 | // Returns the length of underlying data buffer. |
| 59 | size_t length() const { return impl_.length(); } |
| 60 | |
| 61 | bool empty() const { return impl_.empty(); } |
| 62 | |
QUICHE team | 4e952e7 | 2019-05-10 15:44:24 -0700 | [diff] [blame] | 63 | QuicMemSliceImpl* impl() { return &impl_; } |
| 64 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 65 | private: |
| 66 | QuicMemSliceImpl impl_; |
| 67 | }; |
| 68 | |
| 69 | } // namespace quic |
| 70 | |
| 71 | #endif // QUICHE_QUIC_PLATFORM_API_QUIC_MEM_SLICE_H_ |