实现 ASIO 自定义执行处理程序

Implement ASIO custom execution handlers

提问人:getsoubl 提问时间:9/1/2023 最后编辑:Nicol Bolasgetsoubl 更新时间:9/1/2023 访问量:26

问:

我正在学习 asio 教程以及如何实现从特定用户定义区域分配内存的 asio 处理程序。 就我而言,我试图从synchronized_pool_resource中分配内存 在下面找到基于 asio 示例的代码段

我幼稚的功能

#include <array>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <type_traits>
#include <utility>
#include "boost/asio.hpp"
#include <memory_resource>


class MemoryStorage
{
public:
    inline static char buffer[0124] {}; // a small buffer on the stack
    inline static std::pmr::monotonic_buffer_resource pool{std::data(buffer), std::size(buffer)};
    inline static std::pmr::synchronized_pool_resource mem {&pool};
};

class handler_memory
{
public:
  handler_memory()
  {
  }

  handler_memory(const handler_memory&) = delete;
  handler_memory& operator=(const handler_memory&) = delete;

  void* allocate(std::size_t size)
  {
    std::cout << "allocate " << size <<std::endl;
    memory_block_size_ = size;
    return _upstream->allocate(size);
  }

  void deallocate(void* pointer,std::size_t size)
  {
    std::cout << "deallocate" << size << std::endl;
     return _upstream->deallocate(pointer,memory_block_size_);
  }

private:
  
  std::pmr::memory_resource* _upstream = &MemoryStorage::mem;
  std::size_t memory_block_size_ {};
};

template <typename T>
class handler_allocator
{
public:
  using value_type = T;

  explicit handler_allocator(handler_memory& mem)
    : memory_(mem)
  {
  }

  template <typename U>
  handler_allocator(const handler_allocator<U>& other) noexcept
    : memory_(other.memory_)
  {
  }

  template <typename U>
  struct rebind
  {
    typedef handler_allocator<U> other;
  };


  bool operator==(const handler_allocator& other) const noexcept
  {
    return &memory_ == &other.memory_;
  }

  bool operator!=(const handler_allocator& other) const noexcept
  {
    return &memory_ != &other.memory_;
  }

  T* allocate(std::size_t n) const
  {
    std::cout << "allocate" << n << "," << sizeof(T) << std::endl;
    return static_cast<T*>(memory_.allocate(sizeof(T) * n));
  }

  void deallocate(T* p, std::size_t n) const
  {
    return memory_.deallocate(p,sizeof(T) * n);
  }

private:
  template <typename> friend class handler_allocator;

  // The underlying memory.
  handler_memory& memory_;
};

class session
  : public std::enable_shared_from_this<session>
{

public:

  handler_memory handler_memory_;
  void foo()
  {
        boost::system::error_code ec;
        auto handler = [](boost::system::error_code ec){std::cout << "hello world"<<std::endl;};

         auto  allocator_handler =   boost::asio::bind_allocator(
        handler_allocator<std::decay<decltype(handler)>::type>(handler_memory_),std::move(handler));
       boost::asio::system_timer timer(io_context_);
        timer.expires_after(std::chrono::seconds{1});
        timer.async_wait(boost::asio::bind_executor(io_context_, std::move(allocator_handler)));
        io_context_.run();
  }
  boost::asio::io_context io_context_;
};

int main(int argc, char* argv[])
{
    s.foo();
  return 0;
}

专注于foo功能 我几乎不明白为什么在构造 allocator_handler 时,将 handler_allocator 中的 T 型显式推导出为 int,如下例所示,

auto  allocator_handler =   boost::asio::bind_allocator(
        handler_allocator<int>(handler_memory_),std::move(handler));

并打印 sizeof(T),这不是 4 个字节,而是 144 个字节。不知何故,在我的观点中,它是在整个 lamda 对象中推导出来的。但问题是如何?演示

C++ 模板 ASIO C++PMR

评论

0赞 getsoubl 9/1/2023
对此有何评论?

答: 暂无答案