From 4a605640bcb85eb21228a818b31c4b4eda268b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 21 Oct 2018 20:15:37 +0200 Subject: [PATCH] Make ConsumableQueue nogc. --- source/eventcore/internal/consumablequeue.d | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/source/eventcore/internal/consumablequeue.d b/source/eventcore/internal/consumablequeue.d index 63aec28..6fd6a04 100644 --- a/source/eventcore/internal/consumablequeue.d +++ b/source/eventcore/internal/consumablequeue.d @@ -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]; - m_storage = new_storage; + () @trusted { + if (m_storage !is null) + freeNT(m_storage); + m_storage = new_storage; + } (); m_capacityMask = new_capacity_mask; }