Fold QuicheDefaultMemSliceImpl into QuicheMemSlice. PiperOrigin-RevId: 736952984
diff --git a/build/source_list.bzl b/build/source_list.bzl index e56d4c0..17ab549 100644 --- a/build/source_list.bzl +++ b/build/source_list.bzl
@@ -46,7 +46,6 @@ "common/quiche_crypto_logging.h", "common/quiche_data_reader.h", "common/quiche_data_writer.h", - "common/quiche_default_mem_slice_impl.h", "common/quiche_endian.h", "common/quiche_feature_flags_list.h", "common/quiche_intrusive_list.h", @@ -419,6 +418,7 @@ "common/masque/connect_ip_datagram_payload.cc", "common/masque/connect_udp_datagram_payload.cc", "common/platform/api/quiche_hostname_utils.cc", + "common/platform/api/quiche_mem_slice.cc", "common/quiche_buffer_allocator.cc", "common/quiche_crypto_logging.cc", "common/quiche_data_reader.cc",
diff --git a/build/source_list.gni b/build/source_list.gni index 17141aa..5465b6a 100644 --- a/build/source_list.gni +++ b/build/source_list.gni
@@ -46,7 +46,6 @@ "src/quiche/common/quiche_crypto_logging.h", "src/quiche/common/quiche_data_reader.h", "src/quiche/common/quiche_data_writer.h", - "src/quiche/common/quiche_default_mem_slice_impl.h", "src/quiche/common/quiche_endian.h", "src/quiche/common/quiche_feature_flags_list.h", "src/quiche/common/quiche_intrusive_list.h", @@ -419,6 +418,7 @@ "src/quiche/common/masque/connect_ip_datagram_payload.cc", "src/quiche/common/masque/connect_udp_datagram_payload.cc", "src/quiche/common/platform/api/quiche_hostname_utils.cc", + "src/quiche/common/platform/api/quiche_mem_slice.cc", "src/quiche/common/quiche_buffer_allocator.cc", "src/quiche/common/quiche_crypto_logging.cc", "src/quiche/common/quiche_data_reader.cc",
diff --git a/build/source_list.json b/build/source_list.json index 9622054..1a9fe83 100644 --- a/build/source_list.json +++ b/build/source_list.json
@@ -45,7 +45,6 @@ "quiche/common/quiche_crypto_logging.h", "quiche/common/quiche_data_reader.h", "quiche/common/quiche_data_writer.h", - "quiche/common/quiche_default_mem_slice_impl.h", "quiche/common/quiche_endian.h", "quiche/common/quiche_feature_flags_list.h", "quiche/common/quiche_intrusive_list.h", @@ -418,6 +417,7 @@ "quiche/common/masque/connect_ip_datagram_payload.cc", "quiche/common/masque/connect_udp_datagram_payload.cc", "quiche/common/platform/api/quiche_hostname_utils.cc", + "quiche/common/platform/api/quiche_mem_slice.cc", "quiche/common/quiche_buffer_allocator.cc", "quiche/common/quiche_crypto_logging.cc", "quiche/common/quiche_data_reader.cc",
diff --git a/quiche/common/platform/api/quiche_mem_slice.cc b/quiche/common/platform/api/quiche_mem_slice.cc new file mode 100644 index 0000000..ffe7b7e --- /dev/null +++ b/quiche/common/platform/api/quiche_mem_slice.cc
@@ -0,0 +1,65 @@ +#include "quiche/common/platform/api/quiche_mem_slice.h" + +#include <cstddef> +#include <cstdlib> +#include <memory> +#include <utility> + +#include "quiche/common/quiche_buffer_allocator.h" +#include "quiche/common/quiche_callbacks.h" + +namespace quiche { + +QuicheMemSlice::QuicheMemSlice(QuicheBuffer buffer) + : data_(buffer.data()), size_(buffer.size()) { + QuicheUniqueBufferPtr owned = buffer.Release(); + QuicheBufferAllocator* allocator = owned.get_deleter().allocator(); + owned.release(); + done_callback_ = [allocator](const char* ptr) { + allocator->Delete(const_cast<char*>(ptr)); + }; +} + +QuicheMemSlice::QuicheMemSlice(std::unique_ptr<char[]> buffer, size_t length) + : data_(buffer.release()), + size_(length), + done_callback_(+[](const char* ptr) { delete[] ptr; }) {} + +QuicheMemSlice::QuicheMemSlice( + const char* buffer, size_t length, + SingleUseCallback<void(const char*)> done_callback) + : data_(buffer), size_(length), done_callback_(std::move(done_callback)) {} + +// Move constructors. |other| will not hold a reference to the data buffer +// after this call completes. +QuicheMemSlice::QuicheMemSlice(QuicheMemSlice&& other) { + data_ = other.data_; + size_ = other.size_; + done_callback_ = std::move(other.done_callback_); + other.data_ = nullptr; + other.size_ = 0; + other.done_callback_ = nullptr; +} +QuicheMemSlice& QuicheMemSlice::operator=(QuicheMemSlice&& other) { + Reset(); + data_ = other.data_; + size_ = other.size_; + done_callback_ = std::move(other.done_callback_); + other.data_ = nullptr; + other.size_ = 0; + other.done_callback_ = nullptr; + return *this; +} + +QuicheMemSlice::~QuicheMemSlice() { Reset(); } + +void QuicheMemSlice::Reset() { + if (done_callback_ && data_ != nullptr) { + std::move(done_callback_)(data_); + } + data_ = nullptr; + size_ = 0; + done_callback_ = nullptr; +} + +} // namespace quiche
diff --git a/quiche/common/platform/api/quiche_mem_slice.h b/quiche/common/platform/api/quiche_mem_slice.h index 311fd62..1223999 100644 --- a/quiche/common/platform/api/quiche_mem_slice.h +++ b/quiche/common/platform/api/quiche_mem_slice.h
@@ -7,64 +7,64 @@ #include <cstddef> #include <memory> -#include <utility> #include "absl/strings/string_view.h" #include "quiche/common/platform/api/quiche_export.h" #include "quiche/common/quiche_buffer_allocator.h" #include "quiche/common/quiche_callbacks.h" -#include "quiche/common/quiche_default_mem_slice_impl.h" namespace quiche { -// 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. +// QuicheMemSlice is a memory buffer with a type-erased deleter callback. class QUICHE_EXPORT QuicheMemSlice { public: + using ReleaseCallback = SingleUseCallback<void(const char*)>; + // Constructs a empty QuicheMemSlice with no underlying data. QuicheMemSlice() = default; // Constructs a QuicheMemSlice that takes ownership of |buffer|. The length // of the |buffer| must not be zero. To construct an empty QuicheMemSlice, // use the zero-argument constructor instead. - explicit QuicheMemSlice(QuicheBuffer buffer) : impl_(std::move(buffer)) {} + explicit QuicheMemSlice(QuicheBuffer buffer); // Constructs a QuicheMemSlice that takes ownership of |buffer| allocated on // heap. |length| must not be zero. - QuicheMemSlice(std::unique_ptr<char[]> buffer, size_t length) - : impl_(std::move(buffer), length) {} + QuicheMemSlice(std::unique_ptr<char[]> buffer, size_t length); + // Constructs a QuicheMemSlice with a custom deleter callback. QuicheMemSlice(const char* buffer, size_t length, - quiche::SingleUseCallback<void(const char*)> done_callback) - : impl_(buffer, length, std::move(done_callback)) {} + ReleaseCallback done_callback); QuicheMemSlice(const QuicheMemSlice& other) = delete; QuicheMemSlice& operator=(const QuicheMemSlice& other) = delete; // Move constructors. |other| will not hold a reference to the data buffer // after this call completes. - QuicheMemSlice(QuicheMemSlice&& other) = default; - QuicheMemSlice& operator=(QuicheMemSlice&& other) = default; + QuicheMemSlice(QuicheMemSlice&& other); + QuicheMemSlice& operator=(QuicheMemSlice&& other); - ~QuicheMemSlice() = default; + ~QuicheMemSlice(); // Release the underlying reference. Further access the memory will result in // undefined behavior. - void Reset() { impl_.Reset(); } + void Reset(); // Returns a const char pointer to underlying data buffer. - const char* data() const { return impl_.data(); } + const char* data() const { return data_; } // Returns the length of underlying data buffer. - size_t length() const { return impl_.length(); } + size_t length() const { return size_; } // Returns the representation of the underlying data as a string view. absl::string_view AsStringView() const { - return absl::string_view(data(), length()); + return absl::string_view(data_, size_); } - bool empty() const { return impl_.empty(); } + bool empty() const { return size_ == 0; } private: - QuicheDefaultMemSliceImpl impl_; + const char* data_ = nullptr; + size_t size_ = 0; + ReleaseCallback done_callback_ = nullptr; }; } // namespace quiche
diff --git a/quiche/common/quiche_default_mem_slice_impl.h b/quiche/common/quiche_default_mem_slice_impl.h deleted file mode 100644 index e2acde0..0000000 --- a/quiche/common/quiche_default_mem_slice_impl.h +++ /dev/null
@@ -1,89 +0,0 @@ -#ifndef QUICHE_COMMON_QUICHE_DEFAULT_MEM_SLICE_IMPL_H_ -#define QUICHE_COMMON_QUICHE_DEFAULT_MEM_SLICE_IMPL_H_ - -#include <cstddef> -#include <cstdlib> -#include <memory> -#include <utility> - -#include "quiche/common/platform/api/quiche_export.h" -#include "quiche/common/quiche_buffer_allocator.h" -#include "quiche/common/quiche_callbacks.h" - -namespace quiche { - -// The default (and soon, hopefully the only) implementation of QuicheMemSlice. -class QUICHE_EXPORT QuicheDefaultMemSliceImpl { - public: - QuicheDefaultMemSliceImpl() = default; - - explicit QuicheDefaultMemSliceImpl(QuicheBuffer buffer) - : data_(buffer.data()), size_(buffer.size()) { - QuicheUniqueBufferPtr owned = buffer.Release(); - QuicheBufferAllocator* allocator = owned.get_deleter().allocator(); - owned.release(); - done_callback_ = [allocator](const char* ptr) { - allocator->Delete(const_cast<char*>(ptr)); - }; - } - - QuicheDefaultMemSliceImpl(std::unique_ptr<char[]> buffer, size_t length) - : data_(buffer.release()), - size_(length), - done_callback_(+[](const char* ptr) { delete[] ptr; }) {} - - QuicheDefaultMemSliceImpl(const char* buffer, size_t length, - SingleUseCallback<void(const char*)> done_callback) - : data_(buffer), - size_(length), - done_callback_(std::move(done_callback)) {} - - QuicheDefaultMemSliceImpl(const QuicheDefaultMemSliceImpl& other) = delete; - QuicheDefaultMemSliceImpl& operator=(const QuicheDefaultMemSliceImpl& other) = - delete; - - // Move constructors. |other| will not hold a reference to the data buffer - // after this call completes. - QuicheDefaultMemSliceImpl(QuicheDefaultMemSliceImpl&& other) { - data_ = other.data_; - size_ = other.size_; - done_callback_ = std::move(other.done_callback_); - other.data_ = nullptr; - other.size_ = 0; - other.done_callback_ = nullptr; - } - QuicheDefaultMemSliceImpl& operator=(QuicheDefaultMemSliceImpl&& other) { - Reset(); - data_ = other.data_; - size_ = other.size_; - done_callback_ = std::move(other.done_callback_); - other.data_ = nullptr; - other.size_ = 0; - other.done_callback_ = nullptr; - return *this; - } - - ~QuicheDefaultMemSliceImpl() { Reset(); } - - void Reset() { - if (done_callback_ && data_ != nullptr) { - std::move(done_callback_)(data_); - } - data_ = nullptr; - size_ = 0; - done_callback_ = nullptr; - } - - const char* data() const { return data_; } - size_t length() const { return size_; } - bool empty() const { return size_ == 0; } - - private: - const char* data_ = nullptr; - size_t size_ = 0; - SingleUseCallback<void(const char*)> done_callback_ = nullptr; -}; - -} // namespace quiche - -#endif // QUICHE_COMMON_QUICHE_DEFAULT_MEM_SLICE_IMPL_H_