join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
io_ring_buffer_epoll.hpp
Go to the documentation of this file.
1
25// =========================================================================
26// CLASS : IoRingBuffer
27// METHOD : registerBuffer
28// =========================================================================
29template <size_t Count, size_t Size>
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 if (JOIN_UNLIKELY (!arena.reserveAll ()))
41 {
42 lastError = make_error_code (Errc::InUse);
43 return -1;
44 }
45
46 _base = static_cast<char*> (arena.getPtr (0));
47 _release = [&arena] () {
48 arena.releaseAll ();
49 };
50 _size = Size;
51 _count = Count;
52 _group = group;
53 _free = 0;
54
55 for (uint16_t bid = 0; bid < _count - 1; ++bid)
56 {
57 *static_cast<uint16_t*> (get (bid)) = bid + 1;
58 }
59
60 *static_cast<uint16_t*> (get (_count - 1)) = _none;
61
62 return 0;
63}
64
65// =========================================================================
66// CLASS : IoRingBuffer
67// METHOD : unregisterBuffer
68// =========================================================================
70{
71 if (_base == nullptr)
72 {
73 return 0;
74 }
75
76 _release ();
77 _release = nullptr;
78 _base = nullptr;
79 _size = 0;
80 _count = 0;
81 _group = 0;
82 _free = _none;
83
84 return 0;
85}
86
87// =========================================================================
88// CLASS : IoRingBuffer
89// METHOD : select
90// =========================================================================
91inline int join::IoRingBuffer::select () noexcept
92{
93 if (JOIN_UNLIKELY (_free == _none))
94 {
95 return -1;
96 }
97
98 uint16_t bid = _free;
99 _free = *static_cast<uint16_t*> (get (bid));
100
101 return bid;
102}
103
104// =========================================================================
105// CLASS : IoRingBuffer
106// METHOD : recycle
107// =========================================================================
108inline void join::IoRingBuffer::recycle (uint16_t bid) noexcept
109{
110 *static_cast<uint16_t*> (get (bid)) = _free;
111 _free = bid;
112}
memory arena owning backend and managing one or more pools.
Definition memory.hpp:53
void * getPtr(uint32_t idx) const noexcept
get the pointer to a chunk by index.
Definition allocator.hpp:510
bool reserveAll() noexcept
reserve all chunks of a pool at once.
Definition allocator.hpp:521
void releaseAll() noexcept
release all chunks of a pool at once.
Definition allocator.hpp:531
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
int select() noexcept
take a free buffer.
Definition io_ring_buffer_epoll.hpp:91
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