// // buffer_registration.hpp // ~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_BUFFER_REGISTRATION_HPP #define BOOST_ASIO_BUFFER_REGISTRATION_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include #include #include #include #if defined(BOOST_ASIO_HAS_IO_URING) # include # include #endif // defined(BOOST_ASIO_HAS_IO_URING) #if defined(BOOST_ASIO_HAS_MOVE) # include #endif // defined(BOOST_ASIO_HAS_MOVE) #include namespace boost { namespace asio { namespace detail { class buffer_registration_base { protected: static mutable_registered_buffer make_buffer(const mutable_buffer& b, const void* scope, int index) BOOST_ASIO_NOEXCEPT { return mutable_registered_buffer(b, registered_buffer_id(scope, index)); } }; } // namespace detail /// Automatically registers and unregistered buffers with an execution context. /** * For portability, applications should assume that only one registration is * permitted per execution context. */ template > class buffer_registration : detail::buffer_registration_base { public: /// The allocator type used for allocating storage for the buffers container. typedef Allocator allocator_type; #if defined(GENERATING_DOCUMENTATION) /// The type of an iterator over the registered buffers. typedef unspecified iterator; /// The type of a const iterator over the registered buffers. typedef unspecified const_iterator; #else // defined(GENERATING_DOCUMENTATION) typedef std::vector::const_iterator iterator; typedef std::vector::const_iterator const_iterator; #endif // defined(GENERATING_DOCUMENTATION) /// Register buffers with an executor's execution context. template buffer_registration(const Executor& ex, const MutableBufferSequence& buffer_sequence, const allocator_type& alloc = allocator_type(), typename constraint< is_executor::value || execution::is_executor::value >::type = 0) : buffer_sequence_(buffer_sequence), buffers_( BOOST_ASIO_REBIND_ALLOC(allocator_type, mutable_registered_buffer)(alloc)) { init_buffers(buffer_registration::get_context(ex), boost::asio::buffer_sequence_begin(buffer_sequence_), boost::asio::buffer_sequence_end(buffer_sequence_)); } /// Register buffers with an execution context. template buffer_registration(ExecutionContext& ctx, const MutableBufferSequence& buffer_sequence, const allocator_type& alloc = allocator_type(), typename constraint< is_convertible::value >::type = 0) : buffer_sequence_(buffer_sequence), buffers_( BOOST_ASIO_REBIND_ALLOC(allocator_type, mutable_registered_buffer)(alloc)) { init_buffers(ctx, boost::asio::buffer_sequence_begin(buffer_sequence_), boost::asio::buffer_sequence_end(buffer_sequence_)); } #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Move constructor. buffer_registration(buffer_registration&& other) BOOST_ASIO_NOEXCEPT : buffer_sequence_(std::move(other.buffer_sequence_)), buffers_(std::move(other.buffers_)) { #if defined(BOOST_ASIO_HAS_IO_URING) service_ = other.service_; other.service_ = 0; #endif // defined(BOOST_ASIO_HAS_IO_URING) } #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Unregisters the buffers. ~buffer_registration() { #if defined(BOOST_ASIO_HAS_IO_URING) if (service_) service_->unregister_buffers(); #endif // defined(BOOST_ASIO_HAS_IO_URING) } #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Move assignment. buffer_registration& operator=( buffer_registration&& other) BOOST_ASIO_NOEXCEPT { if (this != &other) { buffer_sequence_ = std::move(other.buffer_sequence_); buffers_ = std::move(other.buffers_); #if defined(BOOST_ASIO_HAS_IO_URING) if (service_) service_->unregister_buffers(); service_ = other.service_; other.service_ = 0; #endif // defined(BOOST_ASIO_HAS_IO_URING) } return *this; } #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Get the number of registered buffers. std::size_t size() const BOOST_ASIO_NOEXCEPT { return buffers_.size(); } /// Get the begin iterator for the sequence of registered buffers. const_iterator begin() const BOOST_ASIO_NOEXCEPT { return buffers_.begin(); } /// Get the begin iterator for the sequence of registered buffers. const_iterator cbegin() const BOOST_ASIO_NOEXCEPT { return buffers_.cbegin(); } /// Get the end iterator for the sequence of registered buffers. const_iterator end() const BOOST_ASIO_NOEXCEPT { return buffers_.end(); } /// Get the end iterator for the sequence of registered buffers. const_iterator cend() const BOOST_ASIO_NOEXCEPT { return buffers_.cend(); } /// Get the buffer at the specified index. const mutable_registered_buffer& operator[](std::size_t i) BOOST_ASIO_NOEXCEPT { return buffers_[i]; } /// Get the buffer at the specified index. const mutable_registered_buffer& at(std::size_t i) BOOST_ASIO_NOEXCEPT { return buffers_.at(i); } private: // Disallow copying and assignment. buffer_registration(const buffer_registration&) BOOST_ASIO_DELETED; buffer_registration& operator=(const buffer_registration&) BOOST_ASIO_DELETED; // Helper function to get an executor's context. template static execution_context& get_context(const T& t, typename enable_if::value>::type* = 0) { return boost::asio::query(t, execution::context); } // Helper function to get an executor's context. template static execution_context& get_context(const T& t, typename enable_if::value>::type* = 0) { return t.context(); } // Helper function to initialise the container of buffers. template void init_buffers(execution_context& ctx, Iterator begin, Iterator end) { std::size_t n = std::distance(begin, end); buffers_.resize(n); #if defined(BOOST_ASIO_HAS_IO_URING) service_ = &use_service(ctx); std::vector iovecs(n, BOOST_ASIO_REBIND_ALLOC(allocator_type, iovec)( buffers_.get_allocator())); #endif // defined(BOOST_ASIO_HAS_IO_URING) Iterator iter = begin; for (int index = 0; iter != end; ++index, ++iter) { mutable_buffer b(*iter); std::size_t i = static_cast(index); buffers_[i] = this->make_buffer(b, &ctx, index); #if defined(BOOST_ASIO_HAS_IO_URING) iovecs[i].iov_base = buffers_[i].data(); iovecs[i].iov_len = buffers_[i].size(); #endif // defined(BOOST_ASIO_HAS_IO_URING) } #if defined(BOOST_ASIO_HAS_IO_URING) if (n > 0) { service_->register_buffers(&iovecs[0], static_cast(iovecs.size())); } #endif // defined(BOOST_ASIO_HAS_IO_URING) } MutableBufferSequence buffer_sequence_; std::vector buffers_; #if defined(BOOST_ASIO_HAS_IO_URING) detail::io_uring_service* service_; #endif // defined(BOOST_ASIO_HAS_IO_URING) }; #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Register buffers with an execution context. template BOOST_ASIO_NODISCARD inline buffer_registration register_buffers(const Executor& ex, const MutableBufferSequence& buffer_sequence, typename constraint< is_executor::value || execution::is_executor::value >::type = 0) { return buffer_registration(ex, buffer_sequence); } /// Register buffers with an execution context. template BOOST_ASIO_NODISCARD inline buffer_registration register_buffers(const Executor& ex, const MutableBufferSequence& buffer_sequence, const Allocator& alloc, typename constraint< is_executor::value || execution::is_executor::value >::type = 0) { return buffer_registration( ex, buffer_sequence, alloc); } /// Register buffers with an execution context. template BOOST_ASIO_NODISCARD inline buffer_registration register_buffers(ExecutionContext& ctx, const MutableBufferSequence& buffer_sequence, typename constraint< is_convertible::value >::type = 0) { return buffer_registration(ctx, buffer_sequence); } /// Register buffers with an execution context. template BOOST_ASIO_NODISCARD inline buffer_registration register_buffers(ExecutionContext& ctx, const MutableBufferSequence& buffer_sequence, const Allocator& alloc, typename constraint< is_convertible::value >::type = 0) { return buffer_registration( ctx, buffer_sequence, alloc); } #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_BUFFER_REGISTRATION_HPP