Commit ecf733a1 authored by Xavier Thompson's avatar Xavier Thompson

deque.hpp: Improve shrink implementation

parent 35cdcff8
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define TYPON_FUNDAMENTAL_RINGBUFFER_HPP_INCLUDED #define TYPON_FUNDAMENTAL_RINGBUFFER_HPP_INCLUDED
#include <atomic> #include <atomic>
#include <bit>
namespace typon::fdt::lock_free namespace typon::fdt::lock_free
...@@ -18,7 +19,7 @@ namespace typon::fdt::lock_free ...@@ -18,7 +19,7 @@ namespace typon::fdt::lock_free
const u8 _bits; const u8 _bits;
const u64 _mask; const u64 _mask;
ring_buffer * const _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
...@@ -68,7 +69,24 @@ namespace typon::fdt::lock_free ...@@ -68,7 +69,24 @@ namespace typon::fdt::lock_free
ring_buffer * shrink(u64 start, u64 end) noexcept ring_buffer * shrink(u64 start, u64 end) noexcept
{ {
return fill(std::exchange(_next, nullptr), start, end); ring_buffer * last = nullptr;
ring_buffer * next = this;
u64 size = end - start;
while (auto buff = next->_next)
{
if (buff->capacity() < size * 4)
{
break;
}
last = next;
next = buff;
}
if (!last)
{
return nullptr;
}
last->_next = nullptr;
return fill(next, start, end);
} }
}; };
......
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