Commit d7cda810 authored by Xavier Thompson's avatar Xavier Thompson

Improve ring_buffer.hpp

parent a3f4452a
...@@ -18,14 +18,12 @@ namespace typon::fdt::lock_free ...@@ -18,14 +18,12 @@ namespace typon::fdt::lock_free
using enum std::memory_order; using enum std::memory_order;
const u8 _bits;
const u64 _mask; const u64 _mask;
ring_buffer * _next; ring_buffer * _next;
std::atomic<T> * const _array; std::atomic<T> * const _array;
ring_buffer(u8 bits, ring_buffer * next = nullptr) noexcept ring_buffer(u8 bits, ring_buffer * next = nullptr) noexcept
: _bits(bits) : _mask((u64(1) << bits) - 1)
, _mask(this->capacity() - 1)
, _next(next) , _next(next)
, _array(new std::atomic<T>[this->capacity()]) , _array(new std::atomic<T>[this->capacity()])
{} {}
...@@ -41,7 +39,7 @@ namespace typon::fdt::lock_free ...@@ -41,7 +39,7 @@ namespace typon::fdt::lock_free
u64 capacity() noexcept u64 capacity() noexcept
{ {
return u64(1) << _bits; return _mask + 1;
} }
void put(u64 index, T object) noexcept void put(u64 index, T object) noexcept
...@@ -65,7 +63,8 @@ namespace typon::fdt::lock_free ...@@ -65,7 +63,8 @@ namespace typon::fdt::lock_free
ring_buffer * grow(u64 start, u64 end) noexcept ring_buffer * grow(u64 start, u64 end) noexcept
{ {
return fill(new ring_buffer(_bits + 1, this), start, end); auto buffer = new ring_buffer(std::countr_one(_mask) + 1, this);
return fill(buffer, start, end);
} }
ring_buffer * shrink(u64 start, u64 end) noexcept ring_buffer * shrink(u64 start, u64 end) noexcept
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment