From d50ca464cd5e4f07bc399fd244578b0b34d72aef Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 25 Sep 2019 03:01:58 -0700 Subject: Use a span for holding the source handles --- al/source.cpp | 90 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/al/source.cpp b/al/source.cpp index 3aaaaf90..a802cabc 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -2641,20 +2641,23 @@ START_API_FUNC if UNLIKELY(n <= 0) return; al::vector extra_sources; - std::array source_storage; - ALsource **srchandles{source_storage.data()}; - if UNLIKELY(static_cast(n) > source_storage.size()) + std::array source_storage; + al::span srchandles; + if LIKELY(static_cast(n) <= source_storage.size()) + srchandles = {source_storage.data(), static_cast(n)}; + else { extra_sources.resize(static_cast(n)); - srchandles = extra_sources.data(); + srchandles = {extra_sources.data(), extra_sources.size()}; } std::lock_guard _{context->mSourceLock}; - for(ALsizei i{0};i < n;i++) + for(auto &srchdl : srchandles) { - srchandles[i] = LookupSource(context.get(), sources[i]); - if(!srchandles[i]) - SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", sources[i]); + srchdl = LookupSource(context.get(), *sources); + if(!srchdl) + SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources); + ++sources; } ALCdevice *device{context->mDevice.get()}; @@ -2663,7 +2666,7 @@ START_API_FUNC if UNLIKELY(!device->Connected.load(std::memory_order_acquire)) { /* TODO: Send state change event? */ - std::for_each(srchandles, srchandles+n, + std::for_each(srchandles.begin(), srchandles.end(), [](ALsource *source) -> void { source->OffsetType = AL_NONE; @@ -2684,10 +2687,10 @@ START_API_FUNC }; auto free_voices = std::accumulate(context->mVoices.begin(), context->mVoices.end(), ALuint{0}, count_free_voices); - if UNLIKELY(static_cast(n) > free_voices) + if UNLIKELY(srchandles.size() > free_voices) { /* Increase the number of voices to handle the request. */ - const ALuint need_voices{static_cast(n) - free_voices}; + const size_t need_voices{srchandles.size() - free_voices}; context->mVoices.resize(context->mVoices.size() + need_voices); } @@ -2869,7 +2872,7 @@ START_API_FUNC SendStateChangeEvent(context.get(), source->id, AL_PLAYING); } }; - std::for_each(srchandles, srchandles+n, start_source); + std::for_each(srchandles.begin(), srchandles.end(), start_source); } END_API_FUNC @@ -2890,20 +2893,23 @@ START_API_FUNC if UNLIKELY(n <= 0) return; al::vector extra_sources; - std::array source_storage; - ALsource **srchandles{source_storage.data()}; - if UNLIKELY(static_cast(n) > source_storage.size()) + std::array source_storage; + al::span srchandles; + if LIKELY(static_cast(n) <= source_storage.size()) + srchandles = {source_storage.data(), static_cast(n)}; + else { extra_sources.resize(static_cast(n)); - srchandles = extra_sources.data(); + srchandles = {extra_sources.data(), extra_sources.size()}; } std::lock_guard _{context->mSourceLock}; - for(ALsizei i{0};i < n;i++) + for(auto &srchdl : srchandles) { - srchandles[i] = LookupSource(context.get(), sources[i]); - if(!srchandles[i]) - SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", sources[i]); + srchdl = LookupSource(context.get(), *sources); + if(!srchdl) + SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources); + ++sources; } ALCdevice *device{context->mDevice.get()}; @@ -2924,7 +2930,7 @@ START_API_FUNC SendStateChangeEvent(context.get(), source->id, AL_PAUSED); } }; - std::for_each(srchandles, srchandles+n, pause_source); + std::for_each(srchandles.begin(), srchandles.end(), pause_source); } END_API_FUNC @@ -2945,20 +2951,23 @@ START_API_FUNC if UNLIKELY(n <= 0) return; al::vector extra_sources; - std::array source_storage; - ALsource **srchandles{source_storage.data()}; - if UNLIKELY(static_cast(n) > source_storage.size()) + std::array source_storage; + al::span srchandles; + if LIKELY(static_cast(n) <= source_storage.size()) + srchandles = {source_storage.data(), static_cast(n)}; + else { extra_sources.resize(static_cast(n)); - srchandles = extra_sources.data(); + srchandles = {extra_sources.data(), extra_sources.size()}; } std::lock_guard _{context->mSourceLock}; - for(ALsizei i{0};i < n;i++) + for(auto &srchdl : srchandles) { - srchandles[i] = LookupSource(context.get(), sources[i]); - if(!srchandles[i]) - SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", sources[i]); + srchdl = LookupSource(context.get(), *sources); + if(!srchdl) + SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources); + ++sources; } ALCdevice *device{context->mDevice.get()}; @@ -2989,7 +2998,7 @@ START_API_FUNC source->OffsetType = AL_NONE; source->Offset = 0.0; }; - std::for_each(srchandles, srchandles+n, stop_source); + std::for_each(srchandles.begin(), srchandles.end(), stop_source); } END_API_FUNC @@ -3010,20 +3019,23 @@ START_API_FUNC if UNLIKELY(n <= 0) return; al::vector extra_sources; - std::array source_storage; - ALsource **srchandles{source_storage.data()}; - if UNLIKELY(static_cast(n) > source_storage.size()) + std::array source_storage; + al::span srchandles; + if LIKELY(static_cast(n) <= source_storage.size()) + srchandles = {source_storage.data(), static_cast(n)}; + else { extra_sources.resize(static_cast(n)); - srchandles = extra_sources.data(); + srchandles = {extra_sources.data(), extra_sources.size()}; } std::lock_guard _{context->mSourceLock}; - for(ALsizei i{0};i < n;i++) + for(auto &srchdl : srchandles) { - srchandles[i] = LookupSource(context.get(), sources[i]); - if(!srchandles[i]) - SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", sources[i]); + srchdl = LookupSource(context.get(), *sources); + if(!srchdl) + SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources); + ++sources; } ALCdevice *device{context->mDevice.get()}; @@ -3050,7 +3062,7 @@ START_API_FUNC source->OffsetType = AL_NONE; source->Offset = 0.0; }; - std::for_each(srchandles, srchandles+n, rewind_source); + std::for_each(srchandles.begin(), srchandles.end(), rewind_source); } END_API_FUNC -- cgit v1.2.3