join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
io_ring_buffer_uring.hpp
Go to the documentation of this file.
1
25// =========================================================================
26// CLASS : IoRingBuffer
27// METHOD : registerBuffer
28// =========================================================================
29template <size_t Count, size_t Size>
30int join::IoRingBuffer::registerBuffer (uint16_t group, LocalMem::Allocator<Count, Size>& arena)
31{
32 static_assert (isPow2 (Count), "buffer count must be a power of two");
33
34 if (JOIN_UNLIKELY (_base != nullptr))
35 {
36 lastError = make_error_code (Errc::InUse);
37 return -1;
38 }
39
40 _mapping = LocalMem (Count * sizeof (io_uring_buf));
41
42 if (JOIN_UNLIKELY (!arena.reserveAll ()))
43 {
44 lastError = make_error_code (Errc::InUse);
45 return -1;
46 }
47
48 io_uring_buf_reg reg = {};
49 reg.ring_addr = reinterpret_cast<uint64_t> (_mapping.get ());
50 reg.ring_entries = Count;
51 reg.bgid = group;
52
53 int ret = io_uring_register_buf_ring (_uring, &reg, 0);
54 if (JOIN_UNLIKELY (ret < 0))
55 {
56 arena.releaseAll ();
57 _mapping = LocalMem ();
58 lastError = std::error_code (-ret, std::system_category ());
59 return -1;
60 }
61
62 _ring = static_cast<io_uring_buf_ring*> (_mapping.get ());
63 _base = static_cast<char*> (arena.getPtr (0));
64 _release = [&arena] () {
65 arena.releaseAll ();
66 };
67 _size = Size;
68 _count = Count;
69 _group = group;
70
71 io_uring_buf_ring_init (_ring);
72
73 for (uint16_t bid = 0; bid < _count; ++bid)
74 {
75 io_uring_buf_ring_add (_ring, get (bid), _size, bid, static_cast<int> (_count - 1), bid);
76 }
77
78 io_uring_buf_ring_advance (_ring, static_cast<int> (_count));
79
80 return 0;
81}
82
83// =========================================================================
84// CLASS : IoRingBuffer
85// METHOD : unregisterBuffer
86// =========================================================================
87inline int join::IoRingBuffer::unregisterBuffer () noexcept
88{
89 if (_base == nullptr)
90 {
91 return 0;
92 }
93
94 int ret = io_uring_unregister_buf_ring (_uring, _group);
95 if (JOIN_UNLIKELY (ret < 0))
96 {
97 // LCOV_EXCL_START
98 lastError = std::error_code (-ret, std::system_category ());
99 return -1;
100 // LCOV_EXCL_STOP
101 }
102
103 _release ();
104 _release = nullptr;
105 _mapping = LocalMem ();
106 _ring = nullptr;
107 _base = nullptr;
108 _size = 0;
109 _count = 0;
110 _group = 0;
111
112 return 0;
113}
114
115// =========================================================================
116// CLASS : IoRingBuffer
117// METHOD : recycle
118// =========================================================================
119inline void join::IoRingBuffer::recycle (uint16_t bid) noexcept
120{
121 io_uring_buf_ring_add (_ring, get (bid), _size, bid, static_cast<int> (_count - 1), 0);
122 io_uring_buf_ring_advance (_ring, 1);
123}
void recycle(uint16_t bid) noexcept
give a buffer back.
Definition io_ring_buffer_epoll.hpp:108
uint16_t group() const noexcept
get the buffer group id.
Definition io_ring_buffer.hpp:157
int unregisterBuffer() noexcept
withdraw the buffers and give them back to the arena.
Definition io_ring_buffer_epoll.hpp:69
void * get(uint16_t bid) const noexcept
get the address of a buffer.
Definition io_ring_buffer.hpp:148
int registerBuffer(uint16_t group, LocalMem::Allocator< Count, Size > &arena)
provide the arena buffers to the backend.
Definition io_ring_buffer_epoll.hpp:30
constexpr bool isPow2(uint64_t value) noexcept
check if a value is a power of two.
Definition utils.hpp:496
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
#define JOIN_UNLIKELY(x)
Definition utils.hpp:46