From 8ba2208f5e6e6460c338c5b3168ee6be485b4e10 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 8 Dec 2023 12:23:54 -0800 Subject: Simplify handling the cluster sizes --- core/context.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/context.cpp b/core/context.cpp index 1640fbdc..1415857c 100644 --- a/core/context.cpp +++ b/core/context.cpp @@ -86,10 +86,11 @@ ContextBase::~ContextBase() void ContextBase::allocVoiceChanges() { + static constexpr size_t clustersize{std::tuple_size_v}; + VoiceChangeCluster clusterptr{std::make_unique()}; const auto cluster = al::span{*clusterptr}; - const size_t clustersize{cluster.size()}; for(size_t i{1};i < clustersize;++i) cluster[i-1].mNext.store(std::addressof(cluster[i]), std::memory_order_relaxed); cluster[clustersize-1].mNext.store(mVoiceChangeTail, std::memory_order_relaxed); @@ -100,12 +101,12 @@ void ContextBase::allocVoiceChanges() void ContextBase::allocVoiceProps() { - auto clusterptr = std::make_unique(); - const size_t clustersize{clusterptr->size()}; + static constexpr size_t clustersize{std::tuple_size_v}; TRACE("Increasing allocated voice properties to %zu\n", (mVoicePropClusters.size()+1) * clustersize); + auto clusterptr = std::make_unique(); auto cluster = al::span{*clusterptr}; for(size_t i{1};i < clustersize;++i) cluster[i-1].next.store(std::addressof(cluster[i]), std::memory_order_relaxed); @@ -120,6 +121,10 @@ void ContextBase::allocVoiceProps() void ContextBase::allocVoices(size_t addcount) { + static constexpr size_t clustersize{std::tuple_size_v}; + /* Convert element count to cluster count. */ + addcount = (addcount+(clustersize-1)) / clustersize; + if(!addcount) { if(!mVoiceClusters.empty()) @@ -127,20 +132,17 @@ void ContextBase::allocVoices(size_t addcount) ++addcount; } + if(addcount >= std::numeric_limits::max()/clustersize - mVoiceClusters.size()) + throw std::runtime_error{"Allocating too many voices"}; + const size_t totalcount{(mVoiceClusters.size()+addcount) * clustersize}; + TRACE("Increasing allocated voices to %zu\n", totalcount); + while(addcount) { - auto voicesptr = std::make_unique(); - const size_t clustersize{voicesptr->size()}; - if(1u >= std::numeric_limits::max()/clustersize - mVoiceClusters.size()) - throw std::runtime_error{"Allocating too many voices"}; - - mVoiceClusters.emplace_back(std::move(voicesptr)); - addcount -= std::min(addcount, clustersize);; + mVoiceClusters.emplace_back(std::make_unique()); + --addcount; } - const size_t totalcount{mVoiceClusters.size() * mVoiceClusters.front()->size()}; - TRACE("Increasing allocated voices to %zu\n", totalcount); - auto newarray = VoiceArray::Create(totalcount); auto voice_iter = newarray->begin(); for(VoiceCluster &cluster : mVoiceClusters) -- cgit v1.2.3