From 54d30372c063acda1c5ee09f160557c8b2dd5ba4 Mon Sep 17 00:00:00 2001 From: wokron Date: Sat, 18 Apr 2026 22:11:55 +0800 Subject: [PATCH] add ManagedBuffer --- include/condy/detail/buffers.hpp | 79 ++++++++++++++++++++++++++++++ include/condy/provided_buffers.hpp | 66 ++----------------------- 2 files changed, 83 insertions(+), 62 deletions(-) create mode 100644 include/condy/detail/buffers.hpp diff --git a/include/condy/detail/buffers.hpp b/include/condy/detail/buffers.hpp new file mode 100644 index 00000000..59904ba8 --- /dev/null +++ b/include/condy/detail/buffers.hpp @@ -0,0 +1,79 @@ +/** + * @file buffers.hpp + * @brief Basic buffer types and conversion utilities. + * @details This file defines basic buffer types and conversion functions. + * Buffer types are primarily used in asynchronous operations. + */ + +#pragma once + +#include "condy/detail/utils.hpp" +#include +#include +#include +#include + +namespace condy { +namespace detail { + +template struct ManagedBuffer { +public: + using CondyBuffer = void; + + ManagedBuffer() = default; + ManagedBuffer(void *data, size_t size, BufferPool *pool) + : data_(data), size_(size), pool_(pool) {} + ManagedBuffer(ManagedBuffer &&other) noexcept + : data_(std::exchange(other.data_, nullptr)), + size_(std::exchange(other.size_, 0)), + pool_(std::exchange(other.pool_, nullptr)) {} + ManagedBuffer &operator=(ManagedBuffer &&other) noexcept { + if (this != &other) { + reset(); + data_ = std::exchange(other.data_, nullptr); + size_ = std::exchange(other.size_, 0); + pool_ = std::exchange(other.pool_, nullptr); + } + return *this; + } + + ~ManagedBuffer() { reset(); } + + CONDY_DELETE_COPY(ManagedBuffer); + +public: + /** + * @brief Get the data pointer of the buffer + */ + void *data() const noexcept { return data_; } + + /** * + * @brief Get the size of the buffer + */ + size_t size() const noexcept { return size_; } + + /** + * @brief Reset the buffer, returning it to the pool if owned + */ + void reset() noexcept { + if (pool_ != nullptr) { + pool_->add_buffer_back(data_, size_); + } + data_ = nullptr; + size_ = 0; + pool_ = nullptr; + } + + /** + * @brief Check if the buffer owns a buffer from a pool. + */ + bool owns_buffer() const noexcept { return pool_ != nullptr; } + +private: + void *data_ = nullptr; + size_t size_ = 0; + BufferPool *pool_ = nullptr; +}; + +} // namespace detail +} // namespace condy \ No newline at end of file diff --git a/include/condy/provided_buffers.hpp b/include/condy/provided_buffers.hpp index a118020b..2bd66be3 100644 --- a/include/condy/provided_buffers.hpp +++ b/include/condy/provided_buffers.hpp @@ -9,6 +9,7 @@ #include "condy/concepts.hpp" #include "condy/condy_uring.hpp" +#include "condy/detail/buffers.hpp" #include "condy/detail/context.hpp" #include "condy/detail/ring.hpp" #include "condy/detail/utils.hpp" @@ -241,58 +242,7 @@ class BundledProvidedBufferPool; * @note The lifetime of the provided buffer must not exceed the lifetime of the * provided buffer pool it is associated with. */ -struct ProvidedBuffer { -public: - using CondyBuffer = void; - - ProvidedBuffer() = default; - ProvidedBuffer(void *data, size_t size, - detail::BundledProvidedBufferPool *pool) - : data_(data), size_(size), pool_(pool) {} - ProvidedBuffer(ProvidedBuffer &&other) noexcept - : data_(std::exchange(other.data_, nullptr)), - size_(std::exchange(other.size_, 0)), - pool_(std::exchange(other.pool_, nullptr)) {} - ProvidedBuffer &operator=(ProvidedBuffer &&other) noexcept { - if (this != &other) { - reset(); - data_ = std::exchange(other.data_, nullptr); - size_ = std::exchange(other.size_, 0); - pool_ = std::exchange(other.pool_, nullptr); - } - return *this; - } - - ~ProvidedBuffer() { reset(); } - - CONDY_DELETE_COPY(ProvidedBuffer); - -public: - /** - * @brief Get the data pointer of the provided buffer - */ - void *data() const noexcept { return data_; } - - /** * - * @brief Get the size of the provided buffer - */ - size_t size() const noexcept { return size_; } - - /** - * @brief Reset the provided buffer, returning it to the pool if owned - */ - void reset() noexcept; - - /** - * @brief Check if the provided buffer owns a buffer from a pool. - */ - bool owns_buffer() const noexcept { return pool_ != nullptr; } - -private: - void *data_ = nullptr; - size_t size_ = 0; - detail::BundledProvidedBufferPool *pool_ = nullptr; -}; +using ProvidedBuffer = detail::ManagedBuffer; namespace detail { @@ -428,7 +378,8 @@ class BundledProvidedBufferPool { return buffers; } - void add_buffer_back(void *ptr) noexcept { + void add_buffer_back(void *ptr, [[maybe_unused]] size_t size) noexcept { + assert(size <= buffer_size_); char *base = get_buffers_base_(); assert(ptr >= base); size_t offset = static_cast(ptr) - base; @@ -468,15 +419,6 @@ class BundledProvidedBufferPool { } // namespace detail -inline void ProvidedBuffer::reset() noexcept { - if (pool_ != nullptr) { - pool_->add_buffer_back(data_); - } - data_ = nullptr; - size_ = 0; - pool_ = nullptr; -} - /** * @brief Provided buffer pool. * @details A provided buffer pool manages a pool of buffers that can be used in