Make ConsumableQueue nogc.

This commit is contained in:
Sönke Ludwig 2018-10-21 20:15:37 +02:00
parent 90a60f7981
commit 4a605640bc

View file

@ -1,5 +1,8 @@
module eventcore.internal.consumablequeue;
import eventcore.internal.utils : mallocNT, freeNT;
/** FIFO queue with support for chunk-wise consumption.
*/
final class ConsumableQueue(T)
@ -18,6 +21,12 @@ final class ConsumableQueue(T)
size_t m_pendingCount;
}
~this()
@trusted @nogc nothrow {
if (m_storage !is null)
freeNT(m_storage);
}
@property size_t length() const { return m_pendingCount; }
@property bool empty() const { return length == 0; }
@ -43,11 +52,15 @@ final class ConsumableQueue(T)
while (new_capacity < min_capacity) new_capacity *= 2;
auto new_capacity_mask = new_capacity - 1;
auto new_storage = new Slot[new_capacity];
auto new_storage = mallocNT!Slot(new_capacity);
foreach (i; 0 .. m_consumedCount + m_pendingCount)
new_storage[(m_first + i) & new_capacity_mask] = m_storage[(m_first + i) & m_capacityMask];
() @trusted {
if (m_storage !is null)
freeNT(m_storage);
m_storage = new_storage;
} ();
m_capacityMask = new_capacity_mask;
}