From 518f30e52307e12fe184c34f0da8f197b976ced5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 14 Jan 2023 13:23:17 +0000 Subject: Bring `PtrMap` inline with `StringMap` --- src/queue.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/queue.cpp') diff --git a/src/queue.cpp b/src/queue.cpp index 05076cfbd..2ad9cb29f 100644 --- a/src/queue.cpp +++ b/src/queue.cpp @@ -10,22 +10,19 @@ struct MPSCNode { // template struct MPSCQueue { - gbAllocator allocator; - MPSCNode sentinel; std::atomic *> head; std::atomic *> tail; std::atomic count; }; -template gb_internal void mpsc_init (MPSCQueue *q, gbAllocator const &allocator); +template gb_internal void mpsc_init (MPSCQueue *q); template gb_internal void mpsc_destroy(MPSCQueue *q); template gb_internal isize mpsc_enqueue(MPSCQueue *q, T const &value); template gb_internal bool mpsc_dequeue(MPSCQueue *q, T *value_); template gb_internal void mpsc_init(MPSCQueue *q, gbAllocator const &allocator) { - q->allocator = allocator; q->sentinel.next.store(nullptr, std::memory_order_relaxed); q->head.store(&q->sentinel, std::memory_order_relaxed); q->tail.store(&q->sentinel, std::memory_order_relaxed); @@ -39,7 +36,7 @@ gb_internal void mpsc_destroy(MPSCQueue *q) { template gb_internal MPSCNode *mpsc_alloc_node(MPSCQueue *q, T const &value) { - auto new_node = gb_alloc_item(q->allocator, MPSCNode); + auto new_node = gb_alloc_item(heap_allocator(), MPSCNode); new_node->value = value; return new_node; } @@ -95,7 +92,6 @@ struct MPMCQueue { T * nodes; MPMCQueueAtomicIdx *indices; - gbAllocator allocator; BlockingMutex mutex; MPMCQueueAtomicIdx count; i32 mask; // capacity-1, because capacity must be a power of 2 @@ -108,6 +104,9 @@ struct MPMCQueue { }; +gb_internal gbAllocator mpmc_allocator(void) { + return heap_allocator(); +} gb_internal void mpmc_internal_init_indices(MPMCQueueAtomicIdx *indices, i32 offset, i32 size) { GB_ASSERT(offset % 8 == 0); @@ -129,7 +128,7 @@ gb_internal void mpmc_internal_init_indices(MPMCQueueAtomicIdx *indices, i32 off template -gb_internal void mpmc_init(MPMCQueue *q, gbAllocator a, isize size_i) { +gb_internal void mpmc_init(MPMCQueue *q, isize size_i) { if (size_i < 8) { size_i = 8; } @@ -139,7 +138,7 @@ gb_internal void mpmc_init(MPMCQueue *q, gbAllocator a, isize size_i) { GB_ASSERT(gb_is_power_of_two(size)); q->mask = size-1; - q->allocator = a; + gbAllocator a = mpmc_allocator(); q->nodes = gb_alloc_array(a, T, size); q->indices = gb_alloc_array(a, MPMCQueueAtomicIdx, size); @@ -150,23 +149,25 @@ gb_internal void mpmc_init(MPMCQueue *q, gbAllocator a, isize size_i) { template gb_internal void mpmc_destroy(MPMCQueue *q) { - gb_free(q->allocator, q->nodes); - gb_free(q->allocator, q->indices); + gbAllocator a = mpmc_allocator(); + gb_free(a, q->nodes); + gb_free(a, q->indices); } template gb_internal bool mpmc_internal_grow(MPMCQueue *q) { + gbAllocator a = mpmc_allocator(); mutex_lock(&q->mutex); i32 old_size = q->mask+1; i32 new_size = old_size*2; - resize_array_raw(&q->nodes, q->allocator, old_size, new_size); + resize_array_raw(&q->nodes, a, old_size, new_size); if (q->nodes == nullptr) { GB_PANIC("Unable to resize enqueue: %td -> %td", old_size, new_size); mutex_unlock(&q->mutex); return false; } - resize_array_raw(&q->indices, q->allocator, old_size, new_size); + resize_array_raw(&q->indices, a, old_size, new_size); if (q->indices == nullptr) { GB_PANIC("Unable to resize enqueue: %td -> %td", old_size, new_size); mutex_unlock(&q->mutex); -- cgit v1.2.3