Add a QuicMemSlice constructor that takes a heap allocated buffer. Advantage: (1) For small message (e.g., 64 byte ones in time service client), there is no need to use an allocator. (2) Might make some unit tests easier to write. PiperOrigin-RevId: 343097958 Change-Id: I5ae41200ef5ba48c89049927e896e423914dfb64
diff --git a/quic/platform/api/quic_mem_slice.h b/quic/platform/api/quic_mem_slice.h index 2d44085..362f5dc 100644 --- a/quic/platform/api/quic_mem_slice.h +++ b/quic/platform/api/quic_mem_slice.h
@@ -5,6 +5,7 @@ #ifndef QUICHE_QUIC_PLATFORM_API_QUIC_MEM_SLICE_H_ #define QUICHE_QUIC_PLATFORM_API_QUIC_MEM_SLICE_H_ +#include <memory> #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" #include "net/quic/platform/impl/quic_mem_slice_impl.h" @@ -33,6 +34,11 @@ QuicMemSlice(QuicUniqueBufferPtr buffer, size_t length) : impl_(std::move(buffer), length) {} + // Constructs a QuicMemSlice that takes ownership of |buffer| allocated on + // heap. |length| must not be zero. + QuicMemSlice(std::unique_ptr<char[]> buffer, size_t length) + : impl_(std::move(buffer), length) {} + // Constructs QuicMemSlice from |impl|. It takes the reference away from // |impl|. explicit QuicMemSlice(QuicMemSliceImpl impl) : impl_(std::move(impl)) {}
diff --git a/quic/platform/api/quic_mem_slice_test.cc b/quic/platform/api/quic_mem_slice_test.cc index d441afa..3855010 100644 --- a/quic/platform/api/quic_mem_slice_test.cc +++ b/quic/platform/api/quic_mem_slice_test.cc
@@ -4,6 +4,7 @@ #include "net/third_party/quiche/src/quic/platform/api/quic_mem_slice.h" +#include <memory> #include "net/third_party/quiche/src/quic/core/quic_simple_buffer_allocator.h" #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" @@ -45,6 +46,16 @@ EXPECT_TRUE(slice_.empty()); } +TEST_F(QuicMemSliceTest, SliceAllocatedOnHeap) { + auto buffer = std::make_unique<char[]>(128); + char* orig_data = buffer.get(); + size_t used_length = 105; + QuicMemSlice slice = QuicMemSlice(std::move(buffer), used_length); + QuicMemSlice moved = std::move(slice); + EXPECT_EQ(moved.data(), orig_data); + EXPECT_EQ(moved.length(), used_length); +} + } // namespace } // namespace test } // namespace quic