Provide a default implementaion of QuicheMemSlice.
Also add a test for it, using a script that allows to use the same unit test for both the default and the override.
PiperOrigin-RevId: 431782432
diff --git a/common/platform/api/quiche_mem_slice.h b/common/platform/api/quiche_mem_slice.h
index c7a8fd6..61249bc 100644
--- a/common/platform/api/quiche_mem_slice.h
+++ b/common/platform/api/quiche_mem_slice.h
@@ -7,28 +7,18 @@
#include <memory>
+#include "quiche_platform_impl/quiche_mem_slice_impl.h"
#include "absl/strings/string_view.h"
#include "quic/core/quic_buffer_allocator.h"
#include "common/platform/api/quiche_export.h"
-#include "net/quiche/common/platform/impl/quiche_mem_slice_impl.h"
-
-/* API_DESCRIPTION
- QuicheMemSlice is used to wrap application data and pass to QUIC stream's write
- interface. It refers to a memory block of data which should be around till
- QuicheMemSlice::Reset() is called. It's upto each platform, to implement it as
- reference counted or not.
- API-DESCRIPTION */
namespace quiche {
-// QuicheMemSlice is an internally reference counted data buffer used as the
-// source buffers for write operations. QuicheMemSlice implicitly maintains a
-// reference count and will free the underlying data buffer when the reference
-// count reaches zero.
+// QuicheMemSlice is a wrapper around a platform-specific I/O buffer type. It
+// may be reference counted, though QUICHE itself does not rely on that.
class QUICHE_EXPORT_PRIVATE QuicheMemSlice {
public:
- // Constructs a empty QuicheMemSlice with no underlying data and 0 reference
- // count.
+ // Constructs a empty QuicheMemSlice with no underlying data.
QuicheMemSlice() = default;
// Constructs a QuicheMemSlice that takes ownership of |buffer|. The length
diff --git a/common/platform/default/quiche_platform_impl/quiche_mem_slice_impl.h b/common/platform/default/quiche_platform_impl/quiche_mem_slice_impl.h
new file mode 100644
index 0000000..0d13dec
--- /dev/null
+++ b/common/platform/default/quiche_platform_impl/quiche_mem_slice_impl.h
@@ -0,0 +1,46 @@
+#ifndef QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_MEM_SLICE_IMPL_H_
+#define QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_MEM_SLICE_IMPL_H_
+
+#include "quic/core/quic_buffer_allocator.h"
+#include "quic/core/quic_simple_buffer_allocator.h"
+#include "common/platform/api/quiche_export.h"
+
+namespace quiche {
+
+class QUICHE_EXPORT_PRIVATE QuicheMemSliceImpl {
+ public:
+ QuicheMemSliceImpl() = default;
+
+ explicit QuicheMemSliceImpl(quic::QuicBuffer buffer)
+ : buffer_(std::move(buffer)) {}
+
+ QuicheMemSliceImpl(std::unique_ptr<char[]> buffer, size_t length)
+ : buffer_(quic::QuicBuffer(
+ quic::QuicUniqueBufferPtr(
+ buffer.release(),
+ quic::QuicBufferDeleter(quic::SimpleBufferAllocator::Get())),
+ length)) {}
+
+ QuicheMemSliceImpl(const QuicheMemSliceImpl& other) = delete;
+ QuicheMemSliceImpl& operator=(const QuicheMemSliceImpl& other) = delete;
+
+ // Move constructors. |other| will not hold a reference to the data buffer
+ // after this call completes.
+ QuicheMemSliceImpl(QuicheMemSliceImpl&& other) = default;
+ QuicheMemSliceImpl& operator=(QuicheMemSliceImpl&& other) = default;
+
+ ~QuicheMemSliceImpl() = default;
+
+ void Reset() { buffer_ = quic::QuicBuffer(); }
+
+ const char* data() const { return buffer_.data(); }
+ size_t length() const { return buffer_.size(); }
+ bool empty() const { return buffer_.empty(); }
+
+ private:
+ quic::QuicBuffer buffer_;
+};
+
+} // namespace quiche
+
+#endif // QUICHE_COMMON_PLATFORM_DEFAULT_QUICHE_PLATFORM_IMPL_QUICHE_MEM_SLICE_IMPL_H_
diff --git a/quic/core/quic_buffer_allocator.h b/quic/core/quic_buffer_allocator.h
index de69ca2..4d62f84 100644
--- a/quic/core/quic_buffer_allocator.h
+++ b/quic/core/quic_buffer_allocator.h
@@ -66,6 +66,9 @@
QuicBuffer(QuicBufferAllocator* allocator, size_t size)
: buffer_(MakeUniqueBuffer(allocator, size)), size_(size) {}
+ QuicBuffer(QuicUniqueBufferPtr buffer, size_t size)
+ : buffer_(std::move(buffer)), size_(size) {}
+
// Make sure the move constructor zeroes out the size field.
QuicBuffer(QuicBuffer&& other)
: buffer_(std::move(other.buffer_)), size_(other.size_) {
diff --git a/quic/core/quic_simple_buffer_allocator.h b/quic/core/quic_simple_buffer_allocator.h
index c2a800d..62f406b 100644
--- a/quic/core/quic_simple_buffer_allocator.h
+++ b/quic/core/quic_simple_buffer_allocator.h
@@ -10,6 +10,9 @@
namespace quic {
+// Provides buffer allocation using operators new[] and delete[] on char arrays.
+// Note that some of the QUICHE code relies on this being the case for deleting
+// new[]-allocated arrays from elsewhere.
class QUIC_EXPORT_PRIVATE SimpleBufferAllocator : public QuicBufferAllocator {
public:
static SimpleBufferAllocator* Get() {