From 859319fa7864ed6a81af1f12c68627ab58bb7a7d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 3 Dec 2023 14:31:24 -0800 Subject: Replace a global function with a member function --- al/source.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'al/source.cpp') diff --git a/al/source.cpp b/al/source.cpp index fe5bba40..73a357d4 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -202,7 +202,7 @@ int64_t GetSourceSampleOffset(ALsource *Source, ALCcontext *context, nanoseconds do { refcount = device->waitForMix(); - *clocktime = GetDeviceClockTime(device); + *clocktime = device->getClockTime(); voice = GetSourceVoice(Source, context); if(voice) { @@ -242,7 +242,7 @@ double GetSourceSecOffset(ALsource *Source, ALCcontext *context, nanoseconds *cl do { refcount = device->waitForMix(); - *clocktime = GetDeviceClockTime(device); + *clocktime = device->getClockTime(); voice = GetSourceVoice(Source, context); if(voice) { -- cgit v1.2.3 From b6a68e8d510610e181d638ff993e327059bd6018 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 3 Dec 2023 23:36:07 -0800 Subject: Remove some unnecessary atomic wrappers --- al/auxeffectslot.cpp | 7 ++++--- al/auxeffectslot.h | 2 +- al/buffer.cpp | 17 +++++++++-------- al/buffer.h | 2 +- al/source.cpp | 5 +++-- alc/alc.cpp | 3 ++- alc/backends/base.cpp | 2 +- alc/backends/pipewire.cpp | 2 +- common/atomic.h | 13 +++++-------- common/intrusive_ptr.h | 2 +- core/context.h | 2 +- core/device.h | 2 +- core/hrtf.cpp | 6 +++--- core/hrtf.h | 2 +- 14 files changed, 34 insertions(+), 33 deletions(-) (limited to 'al/source.cpp') diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index fb646389..31e9542b 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -370,7 +370,7 @@ FORCE_ALIGN void AL_APIENTRY alDeleteAuxiliaryEffectSlotsDirect(ALCcontext *cont context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", effectslots[0]); return; } - if(ReadRef(slot->ref) != 0) UNLIKELY + if(slot->ref.load(std::memory_order_relaxed) != 0) UNLIKELY { context->setError(AL_INVALID_OPERATION, "Deleting in-use effect slot %u", effectslots[0]); @@ -390,7 +390,7 @@ FORCE_ALIGN void AL_APIENTRY alDeleteAuxiliaryEffectSlotsDirect(ALCcontext *cont context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", effectslots[i]); return; } - if(ReadRef(slot->ref) != 0) UNLIKELY + if(slot->ref.load(std::memory_order_relaxed) != 0) UNLIKELY { context->setError(AL_INVALID_OPERATION, "Deleting in-use effect slot %u", effectslots[i]); @@ -1530,7 +1530,8 @@ void eax_delete_al_effect_slot(ALCcontext& context, ALeffectslot& effect_slot) std::lock_guard effect_slot_lock{context.mEffectSlotLock}; - if(ReadRef(effect_slot.ref) != 0) { + if(effect_slot.ref.load(std::memory_order_relaxed) != 0) + { ERR(EAX_PREFIX "Deleting in-use effect slot %u.\n", effect_slot.id); return; } diff --git a/al/auxeffectslot.h b/al/auxeffectslot.h index 1ad0ffc4..36216022 100644 --- a/al/auxeffectslot.h +++ b/al/auxeffectslot.h @@ -63,7 +63,7 @@ struct ALeffectslot { SlotState mState{SlotState::Initial}; - RefCount ref{0u}; + std::atomic ref{0u}; EffectSlot *mSlot{nullptr}; diff --git a/al/buffer.cpp b/al/buffer.cpp index 7d043036..ae41585f 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -286,7 +286,7 @@ void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, ALuint size, const FmtChannels DstChannels, const FmtType DstType, const std::byte *SrcData, ALbitfieldSOFT access) { - if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) UNLIKELY + if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u", ALBuf->id); @@ -393,7 +393,7 @@ void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, const FmtChannels DstChannels, const FmtType DstType, ALBUFFERCALLBACKTYPESOFT callback, void *userptr) { - if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) UNLIKELY + if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY return context->setError(AL_INVALID_OPERATION, "Modifying callback for in-use buffer %u", ALBuf->id); @@ -445,7 +445,7 @@ void PrepareCallback(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, void PrepareUserPtr(ALCcontext *context, ALbuffer *ALBuf, ALsizei freq, const FmtChannels DstChannels, const FmtType DstType, std::byte *sdata, const ALuint sdatalen) { - if(ReadRef(ALBuf->ref) != 0 || ALBuf->MappedAccess != 0) UNLIKELY + if(ALBuf->ref.load(std::memory_order_relaxed) != 0 || ALBuf->MappedAccess != 0) UNLIKELY return context->setError(AL_INVALID_OPERATION, "Modifying storage for in-use buffer %u", ALBuf->id); @@ -711,7 +711,7 @@ FORCE_ALIGN void AL_APIENTRY alDeleteBuffersDirect(ALCcontext *context, ALsizei context->setError(AL_INVALID_NAME, "Invalid buffer ID %u", bid); return false; } - if(ReadRef(ALBuf->ref) != 0) UNLIKELY + if(ALBuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY { context->setError(AL_INVALID_OPERATION, "Deleting in-use buffer %u", bid); return false; @@ -826,7 +826,8 @@ FORCE_ALIGN void* AL_APIENTRY alMapBufferDirectSOFT(ALCcontext *context, ALuint else { ALbitfieldSOFT unavailable = (albuf->Access^access) & access; - if(ReadRef(albuf->ref) != 0 && !(access&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY + if(albuf->ref.load(std::memory_order_relaxed) != 0 + && !(access&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY context->setError(AL_INVALID_OPERATION, "Mapping in-use buffer %u without persistent mapping", buffer); else if(albuf->MappedAccess != 0) UNLIKELY @@ -1042,7 +1043,7 @@ FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer, break; case AL_AMBISONIC_LAYOUT_SOFT: - if(ReadRef(albuf->ref) != 0) UNLIKELY + if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic layout", buffer); else if(const auto layout = AmbiLayoutFromEnum(value)) @@ -1052,7 +1053,7 @@ FORCE_ALIGN void AL_APIENTRY alBufferiDirect(ALCcontext *context, ALuint buffer, break; case AL_AMBISONIC_SCALING_SOFT: - if(ReadRef(albuf->ref) != 0) UNLIKELY + if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's ambisonic scaling", buffer); else if(const auto scaling = AmbiScalingFromEnum(value)) @@ -1116,7 +1117,7 @@ FORCE_ALIGN void AL_APIENTRY alBufferivDirect(ALCcontext *context, ALuint buffer else switch(param) { case AL_LOOP_POINTS_SOFT: - if(ReadRef(albuf->ref) != 0) UNLIKELY + if(albuf->ref.load(std::memory_order_relaxed) != 0) UNLIKELY context->setError(AL_INVALID_OPERATION, "Modifying in-use buffer %u's loop points", buffer); else if(values[0] < 0 || values[0] >= values[1] diff --git a/al/buffer.h b/al/buffer.h index f936cf98..cb864aff 100644 --- a/al/buffer.h +++ b/al/buffer.h @@ -43,7 +43,7 @@ struct ALbuffer : public BufferStorage { ALuint mLoopEnd{0u}; /* Number of times buffer was attached to a source (deletion can only occur when 0) */ - RefCount ref{0u}; + std::atomic ref{0u}; /* Self ID */ ALuint id{0}; diff --git a/al/source.cpp b/al/source.cpp index 73a357d4..cb24c09f 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -1604,10 +1604,11 @@ NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, con if(!buffer) UNLIKELY return Context->setError(AL_INVALID_VALUE, "Invalid buffer ID %s", std::to_string(values[0]).c_str()); - if(buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY + if(buffer->MappedAccess + && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) UNLIKELY return Context->setError(AL_INVALID_OPERATION, "Setting non-persistently mapped buffer %u", buffer->id); - if(buffer->mCallback && ReadRef(buffer->ref) != 0) UNLIKELY + if(buffer->mCallback && buffer->ref.load(std::memory_order_relaxed) != 0) UNLIKELY return Context->setError(AL_INVALID_OPERATION, "Setting already-set callback buffer %u", buffer->id); diff --git a/alc/alc.cpp b/alc/alc.cpp index ab4cc7ba..d974777b 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -2514,7 +2514,8 @@ ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, refcount = dev->waitForMix(); basecount = dev->mClockBase.load(std::memory_order_relaxed); samplecount = dev->mSamplesDone.load(std::memory_order_relaxed); - } while(refcount != ReadRef(dev->MixCount)); + std::atomic_thread_fence(std::memory_order_acquire); + } while(refcount != dev->MixCount.load(std::memory_order_relaxed)); basecount += nanoseconds{seconds{samplecount}} / dev->Frequency; *values = basecount.count(); } diff --git a/alc/backends/base.cpp b/alc/backends/base.cpp index 675d8043..b2b567a6 100644 --- a/alc/backends/base.cpp +++ b/alc/backends/base.cpp @@ -52,7 +52,7 @@ ClockLatency BackendBase::getClockLatency() refcount = mDevice->waitForMix(); ret.ClockTime = mDevice->getClockTime(); std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != ReadRef(mDevice->MixCount)); + } while(refcount != mDevice->MixCount.load(std::memory_order_relaxed)); /* NOTE: The device will generally have about all but one periods filled at * any given time during playback. Without a more accurate measurement from diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index e2f32758..754feb6c 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -1827,7 +1827,7 @@ ClockLatency PipeWirePlayback::getClockLatency() mixtime = mDevice->getClockTime(); clock_gettime(CLOCK_MONOTONIC, &tspec); std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != ReadRef(mDevice->MixCount)); + } while(refcount != mDevice->MixCount.load(std::memory_order_relaxed)); /* Convert the monotonic clock, stream ticks, and stream delay to * nanoseconds. diff --git a/common/atomic.h b/common/atomic.h index 5e9b04c6..a579dcab 100644 --- a/common/atomic.h +++ b/common/atomic.h @@ -4,15 +4,12 @@ #include -using RefCount = std::atomic; - -inline void InitRef(RefCount &ref, unsigned int value) -{ ref.store(value, std::memory_order_relaxed); } -inline unsigned int ReadRef(RefCount &ref) -{ return ref.load(std::memory_order_acquire); } -inline unsigned int IncrementRef(RefCount &ref) +template +auto IncrementRef(std::atomic &ref) noexcept { return ref.fetch_add(1u, std::memory_order_acq_rel)+1u; } -inline unsigned int DecrementRef(RefCount &ref) + +template +auto DecrementRef(std::atomic &ref) noexcept { return ref.fetch_sub(1u, std::memory_order_acq_rel)-1u; } diff --git a/common/intrusive_ptr.h b/common/intrusive_ptr.h index 27075347..714a5617 100644 --- a/common/intrusive_ptr.h +++ b/common/intrusive_ptr.h @@ -11,7 +11,7 @@ namespace al { template class intrusive_ref { - RefCount mRef{1u}; + std::atomic mRef{1u}; public: unsigned int add_ref() noexcept { return IncrementRef(mRef); } diff --git a/core/context.h b/core/context.h index ccb7dd3b..6f65663f 100644 --- a/core/context.h +++ b/core/context.h @@ -85,7 +85,7 @@ struct ContextBase { /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit * indicates if updates are currently happening). */ - RefCount mUpdateCount{0u}; + std::atomic mUpdateCount{0u}; std::atomic mHoldUpdates{false}; std::atomic mStopVoicesOnDisconnect{true}; diff --git a/core/device.h b/core/device.h index 842f1d82..1f3c5105 100644 --- a/core/device.h +++ b/core/device.h @@ -284,7 +284,7 @@ struct DeviceBase { * the end, so the bottom bit indicates if the device is currently mixing * and the upper bits indicates how many mixes have been done. */ - RefCount MixCount{0u}; + std::atomic MixCount{0u}; // Contexts created on this device std::atomic*> mContexts{nullptr}; diff --git a/core/hrtf.cpp b/core/hrtf.cpp index 9a13a004..1b7da3f9 100644 --- a/core/hrtf.cpp +++ b/core/hrtf.cpp @@ -392,7 +392,7 @@ std::unique_ptr CreateHrtfStore(uint rate, uint8_t irSize, if(void *ptr{al_calloc(16, total)}) { Hrtf.reset(al::construct_at(static_cast(ptr))); - InitRef(Hrtf->mRef, 1u); + Hrtf->mRef.store(1u, std::memory_order_relaxed); Hrtf->mSampleRate = rate & 0xff'ff'ff; Hrtf->mIrSize = irSize; @@ -1459,9 +1459,9 @@ void HrtfStore::dec_ref() auto remove_unused = [](LoadedHrtf &hrtf) -> bool { HrtfStore *entry{hrtf.mEntry.get()}; - if(entry && ReadRef(entry->mRef) == 0) + if(entry && entry->mRef.load() == 0) { - TRACE("Unloading unused HRTF %s\n", hrtf.mFilename.data()); + TRACE("Unloading unused HRTF %s\n", hrtf.mFilename.c_str()); hrtf.mEntry = nullptr; return true; } diff --git a/core/hrtf.h b/core/hrtf.h index 5e6e09a8..50c4f450 100644 --- a/core/hrtf.h +++ b/core/hrtf.h @@ -18,7 +18,7 @@ struct HrtfStore { - RefCount mRef; + std::atomic mRef; uint mSampleRate : 24; uint mIrSize : 8; -- cgit v1.2.3 From e123e7bbda4330559ef03a5362bc93064eb87e4e Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 4 Dec 2023 01:18:49 -0800 Subject: Use RAII to handle writing under the mixer seqlock --- al/source.cpp | 6 +++--- alc/alc.cpp | 8 ++------ alc/alu.cpp | 28 +++++++++------------------- alc/backends/base.cpp | 2 +- alc/backends/pipewire.cpp | 2 +- core/device.h | 28 ++++++++++++++++++++++++++-- 6 files changed, 42 insertions(+), 32 deletions(-) (limited to 'al/source.cpp') diff --git a/al/source.cpp b/al/source.cpp index cb24c09f..c9ec8f21 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -212,7 +212,7 @@ int64_t GetSourceSampleOffset(ALsource *Source, ALCcontext *context, nanoseconds readPos += voice->mPositionFrac.load(std::memory_order_relaxed); } std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != device->MixCount.load(std::memory_order_relaxed)); + } while(refcount != device->mMixCount.load(std::memory_order_relaxed)); if(!voice) return 0; @@ -252,7 +252,7 @@ double GetSourceSecOffset(ALsource *Source, ALCcontext *context, nanoseconds *cl readPos += voice->mPositionFrac.load(std::memory_order_relaxed); } std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != device->MixCount.load(std::memory_order_relaxed)); + } while(refcount != device->mMixCount.load(std::memory_order_relaxed)); if(!voice) return 0.0f; @@ -302,7 +302,7 @@ NOINLINE T GetSourceOffset(ALsource *Source, ALenum name, ALCcontext *context) readPosFrac = voice->mPositionFrac.load(std::memory_order_relaxed); } std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != device->MixCount.load(std::memory_order_relaxed)); + } while(refcount != device->mMixCount.load(std::memory_order_relaxed)); if(!voice) return T{0}; diff --git a/alc/alc.cpp b/alc/alc.cpp index d974777b..9a919032 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -976,9 +976,7 @@ std::unique_ptr CreateDeviceLimiter(const ALCdevice *device, const f */ inline void UpdateClockBase(ALCdevice *device) { - const auto mixCount = device->MixCount.load(std::memory_order_relaxed); - device->MixCount.store(mixCount+1, std::memory_order_relaxed); - std::atomic_thread_fence(std::memory_order_release); + const auto mixLock = device->getWriteMixLock(); auto samplesDone = device->mSamplesDone.load(std::memory_order_relaxed); auto clockBase = device->mClockBase.load(std::memory_order_relaxed); @@ -986,8 +984,6 @@ inline void UpdateClockBase(ALCdevice *device) clockBase += nanoseconds{seconds{samplesDone}} / device->Frequency; device->mClockBase.store(clockBase, std::memory_order_relaxed); device->mSamplesDone.store(0, std::memory_order_relaxed); - - device->MixCount.store(mixCount+2, std::memory_order_release); } /** @@ -2515,7 +2511,7 @@ ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, basecount = dev->mClockBase.load(std::memory_order_relaxed); samplecount = dev->mSamplesDone.load(std::memory_order_relaxed); std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != dev->MixCount.load(std::memory_order_relaxed)); + } while(refcount != dev->mMixCount.load(std::memory_order_relaxed)); basecount += nanoseconds{seconds{samplecount}} / dev->Frequency; *values = basecount.count(); } diff --git a/alc/alu.cpp b/alc/alu.cpp index 23518fa9..2bc648bf 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -2135,19 +2135,16 @@ uint DeviceBase::renderSamples(const uint numSamples) for(FloatBufferLine &buffer : MixBuffer) buffer.fill(0.0f); - /* Increment the mix count at the start (lsb should now be 1). */ - const auto mixCount = MixCount.load(std::memory_order_relaxed); - MixCount.store(mixCount+1, std::memory_order_relaxed); - std::atomic_thread_fence(std::memory_order_release); + { + const auto mixLock = getWriteMixLock(); - /* Process and mix each context's sources and effects. */ - ProcessContexts(this, samplesToDo); + /* Process and mix each context's sources and effects. */ + ProcessContexts(this, samplesToDo); - /* Increment the clock time. Every second's worth of samples is converted - * and added to clock base so that large sample counts don't overflow - * during conversion. This also guarantees a stable conversion. - */ - { + /* Every second's worth of samples is converted and added to clock base + * so that large sample counts don't overflow during conversion. This + * also guarantees a stable conversion. + */ auto samplesDone = mSamplesDone.load(std::memory_order_relaxed) + samplesToDo; auto clockBase = mClockBase.load(std::memory_order_relaxed) + std::chrono::seconds{samplesDone/Frequency}; @@ -2155,9 +2152,6 @@ uint DeviceBase::renderSamples(const uint numSamples) mClockBase.store(clockBase, std::memory_order_relaxed); } - /* Increment the mix count at the end (lsb should now be 0). */ - MixCount.store(mixCount+2, std::memory_order_release); - /* Apply any needed post-process for finalizing the Dry mix to the RealOut * (Ambisonic decode, UHJ encode, etc). */ @@ -2232,9 +2226,7 @@ void DeviceBase::renderSamples(void *outBuffer, const uint numSamples, const siz void DeviceBase::handleDisconnect(const char *msg, ...) { - const auto mixCount = MixCount.load(std::memory_order_relaxed); - MixCount.store(mixCount+1, std::memory_order_relaxed); - std::atomic_thread_fence(std::memory_order_release); + const auto mixLock = getWriteMixLock(); if(Connected.exchange(false, std::memory_order_acq_rel)) { @@ -2277,6 +2269,4 @@ void DeviceBase::handleDisconnect(const char *msg, ...) std::for_each(voicelist.begin(), voicelist.end(), stop_voice); } } - - MixCount.store(mixCount+2, std::memory_order_release); } diff --git a/alc/backends/base.cpp b/alc/backends/base.cpp index b2b567a6..1677ae19 100644 --- a/alc/backends/base.cpp +++ b/alc/backends/base.cpp @@ -52,7 +52,7 @@ ClockLatency BackendBase::getClockLatency() refcount = mDevice->waitForMix(); ret.ClockTime = mDevice->getClockTime(); std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != mDevice->MixCount.load(std::memory_order_relaxed)); + } while(refcount != mDevice->mMixCount.load(std::memory_order_relaxed)); /* NOTE: The device will generally have about all but one periods filled at * any given time during playback. Without a more accurate measurement from diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index 754feb6c..adf9d62a 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -1827,7 +1827,7 @@ ClockLatency PipeWirePlayback::getClockLatency() mixtime = mDevice->getClockTime(); clock_gettime(CLOCK_MONOTONIC, &tspec); std::atomic_thread_fence(std::memory_order_acquire); - } while(refcount != mDevice->MixCount.load(std::memory_order_relaxed)); + } while(refcount != mDevice->mMixCount.load(std::memory_order_relaxed)); /* Convert the monotonic clock, stream ticks, and stream delay to * nanoseconds. diff --git a/core/device.h b/core/device.h index 1f3c5105..1ac01ba6 100644 --- a/core/device.h +++ b/core/device.h @@ -284,7 +284,7 @@ struct DeviceBase { * the end, so the bottom bit indicates if the device is currently mixing * and the upper bits indicates how many mixes have been done. */ - std::atomic MixCount{0u}; + std::atomic mMixCount{0u}; // Contexts created on this device std::atomic*> mContexts{nullptr}; @@ -299,10 +299,34 @@ struct DeviceBase { uint channelsFromFmt() const noexcept { return ChannelsFromDevFmt(FmtChans, mAmbiOrder); } uint frameSizeFromFmt() const noexcept { return bytesFromFmt() * channelsFromFmt(); } + struct MixLock { + std::atomic &mCount; + const uint mLastVal; + + MixLock(std::atomic &count, const uint last_val) noexcept + : mCount{count}, mLastVal{last_val} + { } + /* Increment the mix count when the lock goes out of scope to "release" + * it (lsb should be 0). + */ + ~MixLock() { mCount.store(mLastVal+2, std::memory_order_release); } + }; + auto getWriteMixLock() noexcept + { + /* Increment the mix count at the start of mixing and writing clock + * info (lsb should be 1). + */ + const auto mixCount = mMixCount.load(std::memory_order_relaxed); + mMixCount.store(mixCount+1, std::memory_order_relaxed); + std::atomic_thread_fence(std::memory_order_release); + return MixLock{mMixCount, mixCount}; + } + + /** Waits for the mixer to not be mixing or updating the clock. */ uint waitForMix() const noexcept { uint refcount; - while((refcount=MixCount.load(std::memory_order_acquire))&1) { + while((refcount=mMixCount.load(std::memory_order_acquire))&1) { } return refcount; } -- cgit v1.2.3 From 8f661a2f59e63cbed540b512dc564a3aca7c4211 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 8 Dec 2023 04:33:32 -0800 Subject: Fix some clang-tidy warnings --- al/auxeffectslot.h | 16 +++--- al/eax/api.h | 10 ++-- al/eax/call.h | 42 +++++++-------- al/eax/fx_slots.h | 9 ++-- al/effect.cpp | 14 +++-- al/effect.h | 10 ++-- al/effects/effects.h | 2 +- al/event.cpp | 2 +- al/source.cpp | 31 ++++++----- al/source.h | 8 +-- alc/alc.cpp | 10 ++-- alc/alu.cpp | 4 +- alc/backends/alsa.cpp | 2 +- alc/backends/coreaudio.cpp | 4 +- alc/backends/dsound.cpp | 6 +-- alc/backends/oboe.cpp | 4 +- alc/backends/oss.cpp | 2 +- alc/backends/pipewire.cpp | 5 +- alc/backends/portaudio.cpp | 4 +- alc/backends/sndio.cpp | 2 +- alc/backends/wasapi.cpp | 2 +- alc/backends/winmm.cpp | 5 +- alc/context.h | 2 +- alc/effects/base.h | 42 ++++++++------- alc/effects/reverb.cpp | 5 -- common/albit.h | 7 +-- common/almalloc.h | 56 ++++++++++---------- common/alnumbers.h | 8 +-- common/alnumeric.h | 2 +- common/alspan.h | 102 ++++++++++++++++++------------------- common/intrusive_ptr.h | 6 +-- common/pffft.cpp | 30 +++++------ common/ringbuffer.h | 25 ++++----- common/vecmat.h | 8 +-- core/async_event.h | 3 +- core/effects/base.h | 4 +- core/except.h | 6 +-- examples/alffplay.cpp | 61 ++++++++++------------ utils/alsoft-config/mainwindow.cpp | 4 +- 39 files changed, 281 insertions(+), 284 deletions(-) (limited to 'al/source.cpp') diff --git a/al/auxeffectslot.h b/al/auxeffectslot.h index 36216022..bfd4038e 100644 --- a/al/auxeffectslot.h +++ b/al/auxeffectslot.h @@ -89,12 +89,12 @@ struct ALeffectslot { public: void eax_initialize(ALCcontext& al_context, EaxFxSlotIndexValue index); - EaxFxSlotIndexValue eax_get_index() const noexcept { return eax_fx_slot_index_; } - const EAX50FXSLOTPROPERTIES& eax_get_eax_fx_slot() const noexcept + [[nodiscard]] auto eax_get_index() const noexcept -> EaxFxSlotIndexValue { return eax_fx_slot_index_; } + [[nodiscard]] auto eax_get_eax_fx_slot() const noexcept -> const EAX50FXSLOTPROPERTIES& { return eax_; } // Returns `true` if all sources should be updated, or `false` otherwise. - bool eax_dispatch(const EaxCall& call) + [[nodiscard]] auto eax_dispatch(const EaxCall& call) -> bool { return call.is_get() ? eax_get(call) : eax_set(call); } void eax_commit(); @@ -282,14 +282,14 @@ private: dst = src; } - constexpr bool eax4_fx_slot_is_legacy() const noexcept + [[nodiscard]] constexpr auto eax4_fx_slot_is_legacy() const noexcept -> bool { return eax_fx_slot_index_ < 2; } void eax4_fx_slot_ensure_unlocked() const; - static ALenum eax_get_efx_effect_type(const GUID& guid); - const GUID& eax_get_eax_default_effect_guid() const noexcept; - long eax_get_eax_default_lock() const noexcept; + [[nodiscard]] static auto eax_get_efx_effect_type(const GUID& guid) -> ALenum; + [[nodiscard]] auto eax_get_eax_default_effect_guid() const noexcept -> const GUID&; + [[nodiscard]] auto eax_get_eax_default_lock() const noexcept -> long; void eax4_fx_slot_set_defaults(Eax4Props& props) noexcept; void eax5_fx_slot_set_defaults(Eax5Props& props) noexcept; @@ -312,7 +312,7 @@ private: void eax4_fx_slot_set_all(const EaxCall& call); void eax5_fx_slot_set_all(const EaxCall& call); - bool eax_fx_slot_should_update_sources() const noexcept; + [[nodiscard]] auto eax_fx_slot_should_update_sources() const noexcept -> bool; // Returns `true` if all sources should be updated, or `false` otherwise. bool eax4_fx_slot_set(const EaxCall& call); diff --git a/al/eax/api.h b/al/eax/api.h index 18d93ef8..038fdf75 100644 --- a/al/eax/api.h +++ b/al/eax/api.h @@ -22,12 +22,12 @@ #ifndef _WIN32 -typedef struct _GUID { +using GUID = struct _GUID { std::uint32_t Data1; std::uint16_t Data2; std::uint16_t Data3; - std::uint8_t Data4[8]; -} GUID; + std::array Data4; +}; inline bool operator==(const GUID& lhs, const GUID& rhs) noexcept { return std::memcmp(&lhs, &rhs, sizeof(GUID)) == 0; } @@ -654,11 +654,11 @@ struct EAXSPEAKERLEVELPROPERTIES { }; // EAXSPEAKERLEVELPROPERTIES struct EAX40ACTIVEFXSLOTS { - GUID guidActiveFXSlots[EAX40_MAX_ACTIVE_FXSLOTS]; + std::array guidActiveFXSlots; }; // EAX40ACTIVEFXSLOTS struct EAX50ACTIVEFXSLOTS { - GUID guidActiveFXSlots[EAX50_MAX_ACTIVE_FXSLOTS]; + std::array guidActiveFXSlots; }; // EAX50ACTIVEFXSLOTS // Use this structure for EAXSOURCE_OBSTRUCTIONPARAMETERS property. diff --git a/al/eax/call.h b/al/eax/call.h index 45ff328c..04e94f3e 100644 --- a/al/eax/call.h +++ b/al/eax/call.h @@ -31,16 +31,16 @@ public: ALvoid* property_buffer, ALuint property_size); - bool is_get() const noexcept { return mCallType == EaxCallType::get; } - bool is_deferred() const noexcept { return mIsDeferred; } - int get_version() const noexcept { return mVersion; } - EaxCallPropertySetId get_property_set_id() const noexcept { return mPropertySetId; } - ALuint get_property_id() const noexcept { return mPropertyId; } - ALuint get_property_al_name() const noexcept { return mPropertySourceId; } - EaxFxSlotIndex get_fx_slot_index() const noexcept { return mFxSlotIndex; } + [[nodiscard]] auto is_get() const noexcept -> bool { return mCallType == EaxCallType::get; } + [[nodiscard]] auto is_deferred() const noexcept -> bool { return mIsDeferred; } + [[nodiscard]] auto get_version() const noexcept -> int { return mVersion; } + [[nodiscard]] auto get_property_set_id() const noexcept -> EaxCallPropertySetId { return mPropertySetId; } + [[nodiscard]] auto get_property_id() const noexcept -> ALuint { return mPropertyId; } + [[nodiscard]] auto get_property_al_name() const noexcept -> ALuint { return mPropertySourceId; } + [[nodiscard]] auto get_fx_slot_index() const noexcept -> EaxFxSlotIndex { return mFxSlotIndex; } template - TValue& get_value() const + [[nodiscard]] auto get_value() const -> TValue& { if(mPropertyBufferSize < sizeof(TValue)) fail_too_small(); @@ -49,7 +49,7 @@ public: } template - al::span get_values(size_t max_count) const + [[nodiscard]] auto get_values(size_t max_count) const -> al::span { if(max_count == 0 || mPropertyBufferSize < sizeof(TValue)) fail_too_small(); @@ -59,28 +59,28 @@ public: } template - al::span get_values() const + [[nodiscard]] auto get_values() const -> al::span { return get_values(~0_uz); } template - void set_value(const TValue& value) const + auto set_value(const TValue& value) const -> void { get_value() = value; } private: - const EaxCallType mCallType; - int mVersion; - EaxFxSlotIndex mFxSlotIndex; - EaxCallPropertySetId mPropertySetId; - bool mIsDeferred; - - const ALuint mPropertyId; - const ALuint mPropertySourceId; - ALvoid*const mPropertyBuffer; - const ALuint mPropertyBufferSize; + const EaxCallType mCallType{}; + int mVersion{}; + EaxFxSlotIndex mFxSlotIndex{}; + EaxCallPropertySetId mPropertySetId{}; + bool mIsDeferred{}; + + const ALuint mPropertyId{}; + const ALuint mPropertySourceId{}; + ALvoid*const mPropertyBuffer{}; + const ALuint mPropertyBufferSize{}; [[noreturn]] static void fail(const char* message); [[noreturn]] static void fail_too_small(); diff --git a/al/eax/fx_slots.h b/al/eax/fx_slots.h index 18b2d3ad..b7ed1031 100644 --- a/al/eax/fx_slots.h +++ b/al/eax/fx_slots.h @@ -25,11 +25,9 @@ public: } - const ALeffectslot& get( - EaxFxSlotIndex index) const; + [[nodiscard]] auto get(EaxFxSlotIndex index) const -> const ALeffectslot&; - ALeffectslot& get( - EaxFxSlotIndex index); + [[nodiscard]] auto get(EaxFxSlotIndex index) -> ALeffectslot&; private: using Items = std::array; @@ -39,8 +37,7 @@ private: [[noreturn]] - static void fail( - const char* message); + static void fail(const char* message); void initialize_fx_slots(ALCcontext& al_context); }; // EaxFxSlots diff --git a/al/effect.cpp b/al/effect.cpp index 3e48e91b..e99226c8 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -58,7 +58,7 @@ #include "eax/exception.h" #endif // ALSOFT_EAX -const EffectList gEffectList[16]{ +const std::array gEffectList{{ { "eaxreverb", EAXREVERB_EFFECT, AL_EFFECT_EAXREVERB }, { "reverb", REVERB_EFFECT, AL_EFFECT_REVERB }, { "autowah", AUTOWAH_EFFECT, AL_EFFECT_AUTOWAH }, @@ -75,9 +75,7 @@ const EffectList gEffectList[16]{ { "dedicated", DEDICATED_EFFECT, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT }, { "dedicated", DEDICATED_EFFECT, AL_EFFECT_DEDICATED_DIALOGUE }, { "convolution", CONVOLUTION_EFFECT, AL_EFFECT_CONVOLUTION_SOFT }, -}; - -bool DisabledEffects[MAX_EFFECTS]; +}}; effect_exception::effect_exception(ALenum code, const char *msg, ...) : mErrorCode{code} @@ -328,7 +326,7 @@ FORCE_ALIGN void AL_APIENTRY alEffectiDirect(ALCcontext *context, ALuint effect, { for(const EffectList &effectitem : gEffectList) { - if(value == effectitem.val && !DisabledEffects[effectitem.type]) + if(value == effectitem.val && !DisabledEffects.test(effectitem.type)) { isOk = true; break; @@ -687,9 +685,9 @@ void LoadReverbPreset(const char *name, ALeffect *effect) return; } - if(!DisabledEffects[EAXREVERB_EFFECT]) + if(!DisabledEffects.test(EAXREVERB_EFFECT)) InitEffectParams(effect, AL_EFFECT_EAXREVERB); - else if(!DisabledEffects[REVERB_EFFECT]) + else if(!DisabledEffects.test(REVERB_EFFECT)) InitEffectParams(effect, AL_EFFECT_REVERB); else InitEffectParams(effect, AL_EFFECT_NULL); @@ -742,7 +740,7 @@ bool IsValidEffectType(ALenum type) noexcept for(const auto &effect_item : gEffectList) { - if(type == effect_item.val && !DisabledEffects[effect_item.type]) + if(type == effect_item.val && !DisabledEffects.test(effect_item.type)) return true; } return false; diff --git a/al/effect.h b/al/effect.h index 27e9dd72..7c5c40dc 100644 --- a/al/effect.h +++ b/al/effect.h @@ -1,6 +1,8 @@ #ifndef AL_EFFECT_H #define AL_EFFECT_H +#include +#include #include #include "AL/al.h" @@ -29,16 +31,14 @@ enum { MAX_EFFECTS }; -extern bool DisabledEffects[MAX_EFFECTS]; - -extern float ReverbBoost; +inline std::bitset DisabledEffects; struct EffectList { const char name[16]; - int type; + ALuint type; ALenum val; }; -extern const EffectList gEffectList[16]; +extern const std::array gEffectList; struct ALeffect { diff --git a/al/effects/effects.h b/al/effects/effects.h index f30f256a..66fc8c44 100644 --- a/al/effects/effects.h +++ b/al/effects/effects.h @@ -24,7 +24,7 @@ public: effect_exception(ALenum code, const char *msg, ...); ~effect_exception() override; - ALenum errorCode() const noexcept { return mErrorCode; } + [[nodiscard]] auto errorCode() const noexcept -> ALenum { return mErrorCode; } }; diff --git a/al/event.cpp b/al/event.cpp index 8b76ceff..95b07dc2 100644 --- a/al/event.cpp +++ b/al/event.cpp @@ -118,7 +118,7 @@ int EventThread(ALCcontext *context) }; auto proc_disconnect = [context,enabledevts](AsyncDisconnectEvent &evt) { - const std::string_view message{evt.msg}; + const std::string_view message{evt.msg.data()}; context->debugMessage(DebugSource::System, DebugType::Error, 0, DebugSeverity::High, message); diff --git a/al/source.cpp b/al/source.cpp index c9ec8f21..9c449434 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -3917,27 +3917,30 @@ void ALsource::eax4_translate(const Eax4Props& src, Eax5Props& dst) noexcept // Active FX slots. // - for (auto i = 0; i < EAX50_MAX_ACTIVE_FXSLOTS; ++i) { + for(size_t i{0};i < EAX50_MAX_ACTIVE_FXSLOTS;++i) + { auto& dst_id = dst.active_fx_slots.guidActiveFXSlots[i]; - if (i < EAX40_MAX_ACTIVE_FXSLOTS) { + if(i < EAX40_MAX_ACTIVE_FXSLOTS) + { const auto& src_id = src.active_fx_slots.guidActiveFXSlots[i]; - if (src_id == EAX_NULL_GUID) + if(src_id == EAX_NULL_GUID) dst_id = EAX_NULL_GUID; - else if (src_id == EAX_PrimaryFXSlotID) + else if(src_id == EAX_PrimaryFXSlotID) dst_id = EAX_PrimaryFXSlotID; - else if (src_id == EAXPROPERTYID_EAX40_FXSlot0) + else if(src_id == EAXPROPERTYID_EAX40_FXSlot0) dst_id = EAXPROPERTYID_EAX50_FXSlot0; - else if (src_id == EAXPROPERTYID_EAX40_FXSlot1) + else if(src_id == EAXPROPERTYID_EAX40_FXSlot1) dst_id = EAXPROPERTYID_EAX50_FXSlot1; - else if (src_id == EAXPROPERTYID_EAX40_FXSlot2) + else if(src_id == EAXPROPERTYID_EAX40_FXSlot2) dst_id = EAXPROPERTYID_EAX50_FXSlot2; - else if (src_id == EAXPROPERTYID_EAX40_FXSlot3) + else if(src_id == EAXPROPERTYID_EAX40_FXSlot3) dst_id = EAXPROPERTYID_EAX50_FXSlot3; else assert(false && "Unknown active FX slot ID."); - } else + } + else dst_id = EAX_NULL_GUID; } @@ -4359,7 +4362,7 @@ void ALsource::eax4_set(const EaxCall& call, Eax4Props& props) break; case EAXSOURCE_ACTIVEFXSLOTID: - eax4_defer_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots); + eax4_defer_active_fx_slot_id(call, al::span{props.active_fx_slots.guidActiveFXSlots}); break; default: @@ -4440,7 +4443,7 @@ void ALsource::eax5_set(const EaxCall& call, Eax5Props& props) break; case EAXSOURCE_ACTIVEFXSLOTID: - eax5_defer_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots); + eax5_defer_active_fx_slot_id(call, al::span{props.active_fx_slots.guidActiveFXSlots}); break; case EAXSOURCE_MACROFXFACTOR: @@ -4730,7 +4733,8 @@ void ALsource::eax4_get(const EaxCall& call, const Eax4Props& props) break; case EAXSOURCE_ACTIVEFXSLOTID: - eax_get_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots, EAX40_MAX_ACTIVE_FXSLOTS); + eax_get_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots.data(), + EAX40_MAX_ACTIVE_FXSLOTS); break; default: @@ -4802,7 +4806,8 @@ void ALsource::eax5_get(const EaxCall& call, const Eax5Props& props) break; case EAXSOURCE_ACTIVEFXSLOTID: - eax_get_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots, EAX50_MAX_ACTIVE_FXSLOTS); + eax_get_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots.data(), + EAX50_MAX_ACTIVE_FXSLOTS); break; case EAXSOURCE_MACROFXFACTOR: diff --git a/al/source.h b/al/source.h index c7694f83..26d425ef 100644 --- a/al/source.h +++ b/al/source.h @@ -978,21 +978,21 @@ private: } template - void eax_defer_active_fx_slot_id(const EaxCall& call, GUID (&dst_ids)[TIdCount]) + void eax_defer_active_fx_slot_id(const EaxCall& call, const al::span dst_ids) { const auto src_ids = call.get_values(TIdCount); std::for_each(src_ids.cbegin(), src_ids.cend(), TValidator{}); - std::uninitialized_copy(src_ids.cbegin(), src_ids.cend(), dst_ids); + std::uninitialized_copy(src_ids.cbegin(), src_ids.cend(), dst_ids.begin()); } template - void eax4_defer_active_fx_slot_id(const EaxCall& call, GUID (&dst_ids)[TIdCount]) + void eax4_defer_active_fx_slot_id(const EaxCall& call, const al::span dst_ids) { eax_defer_active_fx_slot_id(call, dst_ids); } template - void eax5_defer_active_fx_slot_id(const EaxCall& call, GUID (&dst_ids)[TIdCount]) + void eax5_defer_active_fx_slot_id(const EaxCall& call, const al::span dst_ids) { eax_defer_active_fx_slot_id(call, dst_ids); } diff --git a/alc/alc.cpp b/alc/alc.cpp index 9a919032..1ceae5ee 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -660,7 +660,7 @@ void alc_initconfig(void) { if(len == strlen(effectitem.name) && strncmp(effectitem.name, str, len) == 0) - DisabledEffects[effectitem.type] = true; + DisabledEffects.set(effectitem.type); } } while(next++); } @@ -683,15 +683,15 @@ void alc_initconfig(void) else eax_g_is_enabled = true; - if((DisabledEffects[EAXREVERB_EFFECT] || DisabledEffects[CHORUS_EFFECT]) + if((DisabledEffects.test(EAXREVERB_EFFECT) || DisabledEffects.test(CHORUS_EFFECT)) && eax_g_is_enabled) { eax_g_is_enabled = false; TRACE("EAX disabled because %s disabled.\n", - (DisabledEffects[EAXREVERB_EFFECT] && DisabledEffects[CHORUS_EFFECT]) + (DisabledEffects.test(EAXREVERB_EFFECT) && DisabledEffects.test(CHORUS_EFFECT)) ? "EAXReverb and Chorus are" : - DisabledEffects[EAXREVERB_EFFECT] ? "EAXReverb is" : - DisabledEffects[CHORUS_EFFECT] ? "Chorus is" : ""); + DisabledEffects.test(EAXREVERB_EFFECT) ? "EAXReverb is" : + DisabledEffects.test(CHORUS_EFFECT) ? "Chorus is" : ""); } } #endif // ALSOFT_EAX diff --git a/alc/alu.cpp b/alc/alu.cpp index 7b3cd957..fe47f9be 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -2257,10 +2257,10 @@ void DeviceBase::handleDisconnect(const char *msg, ...) va_list args; va_start(args, msg); - int msglen{vsnprintf(disconnect.msg, sizeof(disconnect.msg), msg, args)}; + int msglen{vsnprintf(disconnect.msg.data(), disconnect.msg.size(), msg, args)}; va_end(args); - if(msglen < 0 || static_cast(msglen) >= sizeof(disconnect.msg)) + if(msglen < 0 || static_cast(msglen) >= disconnect.msg.size()) disconnect.msg[sizeof(disconnect.msg)-1] = 0; for(ContextBase *ctx : *mContexts.load()) diff --git a/alc/backends/alsa.cpp b/alc/backends/alsa.cpp index 5aae834a..47c6385e 100644 --- a/alc/backends/alsa.cpp +++ b/alc/backends/alsa.cpp @@ -1039,7 +1039,7 @@ void AlsaCapture::captureSamples(std::byte *buffer, uint samples) { if(mRing) { - mRing->read(buffer, samples); + std::ignore = mRing->read(buffer, samples); return; } diff --git a/alc/backends/coreaudio.cpp b/alc/backends/coreaudio.cpp index 16b0781e..50e3bc66 100644 --- a/alc/backends/coreaudio.cpp +++ b/alc/backends/coreaudio.cpp @@ -657,7 +657,7 @@ OSStatus CoreAudioCapture::RecordProc(AudioUnitRenderActionFlags *ioActionFlags, return err; } - mRing->write(mCaptureData.data(), inNumberFrames); + std::ignore = mRing->write(mCaptureData.data(), inNumberFrames); return noErr; } @@ -924,7 +924,7 @@ void CoreAudioCapture::captureSamples(std::byte *buffer, uint samples) { if(!mConverter) { - mRing->read(buffer, samples); + std::ignore = mRing->read(buffer, samples); return; } diff --git a/alc/backends/dsound.cpp b/alc/backends/dsound.cpp index 58aa69b2..12196f74 100644 --- a/alc/backends/dsound.cpp +++ b/alc/backends/dsound.cpp @@ -717,7 +717,7 @@ void DSoundCapture::stop() } void DSoundCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } uint DSoundCapture::availableSamples() { @@ -740,9 +740,9 @@ uint DSoundCapture::availableSamples() } if(SUCCEEDED(hr)) { - mRing->write(ReadPtr1, ReadCnt1/FrameSize); + std::ignore = mRing->write(ReadPtr1, ReadCnt1/FrameSize); if(ReadPtr2 != nullptr && ReadCnt2 > 0) - mRing->write(ReadPtr2, ReadCnt2/FrameSize); + std::ignore = mRing->write(ReadPtr2, ReadCnt2/FrameSize); hr = mDSCbuffer->Unlock(ReadPtr1, ReadCnt1, ReadPtr2, ReadCnt2); mCursor = ReadCursor; } diff --git a/alc/backends/oboe.cpp b/alc/backends/oboe.cpp index b7bab19a..9666063a 100644 --- a/alc/backends/oboe.cpp +++ b/alc/backends/oboe.cpp @@ -230,7 +230,7 @@ struct OboeCapture final : public BackendBase, public oboe::AudioStreamCallback oboe::DataCallbackResult OboeCapture::onAudioReady(oboe::AudioStream*, void *audioData, int32_t numFrames) { - mRing->write(audioData, static_cast(numFrames)); + std::ignore = mRing->write(audioData, static_cast(numFrames)); return oboe::DataCallbackResult::Continue; } @@ -330,7 +330,7 @@ uint OboeCapture::availableSamples() { return static_cast(mRing->readSpace()); } void OboeCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } } // namespace diff --git a/alc/backends/oss.cpp b/alc/backends/oss.cpp index 87d3ba35..010b0749 100644 --- a/alc/backends/oss.cpp +++ b/alc/backends/oss.cpp @@ -617,7 +617,7 @@ void OSScapture::stop() } void OSScapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } uint OSScapture::availableSamples() { return static_cast(mRing->readSpace()); } diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index 01896b01..96b6623f 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -1933,7 +1933,8 @@ void PipeWireCapture::inputCallback() noexcept const uint offset{minu(bufdata->chunk->offset, bufdata->maxsize)}; const uint size{minu(bufdata->chunk->size, bufdata->maxsize - offset)}; - mRing->write(static_cast(bufdata->data) + offset, size / mRing->getElemSize()); + std::ignore = mRing->write(static_cast(bufdata->data) + offset, + size / mRing->getElemSize()); pw_stream_queue_buffer(mStream.get(), pw_buf); } @@ -2154,7 +2155,7 @@ uint PipeWireCapture::availableSamples() { return static_cast(mRing->readSpace()); } void PipeWireCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } } // namespace diff --git a/alc/backends/portaudio.cpp b/alc/backends/portaudio.cpp index 979a54d6..2e2e33cc 100644 --- a/alc/backends/portaudio.cpp +++ b/alc/backends/portaudio.cpp @@ -271,7 +271,7 @@ PortCapture::~PortCapture() int PortCapture::readCallback(const void *inputBuffer, void*, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo*, const PaStreamCallbackFlags) noexcept { - mRing->write(inputBuffer, framesPerBuffer); + std::ignore = mRing->write(inputBuffer, framesPerBuffer); return 0; } @@ -350,7 +350,7 @@ uint PortCapture::availableSamples() { return static_cast(mRing->readSpace()); } void PortCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } } // namespace diff --git a/alc/backends/sndio.cpp b/alc/backends/sndio.cpp index d54c337b..8bf63a59 100644 --- a/alc/backends/sndio.cpp +++ b/alc/backends/sndio.cpp @@ -497,7 +497,7 @@ void SndioCapture::stop() } void SndioCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } uint SndioCapture::availableSamples() { return static_cast(mRing->readSpace()); } diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp index 436ee402..139fa696 100644 --- a/alc/backends/wasapi.cpp +++ b/alc/backends/wasapi.cpp @@ -2651,7 +2651,7 @@ void WasapiCapture::stopProxy() void WasapiCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } uint WasapiCapture::availableSamples() { return static_cast(mRing->readSpace()); } diff --git a/alc/backends/winmm.cpp b/alc/backends/winmm.cpp index f0fb0a1c..15776d17 100644 --- a/alc/backends/winmm.cpp +++ b/alc/backends/winmm.cpp @@ -435,7 +435,8 @@ int WinMMCapture::captureProc() WAVEHDR &waveHdr = mWaveBuffer[widx]; widx = (widx+1) % mWaveBuffer.size(); - mRing->write(waveHdr.lpData, waveHdr.dwBytesRecorded / mFormat.nBlockAlign); + std::ignore = mRing->write(waveHdr.lpData, + waveHdr.dwBytesRecorded / mFormat.nBlockAlign); mReadable.fetch_sub(1, std::memory_order_acq_rel); waveInAddBuffer(mInHdl, &waveHdr, sizeof(WAVEHDR)); } while(--todo); @@ -573,7 +574,7 @@ void WinMMCapture::stop() } void WinMMCapture::captureSamples(std::byte *buffer, uint samples) -{ mRing->read(buffer, samples); } +{ std::ignore = mRing->read(buffer, samples); } uint WinMMCapture::availableSamples() { return static_cast(mRing->readSpace()); } diff --git a/alc/context.h b/alc/context.h index b190c5ea..32db76c7 100644 --- a/alc/context.h +++ b/alc/context.h @@ -560,7 +560,7 @@ private: using ContextRef = al::intrusive_ptr; -ContextRef GetContextRef(void); +ContextRef GetContextRef(); void UpdateContextProps(ALCcontext *context); diff --git a/alc/effects/base.h b/alc/effects/base.h index 95695857..025ac663 100644 --- a/alc/effects/base.h +++ b/alc/effects/base.h @@ -4,23 +4,29 @@ #include "core/effects/base.h" -EffectStateFactory *NullStateFactory_getFactory(void); -EffectStateFactory *ReverbStateFactory_getFactory(void); -EffectStateFactory *StdReverbStateFactory_getFactory(void); -EffectStateFactory *AutowahStateFactory_getFactory(void); -EffectStateFactory *ChorusStateFactory_getFactory(void); -EffectStateFactory *CompressorStateFactory_getFactory(void); -EffectStateFactory *DistortionStateFactory_getFactory(void); -EffectStateFactory *EchoStateFactory_getFactory(void); -EffectStateFactory *EqualizerStateFactory_getFactory(void); -EffectStateFactory *FlangerStateFactory_getFactory(void); -EffectStateFactory *FshifterStateFactory_getFactory(void); -EffectStateFactory *ModulatorStateFactory_getFactory(void); -EffectStateFactory *PshifterStateFactory_getFactory(void); -EffectStateFactory* VmorpherStateFactory_getFactory(void); - -EffectStateFactory *DedicatedStateFactory_getFactory(void); - -EffectStateFactory *ConvolutionStateFactory_getFactory(void); +/* This is a user config option for modifying the overall output of the reverb + * effect. + */ +inline float ReverbBoost{1.0f}; + + +EffectStateFactory *NullStateFactory_getFactory(); +EffectStateFactory *ReverbStateFactory_getFactory(); +EffectStateFactory *StdReverbStateFactory_getFactory(); +EffectStateFactory *AutowahStateFactory_getFactory(); +EffectStateFactory *ChorusStateFactory_getFactory(); +EffectStateFactory *CompressorStateFactory_getFactory(); +EffectStateFactory *DistortionStateFactory_getFactory(); +EffectStateFactory *EchoStateFactory_getFactory(); +EffectStateFactory *EqualizerStateFactory_getFactory(); +EffectStateFactory *FlangerStateFactory_getFactory(); +EffectStateFactory *FshifterStateFactory_getFactory(); +EffectStateFactory *ModulatorStateFactory_getFactory(); +EffectStateFactory *PshifterStateFactory_getFactory(); +EffectStateFactory* VmorpherStateFactory_getFactory(); + +EffectStateFactory *DedicatedStateFactory_getFactory(); + +EffectStateFactory *ConvolutionStateFactory_getFactory(); #endif /* EFFECTS_BASE_H */ diff --git a/alc/effects/reverb.cpp b/alc/effects/reverb.cpp index 6a9d1997..5157cf72 100644 --- a/alc/effects/reverb.cpp +++ b/alc/effects/reverb.cpp @@ -48,11 +48,6 @@ #include "vecmat.h" #include "vector.h" -/* This is a user config option for modifying the overall output of the reverb - * effect. - */ -float ReverbBoost = 1.0f; - namespace { using uint = unsigned int; diff --git a/common/albit.h b/common/albit.h index 82a4a00d..d54a189c 100644 --- a/common/albit.h +++ b/common/albit.h @@ -1,6 +1,7 @@ #ifndef AL_BIT_H #define AL_BIT_H +#include #include #include #include @@ -17,9 +18,9 @@ std::enable_if_t, To> bit_cast(const From &src) noexcept { - alignas(To) char dst[sizeof(To)]; - std::memcpy(&dst[0], &src, sizeof(To)); - return *std::launder(reinterpret_cast(&dst[0])); + alignas(To) std::array dst; + std::memcpy(dst.data(), &src, sizeof(To)); + return *std::launder(reinterpret_cast(dst.data())); } #ifdef __BYTE_ORDER__ diff --git a/common/almalloc.h b/common/almalloc.h index 873473ca..288b5075 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -211,34 +211,34 @@ struct FlexArray { FlexArray(index_type size) : mStore{size} { } ~FlexArray() = default; - index_type size() const noexcept { return mStore.mSize; } - bool empty() const noexcept { return mStore.mSize == 0; } - - pointer data() noexcept { return mStore.mArray; } - const_pointer data() const noexcept { return mStore.mArray; } - - reference operator[](index_type i) noexcept { return mStore.mArray[i]; } - const_reference operator[](index_type i) const noexcept { return mStore.mArray[i]; } - - reference front() noexcept { return mStore.mArray[0]; } - const_reference front() const noexcept { return mStore.mArray[0]; } - - reference back() noexcept { return mStore.mArray[mStore.mSize-1]; } - const_reference back() const noexcept { return mStore.mArray[mStore.mSize-1]; } - - iterator begin() noexcept { return mStore.mArray; } - const_iterator begin() const noexcept { return mStore.mArray; } - const_iterator cbegin() const noexcept { return mStore.mArray; } - iterator end() noexcept { return mStore.mArray + mStore.mSize; } - const_iterator end() const noexcept { return mStore.mArray + mStore.mSize; } - const_iterator cend() const noexcept { return mStore.mArray + mStore.mSize; } - - reverse_iterator rbegin() noexcept { return end(); } - const_reverse_iterator rbegin() const noexcept { return end(); } - const_reverse_iterator crbegin() const noexcept { return cend(); } - reverse_iterator rend() noexcept { return begin(); } - const_reverse_iterator rend() const noexcept { return begin(); } - const_reverse_iterator crend() const noexcept { return cbegin(); } + [[nodiscard]] auto size() const noexcept -> index_type { return mStore.mSize; } + [[nodiscard]] auto empty() const noexcept -> bool { return mStore.mSize == 0; } + + [[nodiscard]] auto data() noexcept -> pointer { return mStore.mArray; } + [[nodiscard]] auto data() const noexcept -> const_pointer { return mStore.mArray; } + + [[nodiscard]] auto operator[](index_type i) noexcept -> reference { return mStore.mArray[i]; } + [[nodiscard]] auto operator[](index_type i) const noexcept -> const_reference { return mStore.mArray[i]; } + + [[nodiscard]] auto front() noexcept -> reference { return mStore.mArray[0]; } + [[nodiscard]] auto front() const noexcept -> const_reference { return mStore.mArray[0]; } + + [[nodiscard]] auto back() noexcept -> reference { return mStore.mArray[mStore.mSize-1]; } + [[nodiscard]] auto back() const noexcept -> const_reference { return mStore.mArray[mStore.mSize-1]; } + + [[nodiscard]] auto begin() noexcept -> iterator { return mStore.mArray; } + [[nodiscard]] auto begin() const noexcept -> const_iterator { return mStore.mArray; } + [[nodiscard]] auto cbegin() const noexcept -> const_iterator { return mStore.mArray; } + [[nodiscard]] auto end() noexcept -> iterator { return mStore.mArray + mStore.mSize; } + [[nodiscard]] auto end() const noexcept -> const_iterator { return mStore.mArray + mStore.mSize; } + [[nodiscard]] auto cend() const noexcept -> const_iterator { return mStore.mArray + mStore.mSize; } + + [[nodiscard]] auto rbegin() noexcept -> reverse_iterator { return end(); } + [[nodiscard]] auto rbegin() const noexcept -> const_reverse_iterator { return end(); } + [[nodiscard]] auto crbegin() const noexcept -> const_reverse_iterator { return cend(); } + [[nodiscard]] auto rend() noexcept -> reverse_iterator { return begin(); } + [[nodiscard]] auto rend() const noexcept -> const_reverse_iterator { return begin(); } + [[nodiscard]] auto crend() const noexcept -> const_reverse_iterator { return cbegin(); } DEF_PLACE_NEWDEL() }; diff --git a/common/alnumbers.h b/common/alnumbers.h index e92d7b87..7abe6b32 100644 --- a/common/alnumbers.h +++ b/common/alnumbers.h @@ -3,9 +3,7 @@ #include -namespace al { - -namespace numbers { +namespace al::numbers { namespace detail_ { template @@ -29,8 +27,6 @@ inline constexpr auto inv_pi = inv_pi_v; inline constexpr auto sqrt2 = sqrt2_v; inline constexpr auto sqrt3 = sqrt3_v; -} // namespace numbers - -} // namespace al +} // namespace al::numbers #endif /* COMMON_ALNUMBERS_H */ diff --git a/common/alnumeric.h b/common/alnumeric.h index 6281b012..cb8704b2 100644 --- a/common/alnumeric.h +++ b/common/alnumeric.h @@ -245,7 +245,7 @@ inline float fast_roundf(float f) noexcept /* Integral limit, where sub-integral precision is not available for * floats. */ - static constexpr float ilim[2]{ + static constexpr std::array ilim{ 8388608.0f /* 0x1.0p+23 */, -8388608.0f /* -0x1.0p+23 */ }; diff --git a/common/alspan.h b/common/alspan.h index 341ce7c8..d91747c2 100644 --- a/common/alspan.h +++ b/common/alspan.h @@ -107,43 +107,43 @@ public: constexpr span& operator=(const span &rhs) noexcept = default; - constexpr reference front() const { return *mData; } - constexpr reference back() const { return *(mData+E-1); } - constexpr reference operator[](index_type idx) const { return mData[idx]; } - constexpr pointer data() const noexcept { return mData; } - - constexpr index_type size() const noexcept { return E; } - constexpr index_type size_bytes() const noexcept { return E * sizeof(value_type); } - constexpr bool empty() const noexcept { return E == 0; } - - constexpr iterator begin() const noexcept { return mData; } - constexpr iterator end() const noexcept { return mData+E; } - constexpr const_iterator cbegin() const noexcept { return mData; } - constexpr const_iterator cend() const noexcept { return mData+E; } - - constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } - constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } - constexpr const_reverse_iterator crbegin() const noexcept + [[nodiscard]] constexpr auto front() const -> reference { return *mData; } + [[nodiscard]] constexpr auto back() const -> reference { return *(mData+E-1); } + [[nodiscard]] constexpr auto operator[](index_type idx) const -> reference { return mData[idx]; } + [[nodiscard]] constexpr auto data() const noexcept -> pointer { return mData; } + + [[nodiscard]] constexpr auto size() const noexcept -> index_type { return E; } + [[nodiscard]] constexpr auto size_bytes() const noexcept -> index_type { return E * sizeof(value_type); } + [[nodiscard]] constexpr auto empty() const noexcept -> bool { return E == 0; } + + [[nodiscard]] constexpr auto begin() const noexcept -> iterator { return mData; } + [[nodiscard]] constexpr auto end() const noexcept -> iterator { return mData+E; } + [[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return mData; } + [[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return mData+E; } + + [[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; } + [[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; } + [[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cend()}; } - constexpr const_reverse_iterator crend() const noexcept + [[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cbegin()}; } template - constexpr span first() const + [[nodiscard]] constexpr auto first() const -> span { static_assert(E >= C, "New size exceeds original capacity"); return span{mData, C}; } template - constexpr span last() const + [[nodiscard]] constexpr auto last() const -> span { static_assert(E >= C, "New size exceeds original capacity"); return span{mData+(E-C), C}; } template - constexpr auto subspan() const -> std::enable_if_t> + [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { static_assert(E >= O, "Offset exceeds extent"); static_assert(E-O >= C, "New size exceeds original capacity"); @@ -151,7 +151,7 @@ public: } template - constexpr auto subspan() const -> std::enable_if_t> + [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { static_assert(E >= O, "Offset exceeds extent"); return span{mData+O, E-O}; @@ -161,10 +161,10 @@ public: * defining the specialization. As a result, these methods need to be * defined later. */ - constexpr span first(size_t count) const; - constexpr span last(size_t count) const; - constexpr span subspan(size_t offset, - size_t count=dynamic_extent) const; + [[nodiscard]] constexpr auto first(size_t count) const -> span; + [[nodiscard]] constexpr auto last(size_t count) const -> span; + [[nodiscard]] constexpr auto subspan(size_t offset, + size_t count=dynamic_extent) const -> span; private: pointer mData{nullptr}; @@ -221,51 +221,51 @@ public: constexpr span& operator=(const span &rhs) noexcept = default; - constexpr reference front() const { return *mData; } - constexpr reference back() const { return *(mDataEnd-1); } - constexpr reference operator[](index_type idx) const { return mData[idx]; } - constexpr pointer data() const noexcept { return mData; } + [[nodiscard]] constexpr auto front() const -> reference { return *mData; } + [[nodiscard]] constexpr auto back() const -> reference { return *(mDataEnd-1); } + [[nodiscard]] constexpr auto operator[](index_type idx) const -> reference { return mData[idx]; } + [[nodiscard]] constexpr auto data() const noexcept -> pointer { return mData; } - constexpr index_type size() const noexcept { return static_cast(mDataEnd-mData); } - constexpr index_type size_bytes() const noexcept + [[nodiscard]] constexpr auto size() const noexcept -> index_type { return static_cast(mDataEnd-mData); } + [[nodiscard]] constexpr auto size_bytes() const noexcept -> index_type { return static_cast(mDataEnd-mData) * sizeof(value_type); } - constexpr bool empty() const noexcept { return mData == mDataEnd; } + [[nodiscard]] constexpr auto empty() const noexcept -> bool { return mData == mDataEnd; } - constexpr iterator begin() const noexcept { return mData; } - constexpr iterator end() const noexcept { return mDataEnd; } - constexpr const_iterator cbegin() const noexcept { return mData; } - constexpr const_iterator cend() const noexcept { return mDataEnd; } + [[nodiscard]] constexpr auto begin() const noexcept -> iterator { return mData; } + [[nodiscard]] constexpr auto end() const noexcept -> iterator { return mDataEnd; } + [[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return mData; } + [[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return mDataEnd; } - constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } - constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } - constexpr const_reverse_iterator crbegin() const noexcept + [[nodiscard]] constexpr auto rbegin() const noexcept -> reverse_iterator { return reverse_iterator{end()}; } + [[nodiscard]] constexpr auto rend() const noexcept -> reverse_iterator { return reverse_iterator{begin()}; } + [[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cend()}; } - constexpr const_reverse_iterator crend() const noexcept + [[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator { return const_reverse_iterator{cbegin()}; } template - constexpr span first() const + [[nodiscard]] constexpr auto first() const -> span { return span{mData, C}; } - constexpr span first(size_t count) const + [[nodiscard]] constexpr auto first(size_t count) const -> span { return (count >= size()) ? *this : span{mData, mData+count}; } template - constexpr span last() const + [[nodiscard]] constexpr auto last() const -> span { return span{mDataEnd-C, C}; } - constexpr span last(size_t count) const + [[nodiscard]] constexpr auto last(size_t count) const -> span { return (count >= size()) ? *this : span{mDataEnd-count, mDataEnd}; } template - constexpr auto subspan() const -> std::enable_if_t> + [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { return span{mData+O, C}; } template - constexpr auto subspan() const -> std::enable_if_t> + [[nodiscard]] constexpr auto subspan() const -> std::enable_if_t> { return span{mData+O, mDataEnd}; } - constexpr span subspan(size_t offset, size_t count=dynamic_extent) const + [[nodiscard]] constexpr auto subspan(size_t offset, size_t count=dynamic_extent) const -> span { return (offset > size()) ? span{} : (count >= size()-offset) ? span{mData+offset, mDataEnd} : @@ -278,21 +278,21 @@ private: }; template -constexpr inline auto span::first(size_t count) const -> span +[[nodiscard]] constexpr inline auto span::first(size_t count) const -> span { return (count >= size()) ? span{mData, extent} : span{mData, count}; } template -constexpr inline auto span::last(size_t count) const -> span +[[nodiscard]] constexpr inline auto span::last(size_t count) const -> span { return (count >= size()) ? span{mData, extent} : span{mData+extent-count, count}; } template -constexpr inline auto span::subspan(size_t offset, size_t count) const +[[nodiscard]] constexpr inline auto span::subspan(size_t offset, size_t count) const -> span { return (offset > size()) ? span{} : diff --git a/common/intrusive_ptr.h b/common/intrusive_ptr.h index 714a5617..0152b92a 100644 --- a/common/intrusive_ptr.h +++ b/common/intrusive_ptr.h @@ -81,9 +81,9 @@ public: explicit operator bool() const noexcept { return mPtr != nullptr; } - T& operator*() const noexcept { return *mPtr; } - T* operator->() const noexcept { return mPtr; } - T* get() const noexcept { return mPtr; } + [[nodiscard]] auto operator*() const noexcept -> T& { return *mPtr; } + [[nodiscard]] auto operator->() const noexcept -> T* { return mPtr; } + [[nodiscard]] auto get() const noexcept -> T* { return mPtr; } void reset(T *ptr=nullptr) noexcept { diff --git a/common/pffft.cpp b/common/pffft.cpp index 8e849cb4..505c9791 100644 --- a/common/pffft.cpp +++ b/common/pffft.cpp @@ -58,11 +58,11 @@ #include "pffft.h" #include -#include +#include #include +#include +#include #include -#include -#include #include #include "albit.h" @@ -90,7 +90,7 @@ using uint = unsigned int; * Altivec support macros */ #if defined(__ppc__) || defined(__ppc64__) || defined(__powerpc__) || defined(__powerpc64__) -typedef vector float v4sf; +using v4sf = vector float; #define SIMD_SZ 4 #define VZERO() ((vector float) vec_splat_u8(0)) #define VMUL(a,b) vec_madd(a,b, VZERO()) @@ -142,7 +142,7 @@ force_inline void vtranspose4(v4sf &x0, v4sf &x1, v4sf &x2, v4sf &x3) noexcept (defined(_M_IX86_FP) && _M_IX86_FP >= 1) #include -typedef __m128 v4sf; +using v4sf = __m128; #define SIMD_SZ 4 // 4 floats by simd vector -- this is pretty much hardcoded in the preprocess/finalize functions anyway so you will have to work if you want to enable AVX with its 256-bit vectors. #define VZERO _mm_setzero_ps #define VMUL _mm_mul_ps @@ -178,7 +178,7 @@ force_inline void vtranspose4(v4sf &x0, v4sf &x1, v4sf &x2, v4sf &x3) noexcept #elif defined(__ARM_NEON) || defined(__aarch64__) || defined(__arm64) #include -typedef float32x4_t v4sf; +using v4sf = float32x4_t; #define SIMD_SZ 4 #define VZERO() vdupq_n_f32(0) #define VMUL vmulq_f32 @@ -297,7 +297,7 @@ force_inline v4sf vswaphl(v4sf a, v4sf b) noexcept // fallback mode for situations where SIMD is not available, use scalar mode instead #ifdef PFFFT_SIMD_DISABLE -typedef float v4sf; +using v4sf = float; #define SIMD_SZ 1 #define VZERO() 0.f #define VMUL(a,b) ((a)*(b)) @@ -335,14 +335,14 @@ force_inline void vcplxmulconj(v4sf &ar, v4sf &ai, v4sf br, v4sf bi) noexcept [[maybe_unused]] void validate_pffft_simd() { using float4 = std::array; - static constexpr float f[16]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + static constexpr std::array f{{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}}; float4 a0_f, a1_f, a2_f, a3_f, t_f, u_f; v4sf a0_v, a1_v, a2_v, a3_v, t_v, u_v; - std::memcpy(&a0_v, f, 4*sizeof(float)); - std::memcpy(&a1_v, f+4, 4*sizeof(float)); - std::memcpy(&a2_v, f+8, 4*sizeof(float)); - std::memcpy(&a3_v, f+12, 4*sizeof(float)); + std::memcpy(&a0_v, f.data(), 4*sizeof(float)); + std::memcpy(&a1_v, f.data()+4, 4*sizeof(float)); + std::memcpy(&a2_v, f.data()+8, 4*sizeof(float)); + std::memcpy(&a3_v, f.data()+12, 4*sizeof(float)); t_v = VZERO(); t_f = al::bit_cast(t_v); printf("VZERO=[%2g %2g %2g %2g]\n", t_f[0], t_f[1], t_f[2], t_f[3]); assertv4(t, 0, 0, 0, 0); @@ -1331,7 +1331,7 @@ uint decompose(const uint n, const al::span ifac, const al::span ifac) { - static constexpr uint ntryh[]{4,2,3,5}; + static constexpr std::array ntryh{4u,2u,3u,5u}; const uint nf{decompose(n, ifac, ntryh)}; const double argh{2.0*al::numbers::pi / n}; @@ -1365,7 +1365,7 @@ void rffti1_ps(const uint n, float *wa, const al::span ifac) void cffti1_ps(const uint n, float *wa, const al::span ifac) { - static constexpr uint ntryh[]{5,3,4,2}; + static constexpr std::array ntryh{5u,3u,4u,2u}; const uint nf{decompose(n, ifac, ntryh)}; const double argh{2.0*al::numbers::pi / n}; @@ -1814,7 +1814,7 @@ void pffft_transform_internal(const PFFFT_Setup *setup, const v4sf *vinput, v4sf const size_t Ncvec{setup->Ncvec}; const bool nf_odd{(setup->ifac[1]&1) != 0}; - v4sf *buff[2]{voutput, scratch}; + std::array buff{voutput, scratch}; bool ib{nf_odd != ordered}; if(direction == PFFFT_FORWARD) { diff --git a/common/ringbuffer.h b/common/ringbuffer.h index 8c65c3af..718238a3 100644 --- a/common/ringbuffer.h +++ b/common/ringbuffer.h @@ -36,26 +36,26 @@ public: RingBuffer(const std::size_t count) : mBuffer{count} { } /** Reset the read and write pointers to zero. This is not thread safe. */ - void reset() noexcept; + auto reset() noexcept -> void; /** * The non-copying data reader. Returns two ringbuffer data pointers that * hold the current readable data. If the readable data is in one segment * the second segment has zero length. */ - DataPair getReadVector() const noexcept; + [[nodiscard]] auto getReadVector() const noexcept -> DataPair; /** * The non-copying data writer. Returns two ringbuffer data pointers that * hold the current writeable data. If the writeable data is in one segment * the second segment has zero length. */ - DataPair getWriteVector() const noexcept; + [[nodiscard]] auto getWriteVector() const noexcept -> DataPair; /** * Return the number of elements available for reading. This is the number * of elements in front of the read pointer and behind the write pointer. */ - std::size_t readSpace() const noexcept + [[nodiscard]] auto readSpace() const noexcept -> size_t { const size_t w{mWritePtr.load(std::memory_order_acquire)}; const size_t r{mReadPtr.load(std::memory_order_acquire)}; @@ -66,14 +66,14 @@ public: * The copying data reader. Copy at most `cnt' elements into `dest'. * Returns the actual number of elements copied. */ - std::size_t read(void *dest, std::size_t cnt) noexcept; + [[nodiscard]] auto read(void *dest, size_t cnt) noexcept -> size_t; /** * The copying data reader w/o read pointer advance. Copy at most `cnt' * elements into `dest'. Returns the actual number of elements copied. */ - std::size_t peek(void *dest, std::size_t cnt) const noexcept; + [[nodiscard]] auto peek(void *dest, size_t cnt) const noexcept -> size_t; /** Advance the read pointer `cnt' places. */ - void readAdvance(std::size_t cnt) noexcept + auto readAdvance(size_t cnt) noexcept -> void { mReadPtr.fetch_add(cnt, std::memory_order_acq_rel); } @@ -81,7 +81,7 @@ public: * Return the number of elements available for writing. This is the number * of elements in front of the write pointer and behind the read pointer. */ - std::size_t writeSpace() const noexcept + [[nodiscard]] auto writeSpace() const noexcept -> size_t { const size_t w{mWritePtr.load(std::memory_order_acquire)}; const size_t r{mReadPtr.load(std::memory_order_acquire) + mWriteSize - mSizeMask}; @@ -92,12 +92,12 @@ public: * The copying data writer. Copy at most `cnt' elements from `src'. Returns * the actual number of elements copied. */ - std::size_t write(const void *src, std::size_t cnt) noexcept; + [[nodiscard]] auto write(const void *src, size_t cnt) noexcept -> size_t; /** Advance the write pointer `cnt' places. */ - void writeAdvance(std::size_t cnt) noexcept + auto writeAdvance(size_t cnt) noexcept -> void { mWritePtr.fetch_add(cnt, std::memory_order_acq_rel); } - std::size_t getElemSize() const noexcept { return mElemSize; } + [[nodiscard]] auto getElemSize() const noexcept -> size_t { return mElemSize; } /** * Create a new ringbuffer to hold at least `sz' elements of `elem_sz' @@ -105,7 +105,8 @@ public: * (even if it is already a power of two, to ensure the requested amount * can be written). */ - static std::unique_ptr Create(std::size_t sz, std::size_t elem_sz, int limit_writes); + [[nodiscard]] + static auto Create(size_t sz, size_t elem_sz, int limit_writes) -> std::unique_ptr; DEF_FAM_NEWDEL(RingBuffer, mBuffer) }; diff --git a/common/vecmat.h b/common/vecmat.h index a45f262f..0cdb82eb 100644 --- a/common/vecmat.h +++ b/common/vecmat.h @@ -14,7 +14,7 @@ namespace alu { template class VectorR { static_assert(std::is_floating_point::value, "Must use floating-point types"); - alignas(16) T mVals[4]; + alignas(16) std::array mVals; public: constexpr VectorR() noexcept = default; @@ -58,7 +58,7 @@ public: return T{0}; } - constexpr VectorR cross_product(const alu::VectorR &rhs) const noexcept + [[nodiscard]] constexpr auto cross_product(const alu::VectorR &rhs) const noexcept -> VectorR { return VectorR{ mVals[1]*rhs.mVals[2] - mVals[2]*rhs.mVals[1], @@ -67,7 +67,7 @@ public: T{0}}; } - constexpr T dot_product(const alu::VectorR &rhs) const noexcept + [[nodiscard]] constexpr auto dot_product(const alu::VectorR &rhs) const noexcept -> T { return mVals[0]*rhs.mVals[0] + mVals[1]*rhs.mVals[1] + mVals[2]*rhs.mVals[2]; } }; using Vector = VectorR; @@ -75,7 +75,7 @@ using Vector = VectorR; template class MatrixR { static_assert(std::is_floating_point::value, "Must use floating-point types"); - alignas(16) T mVals[16]; + alignas(16) std::array mVals; public: constexpr MatrixR() noexcept = default; diff --git a/core/async_event.h b/core/async_event.h index f1ca0c7b..20857c9c 100644 --- a/core/async_event.h +++ b/core/async_event.h @@ -1,6 +1,7 @@ #ifndef CORE_EVENT_H #define CORE_EVENT_H +#include #include #include @@ -39,7 +40,7 @@ struct AsyncBufferCompleteEvent { }; struct AsyncDisconnectEvent { - char msg[244]; + std::array msg; }; struct AsyncEffectReleaseEvent { diff --git a/core/effects/base.h b/core/effects/base.h index 83df7cf0..7d8770fb 100644 --- a/core/effects/base.h +++ b/core/effects/base.h @@ -71,10 +71,10 @@ union EffectProps { float DecayLFRatio; float ReflectionsGain; float ReflectionsDelay; - float ReflectionsPan[3]; + std::array ReflectionsPan; float LateReverbGain; float LateReverbDelay; - float LateReverbPan[3]; + std::array LateReverbPan; float EchoTime; float EchoDepth; float ModulationTime; diff --git a/core/except.h b/core/except.h index 0e28e9df..eec876db 100644 --- a/core/except.h +++ b/core/except.h @@ -14,12 +14,12 @@ class base_exception : public std::exception { protected: base_exception() = default; - virtual ~base_exception(); + ~base_exception() override; - void setMessage(const char *msg, std::va_list args); + auto setMessage(const char *msg, std::va_list args) -> void; public: - const char *what() const noexcept override { return mMessage.c_str(); } + [[nodiscard]] auto what() const noexcept -> const char* override { return mMessage.c_str(); } }; } // namespace al diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index 1f02ef70..54803035 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -474,8 +474,8 @@ nanoseconds AudioState::getClockNoLock() // Get the current device clock time and latency. auto device = alcGetContextsDevice(alcGetCurrentContext()); - ALCint64SOFT devtimes[2]{0,0}; - alcGetInteger64vSOFT(device, ALC_DEVICE_CLOCK_LATENCY_SOFT, 2, devtimes); + std::array devtimes{}; + alcGetInteger64vSOFT(device, ALC_DEVICE_CLOCK_LATENCY_SOFT, 2, devtimes.data()); auto latency = nanoseconds{devtimes[1]}; auto device_time = nanoseconds{devtimes[0]}; @@ -494,15 +494,14 @@ nanoseconds AudioState::getClockNoLock() * actually the timestamp of the first sample frame played. The audio * clock, then, is that plus the current source offset. */ - ALint64SOFT offset[2]; + std::array offset{}; if(alGetSourcei64vSOFT) - alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset); + alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset.data()); else { ALint ioffset; alGetSourcei(mSource, AL_SAMPLE_OFFSET, &ioffset); offset[0] = ALint64SOFT{ioffset} << 32; - offset[1] = 0; } /* NOTE: The source state must be checked last, in case an underrun * occurs and the source stops between getting the state and retrieving @@ -550,15 +549,14 @@ nanoseconds AudioState::getClockNoLock() nanoseconds pts{mCurrentPts}; if(mSource) { - ALint64SOFT offset[2]; + std::array offset{}; if(alGetSourcei64vSOFT) - alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset); + alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_LATENCY_SOFT, offset.data()); else { ALint ioffset; alGetSourcei(mSource, AL_SAMPLE_OFFSET, &ioffset); offset[0] = ALint64SOFT{ioffset} << 32; - offset[1] = 0; } ALint queued, status; alGetSourcei(mSource, AL_BUFFERS_QUEUED, &queued); @@ -610,8 +608,8 @@ bool AudioState::startPlayback() /* Subtract the total buffer queue time from the current pts to get the * pts of the start of the queue. */ - int64_t srctimes[2]{0,0}; - alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_CLOCK_SOFT, srctimes); + std::array srctimes{}; + alGetSourcei64vSOFT(mSource, AL_SAMPLE_OFFSET_CLOCK_SOFT, srctimes.data()); auto device_time = nanoseconds{srctimes[1]}; auto src_offset = duration_cast(fixed32{srctimes[0]}) / mCodecCtx->sample_rate; @@ -1151,9 +1149,9 @@ int AudioState::handler() mSwresCtx.reset(ps); if(err != 0) { - char errstr[AV_ERROR_MAX_STRING_SIZE]{}; + std::array errstr{}; std::cerr<< "Failed to allocate SwrContext: " - < errstr{}; std::cerr<< "Failed to allocate SwrContext: " - < errstr{}; std::cerr<< "Failed to initialize audio converter: " - <(M_PI / 3.0), static_cast(-M_PI / 3.0)}; - alSourcefv(mSource, AL_STEREO_ANGLES, angles); + const std::array angles{static_cast(M_PI / 3.0), static_cast(-M_PI / 3.0)}; + alSourcefv(mSource, AL_STEREO_ANGLES, angles.data()); } if(has_bfmt_ex) { @@ -1260,7 +1258,7 @@ int AudioState::handler() /* Prefill the codec buffer. */ auto packet_sender = [this]() { - while(1) + while(true) { const int ret{mQueue.sendPacket(mCodecCtx.get())}; if(ret == AVErrorEOF) break; @@ -1287,7 +1285,7 @@ int AudioState::handler() mCurrentPts += skip; } - while(1) + while(true) { if(mMovie.mQuit.load(std::memory_order_relaxed)) { @@ -1451,7 +1449,7 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re auto clocktime = mMovie.getMasterClock(); bool updated{false}; - while(1) + while(true) { size_t next_idx{(read_idx+1)%mPictQ.size()}; if(next_idx == mPictQWrite.load(std::memory_order_acquire)) @@ -1546,18 +1544,15 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re } /* point pict at the queue */ - uint8_t *pict_data[3]; + std::array pict_data; pict_data[0] = static_cast(pixels); pict_data[1] = pict_data[0] + w*h; pict_data[2] = pict_data[1] + w*h/4; - int pict_linesize[3]; - pict_linesize[0] = pitch; - pict_linesize[1] = pitch / 2; - pict_linesize[2] = pitch / 2; + std::array pict_linesize{pitch, pitch/2, pitch/2}; - sws_scale(mSwscaleCtx.get(), reinterpret_cast(frame->data), frame->linesize, - 0, h, pict_data, pict_linesize); + sws_scale(mSwscaleCtx.get(), reinterpret_cast(frame->data), + frame->linesize, 0, h, pict_data.data(), pict_linesize.data()); SDL_UnlockTexture(mImage); } @@ -1599,7 +1594,7 @@ int VideoState::handler() /* Prefill the codec buffer. */ auto packet_sender = [this]() { - while(1) + while(true) { const int ret{mQueue.sendPacket(mCodecCtx.get())}; if(ret == AVErrorEOF) break; @@ -1613,7 +1608,7 @@ int VideoState::handler() } auto current_pts = nanoseconds::zero(); - while(1) + while(true) { size_t write_idx{mPictQWrite.load(std::memory_order_relaxed)}; Picture *vp{&mPictQ[write_idx]}; @@ -2062,7 +2057,7 @@ int main(int argc, char *argv[]) while(fileidx < argc && !movState) { - movState = std::unique_ptr{new MovieState{argv[fileidx++]}}; + movState = std::make_unique(argv[fileidx++]); if(!movState->prepare()) movState = nullptr; } if(!movState) @@ -2077,7 +2072,7 @@ int main(int argc, char *argv[]) Next, Quit } eom_action{EomAction::Next}; seconds last_time{seconds::min()}; - while(1) + while(true) { /* SDL_WaitEventTimeout is broken, just force a 10ms sleep. */ std::this_thread::sleep_for(milliseconds{10}); @@ -2145,7 +2140,7 @@ int main(int argc, char *argv[]) movState = nullptr; while(fileidx < argc && !movState) { - movState = std::unique_ptr{new MovieState{argv[fileidx++]}}; + movState = std::make_unique(argv[fileidx++]); if(!movState->prepare()) movState = nullptr; } if(movState) diff --git a/utils/alsoft-config/mainwindow.cpp b/utils/alsoft-config/mainwindow.cpp index bee7022f..207c98c4 100644 --- a/utils/alsoft-config/mainwindow.cpp +++ b/utils/alsoft-config/mainwindow.cpp @@ -583,7 +583,7 @@ QStringList MainWindow::collectHrtfs() break; } ++i; - } while(1); + } while(true); } } } @@ -618,7 +618,7 @@ QStringList MainWindow::collectHrtfs() break; } ++i; - } while(1); + } while(true); } } } -- cgit v1.2.3 From 4527b873788373edb630046b0ab586255aa15e44 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 8 Dec 2023 05:09:58 -0800 Subject: Try to work around a compiler issue with HexPrinter --- al/source.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'al/source.cpp') diff --git a/al/source.cpp b/al/source.cpp index 9c449434..a6fe4225 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -1371,21 +1371,22 @@ struct PropType { static const char *Name() { return "float"; } }; template<> struct PropType { static const char *Name() { return "double"; } }; -template struct HexPrinter { - char mStr[sizeof(T)*2 + 3]{}; + std::array mStr{}; + + template HexPrinter(T value) { using ST = std::make_signed_t>; if constexpr(std::is_same_v) - std::snprintf(mStr, std::size(mStr), "0x%x", value); + std::snprintf(mStr.data(), mStr.size(), "0x%x", value); else if constexpr(std::is_same_v) - std::snprintf(mStr, std::size(mStr), "0x%lx", value); + std::snprintf(mStr.data(), mStr.size(), "0x%lx", value); else if constexpr(std::is_same_v) - std::snprintf(mStr, std::size(mStr), "0x%llx", value); + std::snprintf(mStr.data(), mStr.size(), "0x%llx", value); } - const char *c_str() const noexcept { return mStr; } + [[nodiscard]] auto c_str() const noexcept -> const char* { return mStr.data(); } }; -- cgit v1.2.3 From 040c172cdf186c9ccfb0642aa9ac598f115bb46b Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 8 Dec 2023 10:11:08 -0800 Subject: Clean up some more clang-tidy warnings --- al/auxeffectslot.cpp | 4 +-- al/source.cpp | 12 +++---- al/source.h | 19 ++++++----- alc/alc.cpp | 42 ++++++++++++------------ alc/alu.cpp | 21 ++++++------ alc/backends/base.h | 2 +- alc/backends/jack.cpp | 6 ++-- alc/backends/opensl.cpp | 2 +- alc/backends/pipewire.cpp | 6 ++-- alc/context.cpp | 2 +- alc/effects/convolution.cpp | 10 +++--- alc/effects/dedicated.cpp | 8 ++--- alc/panning.cpp | 16 ++++----- common/ringbuffer.cpp | 4 ++- core/bformatdec.cpp | 16 +++++---- core/bformatdec.h | 6 ++-- core/buffer_storage.h | 10 +++--- core/context.cpp | 2 +- core/context.h | 4 +-- core/devformat.h | 3 +- core/device.h | 34 +++++++++---------- core/filters/biquad.h | 4 +-- core/filters/nfc.h | 9 +++--- core/fmt_traits.cpp | 8 ++--- core/fmt_traits.h | 5 +-- core/mixer/defs.h | 3 +- core/mixer/mixer_neon.cpp | 2 +- core/mixer/mixer_sse2.cpp | 2 +- core/mixer/mixer_sse41.cpp | 2 +- core/uhjfilter.h | 2 +- core/voice.cpp | 79 +++++++++++++++++++++++---------------------- core/voice.h | 13 ++++---- 32 files changed, 184 insertions(+), 174 deletions(-) (limited to 'al/source.cpp') diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index 31e9542b..7b7672a5 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -164,7 +164,7 @@ void AddActiveEffectSlots(const al::span auxslots, ALCcontext *co std::uninitialized_fill_n(newarray->end(), newcount, nullptr); curarray = context->mActiveAuxSlots.exchange(newarray, std::memory_order_acq_rel); - context->mDevice->waitForMix(); + std::ignore = context->mDevice->waitForMix(); std::destroy_n(curarray->end(), curarray->size()); delete curarray; @@ -203,7 +203,7 @@ void RemoveActiveEffectSlots(const al::span auxslots, ALCcontext std::uninitialized_fill_n(newarray->end(), newsize, nullptr); curarray = context->mActiveAuxSlots.exchange(newarray, std::memory_order_acq_rel); - context->mDevice->waitForMix(); + std::ignore = context->mDevice->waitForMix(); std::destroy_n(curarray->end(), curarray->size()); delete curarray; diff --git a/al/source.cpp b/al/source.cpp index a6fe4225..c99943cf 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -171,7 +171,7 @@ void UpdateSourceProps(const ALsource *source, Voice *voice, ALCcontext *context ret.LFReference = srcsend.LFReference; return ret; }; - std::transform(source->Send.cbegin(), source->Send.cend(), props->Send, copy_send); + std::transform(source->Send.cbegin(), source->Send.cend(), props->Send.begin(), copy_send); if(!props->Send[0].Slot && context->mDefaultSlot) props->Send[0].Slot = context->mDefaultSlot->mSlot; @@ -575,7 +575,7 @@ void SendVoiceChanges(ALCcontext *ctx, VoiceChange *tail) oldhead->mNext.store(tail, std::memory_order_release); const bool connected{device->Connected.load(std::memory_order_acquire)}; - device->waitForMix(); + std::ignore = device->waitForMix(); if(!connected) UNLIKELY { if(ctx->mStopVoicesOnDisconnect.load(std::memory_order_acquire)) @@ -681,7 +681,7 @@ bool SetVoiceOffset(Voice *oldvoice, const VoicePos &vpos, ALsource *source, ALC return true; /* Otherwise, wait for any current mix to finish and check one last time. */ - device->waitForMix(); + std::ignore = device->waitForMix(); if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending) return true; /* The change-over failed because the old voice stopped before the new @@ -1316,11 +1316,11 @@ constexpr ALuint DoubleValsByProp(ALenum prop) struct check_exception : std::exception { }; struct check_size_exception final : check_exception { - const char *what() const noexcept override + [[nodiscard]] auto what() const noexcept -> const char* override { return "check_size_exception"; } }; struct check_value_exception final : check_exception { - const char *what() const noexcept override + [[nodiscard]] auto what() const noexcept -> const char* override { return "check_value_exception"; } }; @@ -1580,7 +1580,7 @@ NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, con * to ensure it isn't currently looping back or reaching the * end. */ - device->waitForMix(); + std::ignore = device->waitForMix(); } return; } diff --git a/al/source.h b/al/source.h index 26d425ef..3fd43a5c 100644 --- a/al/source.h +++ b/al/source.h @@ -37,7 +37,7 @@ enum class SourceStereo : bool { Enhanced = AL_SUPER_STEREO_SOFT }; -#define DEFAULT_SENDS 2 +inline constexpr size_t DefaultSendCount{2}; inline constexpr ALuint InvalidVoiceIndex{std::numeric_limits::max()}; @@ -122,7 +122,7 @@ struct ALsource { float GainLF; float LFReference; }; - std::array Send; + std::array Send; /** * Last user-specified offset, and the offset type (bytes, samples, or @@ -173,18 +173,18 @@ public: private: using Exception = EaxSourceException; - static constexpr auto eax_max_speakers = 9; + static constexpr auto eax_max_speakers{9u}; - using EaxFxSlotIds = const GUID* [EAX_MAX_FXSLOTS]; + using EaxFxSlotIds = std::array; - static constexpr const EaxFxSlotIds eax4_fx_slot_ids = { + static constexpr const EaxFxSlotIds eax4_fx_slot_ids{ &EAXPROPERTYID_EAX40_FXSlot0, &EAXPROPERTYID_EAX40_FXSlot1, &EAXPROPERTYID_EAX40_FXSlot2, &EAXPROPERTYID_EAX40_FXSlot3, }; - static constexpr const EaxFxSlotIds eax5_fx_slot_ids = { + static constexpr const EaxFxSlotIds eax5_fx_slot_ids{ &EAXPROPERTYID_EAX50_FXSlot0, &EAXPROPERTYID_EAX50_FXSlot1, &EAXPROPERTYID_EAX50_FXSlot2, @@ -839,11 +839,10 @@ private: float path_ratio, float lf_ratio) noexcept; - EaxAlLowPassParam eax_create_direct_filter_param() const noexcept; + [[nodiscard]] auto eax_create_direct_filter_param() const noexcept -> EaxAlLowPassParam; - EaxAlLowPassParam eax_create_room_filter_param( - const ALeffectslot& fx_slot, - const EAXSOURCEALLSENDPROPERTIES& send) const noexcept; + [[nodiscard]] auto eax_create_room_filter_param(const ALeffectslot& fx_slot, + const EAXSOURCEALLSENDPROPERTIES& send) const noexcept -> EaxAlLowPassParam; void eax_update_direct_filter(); void eax_update_room_filters(); diff --git a/alc/alc.cpp b/alc/alc.cpp index 1ceae5ee..62f798f2 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -1008,8 +1008,8 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) std::optional opttype; std::optional optlayout; std::optional optscale; - uint period_size{DEFAULT_UPDATE_SIZE}; - uint buffer_size{DEFAULT_UPDATE_SIZE * DEFAULT_NUM_UPDATES}; + uint period_size{DefaultUpdateSize}; + uint buffer_size{DefaultUpdateSize * DefaultNumUpdates}; int hrtf_id{-1}; uint aorder{0u}; @@ -1019,9 +1019,9 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) if(auto freqopt = device->configValue(nullptr, "frequency")) { - optsrate = clampu(*freqopt, MIN_OUTPUT_RATE, MAX_OUTPUT_RATE); + optsrate = clampu(*freqopt, MinOutputRate, MaxOutputRate); - const double scale{static_cast(*optsrate) / DEFAULT_OUTPUT_RATE}; + const double scale{static_cast(*optsrate) / double{DefaultOutputRate}}; period_size = static_cast(period_size*scale + 0.5); } @@ -1030,7 +1030,7 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) if(auto numperopt = device->configValue(nullptr, "periods")) buffer_size = clampu(*numperopt, 2, 16) * period_size; else - buffer_size = period_size * DEFAULT_NUM_UPDATES; + buffer_size = period_size * uint{DefaultNumUpdates}; if(auto typeopt = device->configValue(nullptr, "sample-type")) { @@ -1201,7 +1201,7 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) case ATTRIBUTE(ALC_MAX_AUXILIARY_SENDS) numSends = static_cast(attrList[attrIdx + 1]); if(numSends > INT_MAX) numSends = 0; - else numSends = minu(numSends, MAX_SENDS); + else numSends = minu(numSends, MaxSendCount); break; case ATTRIBUTE(ALC_HRTF_SOFT) @@ -1244,7 +1244,7 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) { if(!optchans || !opttype) return ALC_INVALID_VALUE; - if(freqAttr < MIN_OUTPUT_RATE || freqAttr > MAX_OUTPUT_RATE) + if(freqAttr < int{MinOutputRate} || freqAttr > int{MaxOutputRate}) return ALC_INVALID_VALUE; if(*optchans == DevFmtAmbi3D) { @@ -1321,8 +1321,8 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) if(freqAttr) { - uint oldrate = optsrate.value_or(DEFAULT_OUTPUT_RATE); - freqAttr = clampi(freqAttr, MIN_OUTPUT_RATE, MAX_OUTPUT_RATE); + uint oldrate = optsrate.value_or(DefaultOutputRate); + freqAttr = clampi(freqAttr, MinOutputRate, MaxOutputRate); const double scale{static_cast(freqAttr) / oldrate}; period_size = static_cast(period_size*scale + 0.5); @@ -1397,7 +1397,7 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) device->mAmbiOrder = 0; device->BufferSize = buffer_size; device->UpdateSize = period_size; - device->Frequency = optsrate.value_or(DEFAULT_OUTPUT_RATE); + device->Frequency = optsrate.value_or(DefaultOutputRate); device->Flags.set(FrequencyRequest, optsrate.has_value()) .set(ChannelsRequest, optchans.has_value()) .set(SampleTypeRequest, opttype.has_value()); @@ -1500,7 +1500,7 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) device->NumStereoSources = numStereo; if(auto sendsopt = device->configValue(nullptr, "sends")) - numSends = minu(numSends, static_cast(clampi(*sendsopt, 0, MAX_SENDS))); + numSends = minu(numSends, static_cast(clampi(*sendsopt, 0, MaxSendCount))); device->NumAuxSends = numSends; TRACE("Max sources: %d (%d + %d), effect slots: %d, sends: %d\n", @@ -1781,7 +1781,7 @@ bool ResetDeviceParams(ALCdevice *device, const int *attrList) if(!device->Connected.load(std::memory_order_relaxed)) UNLIKELY { /* Make sure disconnection is finished before continuing on. */ - device->waitForMix(); + std::ignore = device->waitForMix(); for(ContextBase *ctxbase : *device->mContexts.load(std::memory_order_acquire)) { @@ -2101,7 +2101,7 @@ static size_t GetIntegerv(ALCdevice *device, ALCenum param, const al::span values[0] = alcEFXMinorVersion; return 1; case ALC_MAX_AUXILIARY_SENDS: - values[0] = MAX_SENDS; + values[0] = MaxSendCount; return 1; case ALC_ATTRIBUTES_SIZE: @@ -2720,7 +2720,7 @@ ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCin dev->mContexts.store(newarray.release()); if(oldarray != &DeviceBase::sEmptyContextArray) { - dev->waitForMix(); + std::ignore = dev->waitForMix(); delete oldarray; } } @@ -2892,7 +2892,7 @@ ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName) noexcep #ifdef ALSOFT_EAX eax_g_is_enabled ? uint{EAX_MAX_FXSLOTS} : #endif // ALSOFT_EAX - DEFAULT_SENDS + uint{DefaultSendCount} }; DeviceRef device{new ALCdevice{DeviceType::Playback}}; @@ -2900,9 +2900,9 @@ ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName) noexcep /* Set output format */ device->FmtChans = DevFmtChannelsDefault; device->FmtType = DevFmtTypeDefault; - device->Frequency = DEFAULT_OUTPUT_RATE; - device->UpdateSize = DEFAULT_UPDATE_SIZE; - device->BufferSize = DEFAULT_UPDATE_SIZE * DEFAULT_NUM_UPDATES; + device->Frequency = DefaultOutputRate; + device->UpdateSize = DefaultUpdateSize; + device->BufferSize = DefaultUpdateSize * DefaultNumUpdates; device->SourcesMax = 256; device->NumStereoSources = 1; @@ -3199,7 +3199,7 @@ ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceN #ifdef ALSOFT_EAX eax_g_is_enabled ? uint{EAX_MAX_FXSLOTS} : #endif // ALSOFT_EAX - DEFAULT_SENDS + uint{DefaultSendCount} }; DeviceRef device{new ALCdevice{DeviceType::Loopback}}; @@ -3212,7 +3212,7 @@ ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceN device->BufferSize = 0; device->UpdateSize = 0; - device->Frequency = DEFAULT_OUTPUT_RATE; + device->Frequency = DefaultOutputRate; device->FmtChans = DevFmtChannelsDefault; device->FmtType = DevFmtTypeDefault; @@ -3255,7 +3255,7 @@ ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device else { if(DevFmtTypeFromEnum(type).has_value() && DevFmtChannelsFromEnum(channels).has_value() - && freq >= MIN_OUTPUT_RATE && freq <= MAX_OUTPUT_RATE) + && freq >= int{MinOutputRate} && freq <= int{MaxOutputRate}) return ALC_TRUE; } diff --git a/alc/alu.cpp b/alc/alu.cpp index fe47f9be..0a5dabc9 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -282,7 +282,7 @@ void DeviceBase::ProcessHrtf(const size_t SamplesToDo) const size_t lidx{RealOut.ChannelIndex[FrontLeft]}; const size_t ridx{RealOut.ChannelIndex[FrontRight]}; - MixDirectHrtf(RealOut.Buffer[lidx], RealOut.Buffer[ridx], Dry.Buffer, HrtfAccumData, + MixDirectHrtf(RealOut.Buffer[lidx], RealOut.Buffer[ridx], Dry.Buffer, HrtfAccumData.data(), mHrtfState->mTemp.data(), mHrtfState->mChannels.data(), mHrtfState->mIrSize, SamplesToDo); } @@ -776,8 +776,8 @@ struct GainTriplet { float Base, HF, LF; }; void CalcPanningAndFilters(Voice *voice, const float xpos, const float ypos, const float zpos, const float Distance, const float Spread, const GainTriplet &DryGain, - const al::span WetGain, - const al::span SendSlots, const VoiceProps *props, + const al::span WetGain, + const al::span SendSlots, const VoiceProps *props, const ContextParams &Context, DeviceBase *Device) { static constexpr ChanPosMap MonoMap[1]{ @@ -1397,7 +1397,7 @@ void CalcPanningAndFilters(Voice *voice, const float xpos, const float ypos, con void CalcNonAttnSourceParams(Voice *voice, const VoiceProps *props, const ContextBase *context) { DeviceBase *Device{context->mDevice}; - EffectSlot *SendSlots[MAX_SENDS]; + std::array SendSlots; voice->mDirect.Buffer = Device->Dry.Buffer; for(uint i{0};i < Device->NumAuxSends;i++) @@ -1427,7 +1427,8 @@ void CalcNonAttnSourceParams(Voice *voice, const VoiceProps *props, const Contex context->mParams.Gain, GainMixMax); DryGain.HF = props->Direct.GainHF; DryGain.LF = props->Direct.GainLF; - GainTriplet WetGain[MAX_SENDS]; + + std::array WetGain; for(uint i{0};i < Device->NumAuxSends;i++) { WetGain[i].Base = minf(clampf(props->Gain, props->MinGain, props->MaxGain) * @@ -1447,9 +1448,9 @@ void CalcAttnSourceParams(Voice *voice, const VoiceProps *props, const ContextBa /* Set mixing buffers and get send parameters. */ voice->mDirect.Buffer = Device->Dry.Buffer; - std::array SendSlots{}; - std::array RoomRolloff{}; - std::bitset UseDryAttnForRoom{0}; + std::array SendSlots{}; + std::array RoomRolloff{}; + std::bitset UseDryAttnForRoom{0}; for(uint i{0};i < NumSends;i++) { SendSlots[i] = props->Send[i].Slot; @@ -1502,7 +1503,7 @@ void CalcAttnSourceParams(Voice *voice, const VoiceProps *props, const ContextBa /* Calculate distance attenuation */ float ClampedDist{Distance}; float DryGainBase{props->Gain}; - std::array WetGainBase{}; + std::array WetGainBase{}; WetGainBase.fill(props->Gain); float DryAttnBase{1.0f}; @@ -1605,7 +1606,7 @@ void CalcAttnSourceParams(Voice *voice, const VoiceProps *props, const ContextBa DryGain.HF = ConeHF * props->Direct.GainHF; DryGain.LF = props->Direct.GainLF; - std::array WetGain{}; + std::array WetGain{}; for(uint i{0};i < NumSends;i++) { WetGainBase[i] = clampf(WetGainBase[i]*WetCone, props->MinGain, props->MaxGain) * diff --git a/alc/backends/base.h b/alc/backends/base.h index 70f96275..ecca6b2e 100644 --- a/alc/backends/base.h +++ b/alc/backends/base.h @@ -99,7 +99,7 @@ public: backend_exception(backend_error code, const char *msg, ...); ~backend_exception() override; - backend_error errorCode() const noexcept { return mErrorCode; } + [[nodiscard]] auto errorCode() const noexcept -> backend_error { return mErrorCode; } }; } // namespace al diff --git a/alc/backends/jack.cpp b/alc/backends/jack.cpp index 4999a738..ca862276 100644 --- a/alc/backends/jack.cpp +++ b/alc/backends/jack.cpp @@ -307,7 +307,7 @@ struct JackPlayback final : public BackendBase { std::string mPortPattern; jack_client_t *mClient{nullptr}; - std::array mPort{}; + std::array mPort{}; std::mutex mMutex; @@ -339,7 +339,7 @@ JackPlayback::~JackPlayback() int JackPlayback::processRt(jack_nframes_t numframes) noexcept { - std::array out; + std::array out; size_t numchans{0}; for(auto port : mPort) { @@ -363,7 +363,7 @@ int JackPlayback::processRt(jack_nframes_t numframes) noexcept int JackPlayback::process(jack_nframes_t numframes) noexcept { - std::array out; + std::array out; size_t numchans{0}; for(auto port : mPort) { diff --git a/alc/backends/opensl.cpp b/alc/backends/opensl.cpp index 32745edd..f49b2ef8 100644 --- a/alc/backends/opensl.cpp +++ b/alc/backends/opensl.cpp @@ -439,7 +439,7 @@ bool OpenSLPlayback::reset() JCALL(env,ReleaseStringUTFChars)(srateStr, strchars); if(!sampleRate) sampleRate = device->Frequency; - else sampleRate = maxu(sampleRate, MIN_OUTPUT_RATE); + else sampleRate = maxu(sampleRate, MinOutputRate); } #endif diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index 96b6623f..ca7e3cf3 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -831,7 +831,7 @@ void DeviceNode::parseSampleRate(const spa_pod *value, bool force_update) noexce /* [0] is the default, [1] is the min, and [2] is the max. */ TRACE(" sample rate: %d (range: %d -> %d)\n", srates[0], srates[1], srates[2]); if(!mSampleRate || force_update) - mSampleRate = static_cast(clampi(srates[0], MIN_OUTPUT_RATE, MAX_OUTPUT_RATE)); + mSampleRate = static_cast(clampi(srates[0], MinOutputRate, MaxOutputRate)); return; } @@ -857,7 +857,7 @@ void DeviceNode::parseSampleRate(const spa_pod *value, bool force_update) noexce */ for(const auto &rate : srates) { - if(rate >= MIN_OUTPUT_RATE && rate <= MAX_OUTPUT_RATE) + if(rate >= int{MinOutputRate} && rate <= int{MaxOutputRate}) { if(!mSampleRate || force_update) mSampleRate = static_cast(rate); @@ -878,7 +878,7 @@ void DeviceNode::parseSampleRate(const spa_pod *value, bool force_update) noexce TRACE(" sample rate: %d\n", srates[0]); if(!mSampleRate || force_update) - mSampleRate = static_cast(clampi(srates[0], MIN_OUTPUT_RATE, MAX_OUTPUT_RATE)); + mSampleRate = static_cast(clampi(srates[0], MinOutputRate, MaxOutputRate)); return; } diff --git a/alc/context.cpp b/alc/context.cpp index ffc2743e..4e962469 100644 --- a/alc/context.cpp +++ b/alc/context.cpp @@ -276,7 +276,7 @@ bool ALCcontext::deinit() mDevice->mContexts.store(newarray); if(oldarray != &DeviceBase::sEmptyContextArray) { - mDevice->waitForMix(); + std::ignore = mDevice->waitForMix(); delete oldarray; } diff --git a/alc/effects/convolution.cpp b/alc/effects/convolution.cpp index 517e6b08..8db7a045 100644 --- a/alc/effects/convolution.cpp +++ b/alc/effects/convolution.cpp @@ -218,8 +218,8 @@ struct ConvolutionState final : public EffectState { alignas(16) FloatBufferLine mBuffer{}; float mHfScale{}, mLfScale{}; BandSplitter mFilter{}; - float Current[MAX_OUTPUT_CHANNELS]{}; - float Target[MAX_OUTPUT_CHANNELS]{}; + std::array Current{}; + std::array Target{}; }; std::vector mChans; al::vector mComplexData; @@ -246,8 +246,8 @@ void ConvolutionState::NormalMix(const al::span samplesOut, const size_t samplesToDo) { for(auto &chan : mChans) - MixSamples({chan.mBuffer.data(), samplesToDo}, samplesOut, chan.Current, chan.Target, - samplesToDo, 0); + MixSamples({chan.mBuffer.data(), samplesToDo}, samplesOut, chan.Current.data(), + chan.Target.data(), samplesToDo, 0); } void ConvolutionState::UpsampleMix(const al::span samplesOut, @@ -257,7 +257,7 @@ void ConvolutionState::UpsampleMix(const al::span samplesOut, { const al::span src{chan.mBuffer.data(), samplesToDo}; chan.mFilter.processScale(src, chan.mHfScale, chan.mLfScale); - MixSamples(src, samplesOut, chan.Current, chan.Target, samplesToDo, 0); + MixSamples(src, samplesOut, chan.Current.data(), chan.Target.data(), samplesToDo, 0); } } diff --git a/alc/effects/dedicated.cpp b/alc/effects/dedicated.cpp index a9131bfa..69e70847 100644 --- a/alc/effects/dedicated.cpp +++ b/alc/effects/dedicated.cpp @@ -47,8 +47,8 @@ struct DedicatedState final : public EffectState { * gains for all possible output channels and not just the main ambisonic * buffer. */ - float mCurrentGains[MAX_OUTPUT_CHANNELS]; - float mTargetGains[MAX_OUTPUT_CHANNELS]; + std::array mCurrentGains; + std::array mTargetGains; void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override; @@ -104,8 +104,8 @@ void DedicatedState::update(const ContextBase*, const EffectSlot *slot, void DedicatedState::process(const size_t samplesToDo, const al::span samplesIn, const al::span samplesOut) { - MixSamples({samplesIn[0].data(), samplesToDo}, samplesOut, mCurrentGains, mTargetGains, - samplesToDo, 0); + MixSamples({samplesIn[0].data(), samplesToDo}, samplesOut, mCurrentGains.data(), + mTargetGains.data(), samplesToDo, 0); } diff --git a/alc/panning.cpp b/alc/panning.cpp index b512a42a..93ebee73 100644 --- a/alc/panning.cpp +++ b/alc/panning.cpp @@ -249,7 +249,7 @@ void InitNearFieldCtrl(ALCdevice *device, float ctrl_dist, uint order, bool is3d } void InitDistanceComp(ALCdevice *device, const al::span channels, - const al::span dists) + const al::span dists) { const float maxdist{std::accumulate(std::begin(dists), std::end(dists), 0.0f, maxf)}; @@ -329,7 +329,7 @@ constexpr auto GetAmbiLayout(DevAmbiLayout layouttype) noexcept DecoderView MakeDecoderView(ALCdevice *device, const AmbDecConf *conf, - DecoderConfig &decoder) + DecoderConfig &decoder) { DecoderView ret{}; @@ -969,9 +969,9 @@ void aluInitRenderer(ALCdevice *device, int hrtf_id, std::optional> decoder_store; + std::unique_ptr> decoder_store; DecoderView decoder{}; - float speakerdists[MAX_OUTPUT_CHANNELS]{}; + std::array speakerdists{}; auto load_config = [device,&decoder_store,&decoder,&speakerdists](const char *config) { AmbDecConf conf{}; @@ -981,10 +981,10 @@ void aluInitRenderer(ALCdevice *device, int hrtf_id, std::optionalc_str()); return false; } - else if(conf.NumSpeakers > MAX_OUTPUT_CHANNELS) + else if(conf.NumSpeakers > MaxOutputChannels) { - ERR("Unsupported decoder speaker count %zu (max %d)\n", conf.NumSpeakers, - MAX_OUTPUT_CHANNELS); + ERR("Unsupported decoder speaker count %zu (max %zu)\n", conf.NumSpeakers, + MaxOutputChannels); return false; } else if(conf.ChanMask > Ambi3OrderMask) @@ -998,7 +998,7 @@ void aluInitRenderer(ALCdevice *device, int hrtf_id, std::optionalmXOverFreq = clampf(conf.XOverFreq, 100.0f, 1000.0f); - decoder_store = std::make_unique>(); + decoder_store = std::make_unique>(); decoder = MakeDecoderView(device, &conf, *decoder_store); for(size_t i{0};i < decoder.mChannels.size();++i) speakerdists[i] = conf.Speakers[i].Distance; diff --git a/common/ringbuffer.cpp b/common/ringbuffer.cpp index af1f3669..0d3b7e30 100644 --- a/common/ringbuffer.cpp +++ b/common/ringbuffer.cpp @@ -24,7 +24,9 @@ #include #include +#include #include +#include #include "almalloc.h" @@ -40,7 +42,7 @@ RingBufferPtr RingBuffer::Create(std::size_t sz, std::size_t elem_sz, int limit_ power_of_two |= power_of_two>>4; power_of_two |= power_of_two>>8; power_of_two |= power_of_two>>16; - if constexpr(SIZE_MAX > UINT_MAX) + if constexpr(std::numeric_limits::max() > std::numeric_limits::max()) power_of_two |= power_of_two>>32; } ++power_of_two; diff --git a/core/bformatdec.cpp b/core/bformatdec.cpp index a308e185..d6a44799 100644 --- a/core/bformatdec.cpp +++ b/core/bformatdec.cpp @@ -36,7 +36,7 @@ BFormatDec::BFormatDec(const size_t inchans, const al::span co auto &decoder = mChannelDec.emplace>(inchans); for(size_t j{0};j < decoder.size();++j) { - float *outcoeffs{decoder[j].mGains}; + float *outcoeffs{decoder[j].mGains.data()}; for(const ChannelDec &incoeffs : coeffs) *(outcoeffs++) = incoeffs[j]; } @@ -50,11 +50,11 @@ BFormatDec::BFormatDec(const size_t inchans, const al::span co for(size_t j{0};j < decoder.size();++j) { - float *outcoeffs{decoder[j].mGains[sHFBand]}; + float *outcoeffs{decoder[j].mGains[sHFBand].data()}; for(const ChannelDec &incoeffs : coeffs) *(outcoeffs++) = incoeffs[j]; - outcoeffs = decoder[j].mGains[sLFBand]; + outcoeffs = decoder[j].mGains[sLFBand].data(); for(const ChannelDec &incoeffs : coeffslf) *(outcoeffs++) = incoeffs[j]; } @@ -76,8 +76,10 @@ void BFormatDec::process(const al::span OutBuffer, { chandec.mXOver.process({input->data(), SamplesToDo}, hfSamples.data(), lfSamples.data()); - MixSamples(hfSamples, OutBuffer, chandec.mGains[sHFBand], chandec.mGains[sHFBand],0,0); - MixSamples(lfSamples, OutBuffer, chandec.mGains[sLFBand], chandec.mGains[sLFBand],0,0); + MixSamples(hfSamples, OutBuffer, chandec.mGains[sHFBand].data(), + chandec.mGains[sHFBand].data(), 0, 0); + MixSamples(lfSamples, OutBuffer, chandec.mGains[sLFBand].data(), + chandec.mGains[sLFBand].data(), 0, 0); ++input; } }; @@ -86,8 +88,8 @@ void BFormatDec::process(const al::span OutBuffer, auto *input = InSamples; for(auto &chandec : decoder) { - MixSamples({input->data(), SamplesToDo}, OutBuffer, chandec.mGains, chandec.mGains, - 0, 0); + MixSamples({input->data(), SamplesToDo}, OutBuffer, chandec.mGains.data(), + chandec.mGains.data(), 0, 0); ++input; } }; diff --git a/core/bformatdec.h b/core/bformatdec.h index 3bb7f544..97e7c9e4 100644 --- a/core/bformatdec.h +++ b/core/bformatdec.h @@ -25,12 +25,12 @@ class BFormatDec { static constexpr size_t sNumBands{2}; struct ChannelDecoderSingle { - float mGains[MAX_OUTPUT_CHANNELS]; + std::array mGains; }; struct ChannelDecoderDual { BandSplitter mXOver; - float mGains[sNumBands][MAX_OUTPUT_CHANNELS]; + std::array,sNumBands> mGains; }; alignas(16) std::array mSamples; @@ -44,7 +44,7 @@ public: const al::span coeffslf, const float xover_f0norm, std::unique_ptr stablizer); - bool hasStablizer() const noexcept { return mStablizer != nullptr; } + [[nodiscard]] auto hasStablizer() const noexcept -> bool { return mStablizer != nullptr; } /* Decodes the ambisonic input to the given output channels. */ void process(const al::span OutBuffer, const FloatBufferLine *InSamples, diff --git a/core/buffer_storage.h b/core/buffer_storage.h index 3b581b5e..dec774bf 100644 --- a/core/buffer_storage.h +++ b/core/buffer_storage.h @@ -98,19 +98,19 @@ struct BufferStorage { AmbiScaling mAmbiScaling{AmbiScaling::FuMa}; uint mAmbiOrder{0u}; - inline uint bytesFromFmt() const noexcept { return BytesFromFmt(mType); } - inline uint channelsFromFmt() const noexcept + [[nodiscard]] auto bytesFromFmt() const noexcept -> uint { return BytesFromFmt(mType); } + [[nodiscard]] auto channelsFromFmt() const noexcept -> uint { return ChannelsFromFmt(mChannels, mAmbiOrder); } - inline uint frameSizeFromFmt() const noexcept { return channelsFromFmt() * bytesFromFmt(); } + [[nodiscard]] auto frameSizeFromFmt() const noexcept -> uint { return channelsFromFmt() * bytesFromFmt(); } - inline uint blockSizeFromFmt() const noexcept + [[nodiscard]] auto blockSizeFromFmt() const noexcept -> uint { if(mType == FmtIMA4) return ((mBlockAlign-1)/2 + 4) * channelsFromFmt(); if(mType == FmtMSADPCM) return ((mBlockAlign-2)/2 + 7) * channelsFromFmt(); return frameSizeFromFmt(); }; - inline bool isBFormat() const noexcept { return IsBFormat(mChannels); } + [[nodiscard]] auto isBFormat() const noexcept -> bool { return IsBFormat(mChannels); } }; #endif /* CORE_BUFFER_STORAGE_H */ diff --git a/core/context.cpp b/core/context.cpp index 2ebbc7b1..bd7bb006 100644 --- a/core/context.cpp +++ b/core/context.cpp @@ -142,7 +142,7 @@ void ContextBase::allocVoices(size_t addcount) if(auto *oldvoices = mVoices.exchange(newarray.release(), std::memory_order_acq_rel)) { - mDevice->waitForMix(); + std::ignore = mDevice->waitForMix(); delete oldvoices; } } diff --git a/core/context.h b/core/context.h index 6f65663f..fded0902 100644 --- a/core/context.h +++ b/core/context.h @@ -117,12 +117,12 @@ struct ContextBase { std::atomic mActiveVoiceCount{}; void allocVoices(size_t addcount); - al::span getVoicesSpan() const noexcept + [[nodiscard]] auto getVoicesSpan() const noexcept -> al::span { return {mVoices.load(std::memory_order_relaxed)->data(), mActiveVoiceCount.load(std::memory_order_relaxed)}; } - al::span getVoicesSpanAcquired() const noexcept + [[nodiscard]] auto getVoicesSpanAcquired() const noexcept -> al::span { return {mVoices.load(std::memory_order_acquire)->data(), mActiveVoiceCount.load(std::memory_order_acquire)}; diff --git a/core/devformat.h b/core/devformat.h index 485826a3..d918e531 100644 --- a/core/devformat.h +++ b/core/devformat.h @@ -2,6 +2,7 @@ #define CORE_DEVFORMAT_H #include +#include using uint = unsigned int; @@ -71,7 +72,7 @@ enum DevFmtChannels : unsigned char { DevFmtChannelsDefault = DevFmtStereo }; -#define MAX_OUTPUT_CHANNELS 16 +inline constexpr size_t MaxOutputChannels{16}; /* DevFmtType traits, providing the type, etc given a DevFmtType. */ template diff --git a/core/device.h b/core/device.h index 1ac01ba6..8cc15310 100644 --- a/core/device.h +++ b/core/device.h @@ -34,12 +34,12 @@ struct HrtfStore; using uint = unsigned int; -#define MIN_OUTPUT_RATE 8000 -#define MAX_OUTPUT_RATE 192000 -#define DEFAULT_OUTPUT_RATE 48000 +inline constexpr size_t MinOutputRate{8000}; +inline constexpr size_t MaxOutputRate{192000}; +inline constexpr size_t DefaultOutputRate{48000}; -#define DEFAULT_UPDATE_SIZE 960 /* 20ms */ -#define DEFAULT_NUM_UPDATES 3 +inline constexpr size_t DefaultUpdateSize{960}; /* 20ms */ +inline constexpr size_t DefaultNumUpdates{3}; enum class DeviceType : uint8_t { @@ -82,7 +82,7 @@ struct DistanceComp { float *Buffer{nullptr}; }; - std::array mChannels; + std::array mChannels; al::FlexArray mSamples; DistanceComp(size_t count) : mSamples{count} { } @@ -232,21 +232,21 @@ struct DeviceBase { alignas(16) std::array mSampleData; alignas(16) std::array mResampleData; - alignas(16) float FilteredData[BufferLineSize]; + alignas(16) std::array FilteredData; union { - alignas(16) float HrtfSourceData[BufferLineSize + HrtfHistoryLength]; - alignas(16) float NfcSampleData[BufferLineSize]; + alignas(16) std::array HrtfSourceData; + alignas(16) std::array NfcSampleData; }; /* Persistent storage for HRTF mixing. */ - alignas(16) float2 HrtfAccumData[BufferLineSize + HrirLength]; + alignas(16) std::array HrtfAccumData; /* Mixing buffer used by the Dry mix and Real output. */ al::vector MixBuffer; /* The "dry" path corresponds to the main output. */ MixParams Dry; - uint NumChannelsPerOrder[MaxAmbiOrder+1]{}; + std::array NumChannelsPerOrder{}; /* "Real" output, which will be written to the device buffer. May alias the * dry buffer. @@ -295,9 +295,9 @@ struct DeviceBase { DeviceBase& operator=(const DeviceBase&) = delete; ~DeviceBase(); - uint bytesFromFmt() const noexcept { return BytesFromDevFmt(FmtType); } - uint channelsFromFmt() const noexcept { return ChannelsFromDevFmt(FmtChans, mAmbiOrder); } - uint frameSizeFromFmt() const noexcept { return bytesFromFmt() * channelsFromFmt(); } + [[nodiscard]] auto bytesFromFmt() const noexcept -> uint { return BytesFromDevFmt(FmtType); } + [[nodiscard]] auto channelsFromFmt() const noexcept -> uint { return ChannelsFromDevFmt(FmtChans, mAmbiOrder); } + [[nodiscard]] auto frameSizeFromFmt() const noexcept -> uint { return bytesFromFmt() * channelsFromFmt(); } struct MixLock { std::atomic &mCount; @@ -323,7 +323,7 @@ struct DeviceBase { } /** Waits for the mixer to not be mixing or updating the clock. */ - uint waitForMix() const noexcept + [[nodiscard]] auto waitForMix() const noexcept -> uint { uint refcount; while((refcount=mMixCount.load(std::memory_order_acquire))&1) { @@ -336,7 +336,7 @@ struct DeviceBase { * SamplesDone converted from the sample rate. Should only be called while * watching the MixCount. */ - std::chrono::nanoseconds getClockTime() const noexcept + [[nodiscard]] auto getClockTime() const noexcept -> std::chrono::nanoseconds { using std::chrono::seconds; using std::chrono::nanoseconds; @@ -369,7 +369,7 @@ struct DeviceBase { * Returns the index for the given channel name (e.g. FrontCenter), or * InvalidChannelIndex if it doesn't exist. */ - uint8_t channelIdxByName(Channel chan) const noexcept + [[nodiscard]] auto channelIdxByName(Channel chan) const noexcept -> uint8_t { return RealOut.ChannelIndex[chan]; } DISABLE_ALLOC() diff --git a/core/filters/biquad.h b/core/filters/biquad.h index 75a4009b..e176caae 100644 --- a/core/filters/biquad.h +++ b/core/filters/biquad.h @@ -119,9 +119,9 @@ public: void dualProcess(BiquadFilterR &other, const al::span src, Real *dst); /* Rather hacky. It's just here to support "manual" processing. */ - std::pair getComponents() const noexcept { return {mZ1, mZ2}; } + [[nodiscard]] auto getComponents() const noexcept -> std::pair { return {mZ1, mZ2}; } void setComponents(Real z1, Real z2) noexcept { mZ1 = z1; mZ2 = z2; } - Real processOne(const Real in, Real &z1, Real &z2) const noexcept + [[nodiscard]] auto processOne(const Real in, Real &z1, Real &z2) const noexcept -> Real { const Real out{in*mB0 + z1}; z1 = in*mB1 - out*mA1 + z2; diff --git a/core/filters/nfc.h b/core/filters/nfc.h index 4b8e68b5..7d0a7488 100644 --- a/core/filters/nfc.h +++ b/core/filters/nfc.h @@ -1,6 +1,7 @@ #ifndef CORE_FILTERS_NFC_H #define CORE_FILTERS_NFC_H +#include #include #include "alspan.h" @@ -9,22 +10,22 @@ struct NfcFilter1 { float base_gain, gain; float b1, a1; - float z[1]; + std::array z; }; struct NfcFilter2 { float base_gain, gain; float b1, b2, a1, a2; - float z[2]; + std::array z; }; struct NfcFilter3 { float base_gain, gain; float b1, b2, b3, a1, a2, a3; - float z[3]; + std::array z; }; struct NfcFilter4 { float base_gain, gain; float b1, b2, b3, b4, a1, a2, a3, a4; - float z[4]; + std::array z; }; class NfcFilter { diff --git a/core/fmt_traits.cpp b/core/fmt_traits.cpp index 054d8766..9d79287d 100644 --- a/core/fmt_traits.cpp +++ b/core/fmt_traits.cpp @@ -6,7 +6,7 @@ namespace al { -const int16_t muLawDecompressionTable[256] = { +const std::array muLawDecompressionTable{{ -32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956, -23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764, -15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412, @@ -39,9 +39,9 @@ const int16_t muLawDecompressionTable[256] = { 244, 228, 212, 196, 180, 164, 148, 132, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0 -}; +}}; -const int16_t aLawDecompressionTable[256] = { +const std::array aLawDecompressionTable{{ -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, @@ -74,6 +74,6 @@ const int16_t aLawDecompressionTable[256] = { 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848 -}; +}}; } // namespace al diff --git a/core/fmt_traits.h b/core/fmt_traits.h index 02473014..101e20b6 100644 --- a/core/fmt_traits.h +++ b/core/fmt_traits.h @@ -1,6 +1,7 @@ #ifndef CORE_FMT_TRAITS_H #define CORE_FMT_TRAITS_H +#include #include #include @@ -9,8 +10,8 @@ namespace al { -extern const int16_t muLawDecompressionTable[256]; -extern const int16_t aLawDecompressionTable[256]; +extern const std::array muLawDecompressionTable; +extern const std::array aLawDecompressionTable; template diff --git a/core/mixer/defs.h b/core/mixer/defs.h index 48daca9b..6e68978c 100644 --- a/core/mixer/defs.h +++ b/core/mixer/defs.h @@ -94,7 +94,8 @@ void MixDirectHrtf_(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOu /* Vectorized resampler helpers */ template -inline void InitPosArrays(uint frac, uint increment, uint (&frac_arr)[N], uint (&pos_arr)[N]) +inline void InitPosArrays(uint frac, uint increment, const al::span frac_arr, + const al::span pos_arr) { pos_arr[0] = 0; frac_arr[0] = frac; diff --git a/core/mixer/mixer_neon.cpp b/core/mixer/mixer_neon.cpp index ead775af..f838b20d 100644 --- a/core/mixer/mixer_neon.cpp +++ b/core/mixer/mixer_neon.cpp @@ -149,7 +149,7 @@ void Resample_(const InterpState*, const float *RESTRICT src, u alignas(16) uint pos_[4], frac_[4]; int32x4_t pos4, frac4; - InitPosArrays(frac, increment, frac_, pos_); + InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_}); frac4 = vld1q_s32(reinterpret_cast(frac_)); pos4 = vld1q_s32(reinterpret_cast(pos_)); diff --git a/core/mixer/mixer_sse2.cpp b/core/mixer/mixer_sse2.cpp index edaaf7a1..aa99250e 100644 --- a/core/mixer/mixer_sse2.cpp +++ b/core/mixer/mixer_sse2.cpp @@ -45,7 +45,7 @@ void Resample_(const InterpState*, const float *RESTRICT src, u const __m128i fracMask4{_mm_set1_epi32(MixerFracMask)}; alignas(16) uint pos_[4], frac_[4]; - InitPosArrays(frac, increment, frac_, pos_); + InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_}); __m128i frac4{_mm_setr_epi32(static_cast(frac_[0]), static_cast(frac_[1]), static_cast(frac_[2]), static_cast(frac_[3]))}; __m128i pos4{_mm_setr_epi32(static_cast(pos_[0]), static_cast(pos_[1]), diff --git a/core/mixer/mixer_sse41.cpp b/core/mixer/mixer_sse41.cpp index 8ccd9fd3..4e4605df 100644 --- a/core/mixer/mixer_sse41.cpp +++ b/core/mixer/mixer_sse41.cpp @@ -46,7 +46,7 @@ void Resample_(const InterpState*, const float *RESTRICT src, u const __m128i fracMask4{_mm_set1_epi32(MixerFracMask)}; alignas(16) uint pos_[4], frac_[4]; - InitPosArrays(frac, increment, frac_, pos_); + InitPosArrays(frac, increment, al::span{frac_}, al::span{pos_}); __m128i frac4{_mm_setr_epi32(static_cast(frac_[0]), static_cast(frac_[1]), static_cast(frac_[2]), static_cast(frac_[3]))}; __m128i pos4{_mm_setr_epi32(static_cast(pos_[0]), static_cast(pos_[1]), diff --git a/core/uhjfilter.h b/core/uhjfilter.h index 348dc7e1..29838410 100644 --- a/core/uhjfilter.h +++ b/core/uhjfilter.h @@ -25,7 +25,7 @@ extern UhjQualityType UhjEncodeQuality; struct UhjAllPassFilter { struct AllPassState { /* Last two delayed components for direct form II. */ - float z[2]; + std::array z; }; std::array mState; diff --git a/core/voice.cpp b/core/voice.cpp index 3889c42d..d2645b7f 100644 --- a/core/voice.cpp +++ b/core/voice.cpp @@ -9,11 +9,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include @@ -133,18 +133,18 @@ void Voice::InitMixer(std::optional resampler) if(resampler) { struct ResamplerEntry { - const char name[16]; + const char *name; const Resampler resampler; }; - constexpr ResamplerEntry ResamplerList[]{ - { "none", Resampler::Point }, - { "point", Resampler::Point }, - { "linear", Resampler::Linear }, - { "cubic", Resampler::Cubic }, - { "bsinc12", Resampler::BSinc12 }, - { "fast_bsinc12", Resampler::FastBSinc12 }, - { "bsinc24", Resampler::BSinc24 }, - { "fast_bsinc24", Resampler::FastBSinc24 }, + constexpr std::array ResamplerList{ + ResamplerEntry{"none", Resampler::Point}, + ResamplerEntry{"point", Resampler::Point}, + ResamplerEntry{"linear", Resampler::Linear}, + ResamplerEntry{"cubic", Resampler::Cubic}, + ResamplerEntry{"bsinc12", Resampler::BSinc12}, + ResamplerEntry{"fast_bsinc12", Resampler::FastBSinc12}, + ResamplerEntry{"bsinc24", Resampler::BSinc24}, + ResamplerEntry{"fast_bsinc24", Resampler::FastBSinc24}, }; const char *str{resampler->c_str()}; @@ -159,10 +159,10 @@ void Voice::InitMixer(std::optional resampler) str = "cubic"; } - auto iter = std::find_if(std::begin(ResamplerList), std::end(ResamplerList), + auto iter = std::find_if(ResamplerList.begin(), ResamplerList.end(), [str](const ResamplerEntry &entry) -> bool { return al::strcasecmp(str, entry.name) == 0; }); - if(iter == std::end(ResamplerList)) + if(iter == ResamplerList.end()) ERR("Invalid resampler: %s\n", str); else ResamplerDefault = iter->resampler; @@ -178,7 +178,7 @@ void Voice::InitMixer(std::optional resampler) namespace { /* IMA ADPCM Stepsize table */ -constexpr int IMAStep_size[89] = { +constexpr std::array IMAStep_size{{ 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, @@ -188,35 +188,35 @@ constexpr int IMAStep_size[89] = { 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493,10442, 11487,12635,13899,15289,16818,18500,20350,22358,24633,27086,29794, 32767 -}; +}}; /* IMA4 ADPCM Codeword decode table */ -constexpr int IMA4Codeword[16] = { +constexpr std::array IMA4Codeword{{ 1, 3, 5, 7, 9, 11, 13, 15, -1,-3,-5,-7,-9,-11,-13,-15, -}; +}}; /* IMA4 ADPCM Step index adjust decode table */ -constexpr int IMA4Index_adjust[16] = { +constexpr std::arrayIMA4Index_adjust{{ -1,-1,-1,-1, 2, 4, 6, 8, -1,-1,-1,-1, 2, 4, 6, 8 -}; +}}; /* MSADPCM Adaption table */ -constexpr int MSADPCMAdaption[16] = { +constexpr std::array MSADPCMAdaption{{ 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 -}; +}}; /* MSADPCM Adaption Coefficient tables */ -constexpr int MSADPCMAdaptionCoeff[7][2] = { - { 256, 0 }, - { 512, -256 }, - { 0, 0 }, - { 192, 64 }, - { 240, 0 }, - { 460, -208 }, - { 392, -232 } +constexpr std::array MSADPCMAdaptionCoeff{ + std::array{256, 0}, + std::array{512, -256}, + std::array{ 0, 0}, + std::array{192, 64}, + std::array{240, 0}, + std::array{460, -208}, + std::array{392, -232} }; @@ -307,7 +307,7 @@ inline void LoadSamples(float *RESTRICT dstSamples, const std::byte *sr auto decode_sample = [&sample,&index](const uint nibble) { - sample += IMA4Codeword[nibble] * IMAStep_size[index] / 8; + sample += IMA4Codeword[nibble] * IMAStep_size[static_cast(index)] / 8; sample = clampi(sample, -32768, 32767); index += IMA4Index_adjust[nibble]; @@ -382,7 +382,7 @@ inline void LoadSamples(float *RESTRICT dstSamples, const std::byte int delta{int(input[2*srcChan + 0]) | (int(input[2*srcChan + 1]) << 8)}; input += srcStep*2; - int sampleHistory[2]{}; + std::array sampleHistory{}; sampleHistory[0] = int(input[2*srcChan + 0]) | (int(input[2*srcChan + 1])<<8); input += srcStep*2; sampleHistory[1] = int(input[2*srcChan + 0]) | (int(input[2*srcChan + 1])<<8); @@ -421,7 +421,7 @@ inline void LoadSamples(float *RESTRICT dstSamples, const std::byte sampleHistory[1] = sampleHistory[0]; sampleHistory[0] = pred; - delta = (MSADPCMAdaption[nibble] * delta) / 256; + delta = (MSADPCMAdaption[static_cast(nibble)] * delta) / 256; delta = maxi(16, delta); return pred; @@ -630,8 +630,8 @@ void DoHrtfMix(const float *samples, const uint DstBufferSize, DirectParams &par parms.Hrtf.Target.Coeffs, parms.Hrtf.Target.Delay, 0.0f, gain / static_cast(fademix)}; - MixHrtfBlendSamples(HrtfSamples, AccumSamples+OutPos, IrSize, &parms.Hrtf.Old, &hrtfparams, - fademix); + MixHrtfBlendSamples(HrtfSamples.data(), AccumSamples.data()+OutPos, IrSize, + &parms.Hrtf.Old, &hrtfparams, fademix); /* Update the old parameters with the result. */ parms.Hrtf.Old = parms.Hrtf.Target; @@ -658,7 +658,8 @@ void DoHrtfMix(const float *samples, const uint DstBufferSize, DirectParams &par parms.Hrtf.Target.Delay, parms.Hrtf.Old.Gain, (gain - parms.Hrtf.Old.Gain) / static_cast(todo)}; - MixHrtfSamples(HrtfSamples+fademix, AccumSamples+OutPos, IrSize, &hrtfparams, todo); + MixHrtfSamples(HrtfSamples.data()+fademix, AccumSamples.data()+OutPos, IrSize, &hrtfparams, + todo); /* Store the now-current gain for next time. */ parms.Hrtf.Old.Gain = gain; @@ -669,8 +670,8 @@ void DoNfcMix(const al::span samples, FloatBufferLine *OutBuffer, D const float *TargetGains, const uint Counter, const uint OutPos, DeviceBase *Device) { using FilterProc = void (NfcFilter::*)(const al::span, float*); - static constexpr FilterProc NfcProcess[MaxAmbiOrder+1]{ - nullptr, &NfcFilter::process1, &NfcFilter::process2, &NfcFilter::process3}; + static constexpr std::array NfcProcess{{ + nullptr, &NfcFilter::process1, &NfcFilter::process2, &NfcFilter::process3}}; float *CurrentGains{parms.Gains.Current.data()}; MixSamples(samples, {OutBuffer, 1u}, CurrentGains, TargetGains, Counter, OutPos); @@ -678,7 +679,7 @@ void DoNfcMix(const al::span samples, FloatBufferLine *OutBuffer, D ++CurrentGains; ++TargetGains; - const al::span nfcsamples{Device->NfcSampleData, samples.size()}; + const al::span nfcsamples{Device->NfcSampleData.data(), samples.size()}; size_t order{1}; while(const size_t chancount{Device->NumChannelsPerOrder[order]}) { @@ -697,7 +698,7 @@ void DoNfcMix(const al::span samples, FloatBufferLine *OutBuffer, D void Voice::mix(const State vstate, ContextBase *Context, const nanoseconds deviceTime, const uint SamplesToDo) { - static constexpr std::array SilentTarget{}; + static constexpr std::array SilentTarget{}; ASSUME(SamplesToDo > 0); diff --git a/core/voice.h b/core/voice.h index a599eda8..6c953804 100644 --- a/core/voice.h +++ b/core/voice.h @@ -32,7 +32,7 @@ enum class DistanceModel : unsigned char; using uint = unsigned int; -#define MAX_SENDS 6 +inline constexpr size_t MaxSendCount{6}; enum class SpatializeMode : unsigned char { @@ -72,8 +72,8 @@ struct DirectParams { } Hrtf; struct { - std::array Current; - std::array Target; + std::array Current; + std::array Target; } Gains; }; @@ -154,7 +154,8 @@ struct VoiceProps { float HFReference; float GainLF; float LFReference; - } Send[MAX_SENDS]; + }; + std::array Send; }; struct VoicePropsItem : public VoiceProps { @@ -239,7 +240,7 @@ struct Voice { al::span Buffer; }; TargetData mDirect; - std::array mSend; + std::array mSend; /* The first MaxResamplerPadding/2 elements are the sample history from the * previous mix, with an additional MaxResamplerPadding/2 elements that are @@ -254,7 +255,7 @@ struct Voice { BandSplitter mAmbiSplitter; DirectParams mDryParams; - std::array mWetParams; + std::array mWetParams; }; al::vector mChans{2}; -- cgit v1.2.3 From cfb6bdfabfdf70ce5bcb650d356c3a9c1db3df0c Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 23 Dec 2023 04:18:07 -0800 Subject: Avoid assignments in if conditions --- al/source.cpp | 4 ++-- alc/alc.cpp | 37 ++++++++++++++++++++----------------- alc/backends/alsa.cpp | 44 +++++++++++++++++++++++++++----------------- alc/backends/pipewire.cpp | 7 +++++-- alc/backends/portaudio.cpp | 3 ++- alc/backends/pulseaudio.cpp | 4 ++-- examples/alstreamcb.cpp | 3 ++- utils/makemhr/makemhr.cpp | 4 ++-- 8 files changed, 62 insertions(+), 44 deletions(-) (limited to 'al/source.cpp') diff --git a/al/source.cpp b/al/source.cpp index c99943cf..f119ee36 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -3410,8 +3410,8 @@ FORCE_ALIGN void AL_APIENTRY alSourceQueueBuffersDirect(ALCcontext *context, ALu for(ALsizei i{0};i < nb;i++) { bool fmt_mismatch{false}; - ALbuffer *buffer{nullptr}; - if(buffers[i] && (buffer=LookupBuffer(device, buffers[i])) == nullptr) + ALbuffer *buffer{buffers[i] ? LookupBuffer(device, buffers[i]) : nullptr}; + if(buffers[i] && !buffer) { context->setError(AL_INVALID_NAME, "Queueing invalid buffer ID %u", buffers[i]); goto buffer_error; diff --git a/alc/alc.cpp b/alc/alc.cpp index 3ad07aaa..d944cf0c 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -562,7 +562,8 @@ void alc_initconfig() auto BackendListEnd = std::end(BackendList); auto devopt = al::getenv("ALSOFT_DRIVERS"); - if(devopt || (devopt=ConfigValueStr({}, {}, "drivers"))) + if(!devopt) devopt = ConfigValueStr({}, {}, "drivers"); + if(devopt) { auto backendlist_cur = std::begin(BackendList); @@ -670,8 +671,8 @@ void alc_initconfig() InitEffect(&ALCcontext::sDefaultEffect); auto defrevopt = al::getenv("ALSOFT_DEFAULT_REVERB"); - if(defrevopt || (defrevopt=ConfigValueStr({}, {}, "default-reverb"))) - LoadReverbPreset(defrevopt->c_str(), &ALCcontext::sDefaultEffect); + if(!defrevopt) defrevopt = ConfigValueStr({}, {}, "default-reverb"); + if(defrevopt) LoadReverbPreset(defrevopt->c_str(), &ALCcontext::sDefaultEffect); #ifdef ALSOFT_EAX { @@ -2556,23 +2557,25 @@ ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const A { DeviceRef dev{VerifyDevice(device)}; if(!extName) + { alcSetError(dev.get(), ALC_INVALID_VALUE); - else + return ALC_FALSE; + } + + const std::string_view tofind{extName}; + auto extlist = dev ? std::string_view{alcExtensionList} : std::string_view{alcNoDeviceExtList}; + while(!extlist.empty()) { - size_t len = strlen(extName); - const char *ptr = (dev ? alcExtensionList : alcNoDeviceExtList); - while(ptr && *ptr) - { - if(al::strncasecmp(ptr, extName, len) == 0 && (ptr[len] == '\0' || isspace(ptr[len]))) - return ALC_TRUE; + auto nextpos = extlist.find(' '); + auto tocheck = extlist.substr(0, nextpos); + if(tocheck.size() == tofind.size() + && al::strncasecmp(tofind.data(), tocheck.data(), tofind.size()) == 0) + return ALC_TRUE; - if((ptr=strchr(ptr, ' ')) != nullptr) - { - do { - ++ptr; - } while(isspace(*ptr)); - } - } + if(nextpos == std::string_view::npos) + break; + + extlist.remove_prefix(nextpos+1); } return ALC_FALSE; } diff --git a/alc/backends/alsa.cpp b/alc/backends/alsa.cpp index 9bbcb241..d7dd16c0 100644 --- a/alc/backends/alsa.cpp +++ b/alc/backends/alsa.cpp @@ -311,12 +311,14 @@ std::vector probe_devices(snd_pcm_stream_t stream) std::string name{"hw:" + std::to_string(card)}; snd_ctl_t *handle; - if((err=snd_ctl_open(&handle, name.c_str(), 0)) < 0) + err = snd_ctl_open(&handle, name.c_str(), 0); + if(err < 0) { ERR("control open (hw:%d): %s\n", card, snd_strerror(err)); continue; } - if((err=snd_ctl_card_info(handle, info)) < 0) + err = snd_ctl_card_info(handle, info); + if(err < 0) { ERR("control hardware info (hw:%d): %s\n", card, snd_strerror(err)); snd_ctl_close(handle); @@ -340,7 +342,8 @@ std::vector probe_devices(snd_pcm_stream_t stream) snd_pcm_info_set_device(pcminfo, static_cast(dev)); snd_pcm_info_set_subdevice(pcminfo, 0); snd_pcm_info_set_stream(pcminfo, stream); - if((err=snd_ctl_pcm_info(handle, pcminfo)) < 0) + err = snd_ctl_pcm_info(handle, pcminfo); + if(err < 0) { if(err != -ENOENT) ERR("control digital audio info (hw:%d): %s\n", card, snd_strerror(err)); @@ -405,13 +408,14 @@ int verify_state(snd_pcm_t *handle) break; case SND_PCM_STATE_XRUN: - if((err=snd_pcm_recover(handle, -EPIPE, 1)) < 0) - return err; + err=snd_pcm_recover(handle, -EPIPE, 1); + if(err < 0) return err; break; case SND_PCM_STATE_SUSPENDED: - if((err=snd_pcm_recover(handle, -ESTRPIPE, 1)) < 0) - return err; + err = snd_pcm_recover(handle, -ESTRPIPE, 1); + if(err < 0) return err; break; + case SND_PCM_STATE_DISCONNECTED: return -ENODEV; } @@ -698,7 +702,8 @@ bool AlsaPlayback::reset() int err{}; HwParamsPtr hp{CreateHwParams()}; #define CHECK(x) do { \ - if((err=(x)) < 0) \ + err = (x); \ + if(err < 0) \ throw al::backend_exception{al::backend_error::DeviceError, #x " failed: %s", \ snd_strerror(err)}; \ } while(0) @@ -759,11 +764,11 @@ bool AlsaPlayback::reset() WARN("Failed to enable ALSA resampler\n"); CHECK(snd_pcm_hw_params_set_rate_near(mPcmHandle, hp.get(), &rate, nullptr)); /* set period time (implicitly constrains period/buffer parameters) */ - if((err=snd_pcm_hw_params_set_period_time_near(mPcmHandle, hp.get(), &periodLen, nullptr)) < 0) - ERR("snd_pcm_hw_params_set_period_time_near failed: %s\n", snd_strerror(err)); + err = snd_pcm_hw_params_set_period_time_near(mPcmHandle, hp.get(), &periodLen, nullptr); + if(err < 0) ERR("snd_pcm_hw_params_set_period_time_near failed: %s\n", snd_strerror(err)); /* set buffer time (implicitly sets buffer size/bytes/time and period size/bytes) */ - if((err=snd_pcm_hw_params_set_buffer_time_near(mPcmHandle, hp.get(), &bufferLen, nullptr)) < 0) - ERR("snd_pcm_hw_params_set_buffer_time_near failed: %s\n", snd_strerror(err)); + err = snd_pcm_hw_params_set_buffer_time_near(mPcmHandle, hp.get(), &bufferLen, nullptr); + if(err < 0) ERR("snd_pcm_hw_params_set_buffer_time_near failed: %s\n", snd_strerror(err)); /* install and prepare hardware configuration */ CHECK(snd_pcm_hw_params(mPcmHandle, hp.get())); @@ -801,7 +806,8 @@ void AlsaPlayback::start() snd_pcm_access_t access{}; HwParamsPtr hp{CreateHwParams()}; #define CHECK(x) do { \ - if((err=(x)) < 0) \ + err = (x); \ + if(err < 0) \ throw al::backend_exception{al::backend_error::DeviceError, #x " failed: %s", \ snd_strerror(err)}; \ } while(0) @@ -958,7 +964,8 @@ void AlsaCapture::open(std::string_view name) bool needring{false}; HwParamsPtr hp{CreateHwParams()}; #define CHECK(x) do { \ - if((err=(x)) < 0) \ + err = (x); \ + if(err < 0) \ throw al::backend_exception{al::backend_error::DeviceError, #x " failed: %s", \ snd_strerror(err)}; \ } while(0) @@ -1065,7 +1072,8 @@ void AlsaCapture::captureSamples(std::byte *buffer, uint samples) if(amt == -EAGAIN) continue; - if((amt=snd_pcm_recover(mPcmHandle, static_cast(amt), 1)) >= 0) + amt = snd_pcm_recover(mPcmHandle, static_cast(amt), 1); + if(amt >= 0) { amt = snd_pcm_start(mPcmHandle); if(amt >= 0) @@ -1102,7 +1110,8 @@ uint AlsaCapture::availableSamples() { ERR("avail update failed: %s\n", snd_strerror(static_cast(avail))); - if((avail=snd_pcm_recover(mPcmHandle, static_cast(avail), 1)) >= 0) + avail = snd_pcm_recover(mPcmHandle, static_cast(avail), 1); + if(avail >= 0) { if(mDoCapture) avail = snd_pcm_start(mPcmHandle); @@ -1138,7 +1147,8 @@ uint AlsaCapture::availableSamples() if(amt == -EAGAIN) continue; - if((amt=snd_pcm_recover(mPcmHandle, static_cast(amt), 1)) >= 0) + amt = snd_pcm_recover(mPcmHandle, static_cast(amt), 1); + if(amt >= 0) { if(mDoCapture) amt = snd_pcm_start(mPcmHandle); diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp index 44b84296..2e6e20cc 100644 --- a/alc/backends/pipewire.cpp +++ b/alc/backends/pipewire.cpp @@ -1088,8 +1088,11 @@ void NodeProxy::paramCallback(int, uint32_t id, uint32_t, uint32_t, const spa_po if(const spa_pod_prop *prop{spa_pod_find_prop(param, nullptr, SPA_FORMAT_AUDIO_position)}) node->parsePositions(&prop->value, force_update); - else if((prop=spa_pod_find_prop(param, nullptr, SPA_FORMAT_AUDIO_channels)) != nullptr) - node->parseChannelCount(&prop->value, force_update); + else + { + prop = spa_pod_find_prop(param, nullptr, SPA_FORMAT_AUDIO_channels); + if(prop) node->parseChannelCount(&prop->value, force_update); + } } } diff --git a/alc/backends/portaudio.cpp b/alc/backends/portaudio.cpp index 15a0f3ac..2ccb1c64 100644 --- a/alc/backends/portaudio.cpp +++ b/alc/backends/portaudio.cpp @@ -394,7 +394,8 @@ bool PortBackendFactory::init() LOAD_FUNC(Pa_GetStreamInfo); #undef LOAD_FUNC - if((err=Pa_Initialize()) != paNoError) + err = Pa_Initialize(); + if(err != paNoError) { ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err)); CloseLib(pa_handle); diff --git a/alc/backends/pulseaudio.cpp b/alc/backends/pulseaudio.cpp index 6d842475..dcf01095 100644 --- a/alc/backends/pulseaudio.cpp +++ b/alc/backends/pulseaudio.cpp @@ -510,8 +510,8 @@ void MainloopUniqueLock::connectContext() pa_context_set_state_callback(mutex()->mContext, [](pa_context *ctx, void *pdata) noexcept { return static_cast(pdata)->contextStateCallback(ctx); }, this); - int err; - if((err=pa_context_connect(mutex()->mContext, nullptr, pulse_ctx_flags, nullptr)) >= 0) + int err{pa_context_connect(mutex()->mContext, nullptr, pulse_ctx_flags, nullptr)}; + if(err >= 0) { pa_context_state_t state; while((state=pa_context_get_state(mutex()->mContext)) != PA_CONTEXT_READY) diff --git a/examples/alstreamcb.cpp b/examples/alstreamcb.cpp index 1721d367..2da33b21 100644 --- a/examples/alstreamcb.cpp +++ b/examples/alstreamcb.cpp @@ -522,7 +522,8 @@ int main(int argc, char **argv) /* Get the name portion, without the path, for display. */ const char *namepart{strrchr(argv[i], '/')}; - if(namepart || (namepart=strrchr(argv[i], '\\'))) + if(!namepart) namepart = strrchr(argv[i], '\\'); + if(namepart) ++namepart; else namepart = argv[i]; diff --git a/utils/makemhr/makemhr.cpp b/utils/makemhr/makemhr.cpp index 2b6d04ce..f14110c0 100644 --- a/utils/makemhr/makemhr.cpp +++ b/utils/makemhr/makemhr.cpp @@ -342,9 +342,9 @@ static int StoreMhr(const HrirDataT *hData, const char *filename) const uint n{hData->mIrPoints}; uint dither_seed{22222}; uint fi, ei, ai, i; - FILE *fp; - if((fp=fopen(filename, "wb")) == nullptr) + FILE *fp{fopen(filename, "wb")}; + if(!fp) { fprintf(stderr, "\nError: Could not open MHR file '%s'.\n", filename); return 0; -- cgit v1.2.3 From 095d1964feacaa4ec253db028fb1e8c04f718511 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 25 Dec 2023 21:04:52 -0800 Subject: Avoid another assignment in an if statement --- al/source.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'al/source.cpp') diff --git a/al/source.cpp b/al/source.cpp index f119ee36..a440a04b 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -1923,7 +1923,8 @@ NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, con ALeffectslot *slot{}; if(values[0]) { - if((slot=LookupEffectSlot(Context, slotid)) == nullptr) UNLIKELY + slot = LookupEffectSlot(Context, slotid); + if(!slot) UNLIKELY return Context->setError(AL_INVALID_VALUE, "Invalid effect ID %s", std::to_string(slotid).c_str()); } -- cgit v1.2.3 From 1fddc044ac765d00e64628e59edcbcd71f0046b1 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 26 Dec 2023 00:02:23 -0800 Subject: Clean up some gotos and non-optimal casts --- al/effects/chorus.cpp | 4 +- al/effects/fshifter.cpp | 4 +- al/effects/modulator.cpp | 2 +- al/effects/vmorpher.cpp | 6 +- al/source.cpp | 146 +++++++++++++++++++++++--------------------- alc/alu.cpp | 12 ++-- alc/backends/oss.cpp | 14 +---- alc/backends/portaudio.cpp | 17 ++---- alc/backends/pulseaudio.cpp | 5 +- alc/backends/sndio.cpp | 131 ++++++++++++++++++++------------------- examples/alconvolve.c | 15 +++-- examples/alffplay.cpp | 3 +- examples/almultireverb.c | 6 +- examples/alstream.c | 9 ++- examples/alstreamcb.cpp | 4 +- utils/makemhr/loaddef.cpp | 6 +- 16 files changed, 190 insertions(+), 194 deletions(-) (limited to 'al/source.cpp') diff --git a/al/effects/chorus.cpp b/al/effects/chorus.cpp index dba59d1d..913d1215 100644 --- a/al/effects/chorus.cpp +++ b/al/effects/chorus.cpp @@ -49,7 +49,7 @@ inline ALenum EnumFromWaveform(ChorusWaveform type) EffectProps genDefaultChorusProps() noexcept { ChorusProps props{}; - props.Waveform = *WaveformFromEnum(AL_CHORUS_DEFAULT_WAVEFORM); + props.Waveform = WaveformFromEnum(AL_CHORUS_DEFAULT_WAVEFORM).value(); props.Phase = AL_CHORUS_DEFAULT_PHASE; props.Rate = AL_CHORUS_DEFAULT_RATE; props.Depth = AL_CHORUS_DEFAULT_DEPTH; @@ -61,7 +61,7 @@ EffectProps genDefaultChorusProps() noexcept EffectProps genDefaultFlangerProps() noexcept { FlangerProps props{}; - props.Waveform = *WaveformFromEnum(AL_FLANGER_DEFAULT_WAVEFORM); + props.Waveform = WaveformFromEnum(AL_FLANGER_DEFAULT_WAVEFORM).value(); props.Phase = AL_FLANGER_DEFAULT_PHASE; props.Rate = AL_FLANGER_DEFAULT_RATE; props.Depth = AL_FLANGER_DEFAULT_DEPTH; diff --git a/al/effects/fshifter.cpp b/al/effects/fshifter.cpp index 6f19e0dd..556244ac 100644 --- a/al/effects/fshifter.cpp +++ b/al/effects/fshifter.cpp @@ -45,8 +45,8 @@ EffectProps genDefaultProps() noexcept { FshifterProps props{}; props.Frequency = AL_FREQUENCY_SHIFTER_DEFAULT_FREQUENCY; - props.LeftDirection = *DirectionFromEmum(AL_FREQUENCY_SHIFTER_DEFAULT_LEFT_DIRECTION); - props.RightDirection = *DirectionFromEmum(AL_FREQUENCY_SHIFTER_DEFAULT_RIGHT_DIRECTION); + props.LeftDirection = DirectionFromEmum(AL_FREQUENCY_SHIFTER_DEFAULT_LEFT_DIRECTION).value(); + props.RightDirection = DirectionFromEmum(AL_FREQUENCY_SHIFTER_DEFAULT_RIGHT_DIRECTION).value(); return props; } diff --git a/al/effects/modulator.cpp b/al/effects/modulator.cpp index 566b333e..7e9424c0 100644 --- a/al/effects/modulator.cpp +++ b/al/effects/modulator.cpp @@ -47,7 +47,7 @@ EffectProps genDefaultProps() noexcept ModulatorProps props{}; props.Frequency = AL_RING_MODULATOR_DEFAULT_FREQUENCY; props.HighPassCutoff = AL_RING_MODULATOR_DEFAULT_HIGHPASS_CUTOFF; - props.Waveform = *WaveformFromEmum(AL_RING_MODULATOR_DEFAULT_WAVEFORM); + props.Waveform = WaveformFromEmum(AL_RING_MODULATOR_DEFAULT_WAVEFORM).value(); return props; } diff --git a/al/effects/vmorpher.cpp b/al/effects/vmorpher.cpp index a986ddf7..35a64d32 100644 --- a/al/effects/vmorpher.cpp +++ b/al/effects/vmorpher.cpp @@ -126,11 +126,11 @@ EffectProps genDefaultProps() noexcept { VmorpherProps props{}; props.Rate = AL_VOCAL_MORPHER_DEFAULT_RATE; - props.PhonemeA = *PhenomeFromEnum(AL_VOCAL_MORPHER_DEFAULT_PHONEMEA); - props.PhonemeB = *PhenomeFromEnum(AL_VOCAL_MORPHER_DEFAULT_PHONEMEB); + props.PhonemeA = PhenomeFromEnum(AL_VOCAL_MORPHER_DEFAULT_PHONEMEA).value(); + props.PhonemeB = PhenomeFromEnum(AL_VOCAL_MORPHER_DEFAULT_PHONEMEB).value(); props.PhonemeACoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEA_COARSE_TUNING; props.PhonemeBCoarseTuning = AL_VOCAL_MORPHER_DEFAULT_PHONEMEB_COARSE_TUNING; - props.Waveform = *WaveformFromEmum(AL_VOCAL_MORPHER_DEFAULT_WAVEFORM); + props.Waveform = WaveformFromEmum(AL_VOCAL_MORPHER_DEFAULT_WAVEFORM).value(); return props; } diff --git a/al/source.cpp b/al/source.cpp index a440a04b..bf96a769 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -3407,90 +3407,94 @@ FORCE_ALIGN void AL_APIENTRY alSourceQueueBuffersDirect(ALCcontext *context, ALu std::unique_lock buflock{device->BufferLock}; const size_t NewListStart{source->mQueue.size()}; - ALbufferQueueItem *BufferList{nullptr}; - for(ALsizei i{0};i < nb;i++) - { - bool fmt_mismatch{false}; - ALbuffer *buffer{buffers[i] ? LookupBuffer(device, buffers[i]) : nullptr}; - if(buffers[i] && !buffer) - { - context->setError(AL_INVALID_NAME, "Queueing invalid buffer ID %u", buffers[i]); - goto buffer_error; - } - if(buffer) + try { + ALbufferQueueItem *BufferList{nullptr}; + for(ALsizei i{0};i < nb;i++) { - if(buffer->mSampleRate < 1) + bool fmt_mismatch{false}; + ALbuffer *buffer{buffers[i] ? LookupBuffer(device, buffers[i]) : nullptr}; + if(buffers[i] && !buffer) { - context->setError(AL_INVALID_OPERATION, "Queueing buffer %u with no format", - buffer->id); - goto buffer_error; + context->setError(AL_INVALID_NAME, "Queueing invalid buffer ID %u", buffers[i]); + throw std::exception{}; } - if(buffer->mCallback) + if(buffer) { - context->setError(AL_INVALID_OPERATION, "Queueing callback buffer %u", buffer->id); - goto buffer_error; + if(buffer->mSampleRate < 1) + { + context->setError(AL_INVALID_OPERATION, "Queueing buffer %u with no format", + buffer->id); + throw std::exception{}; + } + if(buffer->mCallback) + { + context->setError(AL_INVALID_OPERATION, "Queueing callback buffer %u", + buffer->id); + throw std::exception{}; + } + if(buffer->MappedAccess != 0 && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) + { + context->setError(AL_INVALID_OPERATION, + "Queueing non-persistently mapped buffer %u", buffer->id); + throw std::exception{}; + } } - if(buffer->MappedAccess != 0 && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT)) + + source->mQueue.emplace_back(); + if(!BufferList) + BufferList = &source->mQueue.back(); + else { - context->setError(AL_INVALID_OPERATION, - "Queueing non-persistently mapped buffer %u", buffer->id); - goto buffer_error; + auto &item = source->mQueue.back(); + BufferList->mNext.store(&item, std::memory_order_relaxed); + BufferList = &item; } - } - - source->mQueue.emplace_back(); - if(!BufferList) - BufferList = &source->mQueue.back(); - else - { - auto &item = source->mQueue.back(); - BufferList->mNext.store(&item, std::memory_order_relaxed); - BufferList = &item; - } - if(!buffer) continue; - BufferList->mBlockAlign = buffer->mBlockAlign; - BufferList->mSampleLen = buffer->mSampleLen; - BufferList->mLoopEnd = buffer->mSampleLen; - BufferList->mSamples = buffer->mData.data(); - BufferList->mBuffer = buffer; - IncrementRef(buffer->ref); - - if(BufferFmt == nullptr) - BufferFmt = buffer; - else - { - fmt_mismatch |= BufferFmt->mSampleRate != buffer->mSampleRate; - fmt_mismatch |= BufferFmt->mChannels != buffer->mChannels; - fmt_mismatch |= BufferFmt->mType != buffer->mType; - if(BufferFmt->isBFormat()) + if(!buffer) continue; + BufferList->mBlockAlign = buffer->mBlockAlign; + BufferList->mSampleLen = buffer->mSampleLen; + BufferList->mLoopEnd = buffer->mSampleLen; + BufferList->mSamples = buffer->mData.data(); + BufferList->mBuffer = buffer; + IncrementRef(buffer->ref); + + if(BufferFmt == nullptr) + BufferFmt = buffer; + else { - fmt_mismatch |= BufferFmt->mAmbiLayout != buffer->mAmbiLayout; - fmt_mismatch |= BufferFmt->mAmbiScaling != buffer->mAmbiScaling; + fmt_mismatch |= BufferFmt->mSampleRate != buffer->mSampleRate; + fmt_mismatch |= BufferFmt->mChannels != buffer->mChannels; + fmt_mismatch |= BufferFmt->mType != buffer->mType; + if(BufferFmt->isBFormat()) + { + fmt_mismatch |= BufferFmt->mAmbiLayout != buffer->mAmbiLayout; + fmt_mismatch |= BufferFmt->mAmbiScaling != buffer->mAmbiScaling; + } + fmt_mismatch |= BufferFmt->mAmbiOrder != buffer->mAmbiOrder; } - fmt_mismatch |= BufferFmt->mAmbiOrder != buffer->mAmbiOrder; - } - if(fmt_mismatch) UNLIKELY - { - context->setError(AL_INVALID_OPERATION, "Queueing buffer with mismatched format\n" - " Expected: %uhz, %s, %s ; Got: %uhz, %s, %s\n", BufferFmt->mSampleRate, - NameFromFormat(BufferFmt->mType), NameFromFormat(BufferFmt->mChannels), - buffer->mSampleRate, NameFromFormat(buffer->mType), - NameFromFormat(buffer->mChannels)); - - buffer_error: - /* A buffer failed (invalid ID or format), so unlock and release - * each buffer we had. - */ - auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart); - for(;iter != source->mQueue.end();++iter) + if(fmt_mismatch) UNLIKELY { - if(ALbuffer *buf{iter->mBuffer}) - DecrementRef(buf->ref); + context->setError(AL_INVALID_OPERATION, "Queueing buffer with mismatched format\n" + " Expected: %uhz, %s, %s ; Got: %uhz, %s, %s\n", BufferFmt->mSampleRate, + NameFromFormat(BufferFmt->mType), NameFromFormat(BufferFmt->mChannels), + buffer->mSampleRate, NameFromFormat(buffer->mType), + NameFromFormat(buffer->mChannels)); + throw std::exception{}; } - source->mQueue.resize(NewListStart); - return; } } + catch(...) { + /* A buffer failed (invalid ID or format), or there was some other + * unexpected error, so unlock and release each buffer we had. + */ + auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart); + for(;iter != source->mQueue.end();++iter) + { + if(ALbuffer *buf{iter->mBuffer}) + DecrementRef(buf->ref); + } + source->mQueue.resize(NewListStart); + return; + } /* All buffers good. */ buflock.unlock(); diff --git a/alc/alu.cpp b/alc/alu.cpp index eb3dee5f..9e7a758e 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -678,16 +678,16 @@ void AmbiRotator(AmbiRotateMatrix &matrix, const int order) auto P = [](const int i, const int l, const int a, const int n, const size_t last_band, const AmbiRotateMatrix &R) { - const float ri1{ R[ 1+2][static_cast(i+2)]}; - const float rim1{R[-1+2][static_cast(i+2)]}; - const float ri0{ R[ 0+2][static_cast(i+2)]}; + const float ri1{ R[ 1+2][static_cast(i+2_z)]}; + const float rim1{R[-1+2][static_cast(i+2_z)]}; + const float ri0{ R[ 0+2][static_cast(i+2_z)]}; const size_t y{last_band + static_cast(a+l-1)}; if(n == -l) - return ri1*R[last_band][y] + rim1*R[last_band + static_cast(l-1)*2][y]; + return ri1*R[last_band][y] + rim1*R[last_band + static_cast(l-1_z)*2][y]; if(n == l) - return ri1*R[last_band + static_cast(l-1)*2][y] - rim1*R[last_band][y]; - return ri0*R[last_band + static_cast(n+l-1)][y]; + return ri1*R[last_band + static_cast(l-1_z)*2][y] - rim1*R[last_band][y]; + return ri0*R[last_band + static_cast(l-1_z+n)][y]; }; auto U = [P](const int l, const int m, const int n, const size_t last_band, diff --git a/alc/backends/oss.cpp b/alc/backends/oss.cpp index 50bed5ee..d541b534 100644 --- a/alc/backends/oss.cpp +++ b/alc/backends/oss.cpp @@ -367,11 +367,9 @@ bool OSSPlayback::reset() uint numFragmentsLogSize{(periods << 16) | log2FragmentSize}; audio_buf_info info{}; - const char *err; -#define CHECKERR(func) if((func) < 0) { \ - err = #func; \ - goto err; \ -} +#define CHECKERR(func) if((func) < 0) \ + throw al::backend_exception{al::backend_error::DeviceError, "%s failed: %s\n", #func, strerror(errno)}; + /* Don't fail if SETFRAGMENT fails. We can handle just about anything * that's reported back via GETOSPACE */ ioctl(mFd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize); @@ -379,12 +377,6 @@ bool OSSPlayback::reset() CHECKERR(ioctl(mFd, SNDCTL_DSP_CHANNELS, &numChannels)); CHECKERR(ioctl(mFd, SNDCTL_DSP_SPEED, &ossSpeed)); CHECKERR(ioctl(mFd, SNDCTL_DSP_GETOSPACE, &info)); - if(false) - { - err: - ERR("%s failed: %s\n", err, strerror(errno)); - return false; - } #undef CHECKERR if(mDevice->channelsFromFmt() != numChannels) diff --git a/alc/backends/portaudio.cpp b/alc/backends/portaudio.cpp index 2ccb1c64..a8bd00fd 100644 --- a/alc/backends/portaudio.cpp +++ b/alc/backends/portaudio.cpp @@ -148,7 +148,6 @@ void PortPlayback::open(std::string_view name) break; } -retry_open: static constexpr auto writeCallback = [](const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags, void *userData) noexcept @@ -157,17 +156,13 @@ retry_open: framesPerBuffer, timeInfo, statusFlags); }; PaStream *stream{}; - PaError err{Pa_OpenStream(&stream, nullptr, ¶ms, mDevice->Frequency, mDevice->UpdateSize, - paNoFlag, writeCallback, this)}; - if(err != paNoError) + while(PaError err{Pa_OpenStream(&stream, nullptr, ¶ms, mDevice->Frequency, + mDevice->UpdateSize, paNoFlag, writeCallback, this)}) { - if(params.sampleFormat == paFloat32) - { - params.sampleFormat = paInt16; - goto retry_open; - } - throw al::backend_exception{al::backend_error::NoDevice, "Failed to open stream: %s", - Pa_GetErrorText(err)}; + if(params.sampleFormat != paFloat32) + throw al::backend_exception{al::backend_error::NoDevice, "Failed to open stream: %s", + Pa_GetErrorText(err)}; + params.sampleFormat = paInt16; } Pa_CloseStream(mStream); diff --git a/alc/backends/pulseaudio.cpp b/alc/backends/pulseaudio.cpp index dcf01095..77d45466 100644 --- a/alc/backends/pulseaudio.cpp +++ b/alc/backends/pulseaudio.cpp @@ -968,8 +968,9 @@ bool PulsePlayback::reset() const auto scale = static_cast(mSpec.rate) / mDevice->Frequency; const auto perlen = static_cast(clampd(scale*mDevice->UpdateSize + 0.5, 64.0, 8192.0)); - const auto buflen = static_cast(clampd(scale*mDevice->BufferSize + 0.5, perlen*2, - std::numeric_limits::max()/mFrameSize)); + const auto bufmax = uint{std::numeric_limits::max() / mFrameSize}; + const auto buflen = static_cast(clampd(scale*mDevice->BufferSize + 0.5, perlen*2.0, + bufmax)); mAttr.maxlength = ~0u; mAttr.tlength = buflen * mFrameSize; diff --git a/alc/backends/sndio.cpp b/alc/backends/sndio.cpp index 0e667874..ce3de366 100644 --- a/alc/backends/sndio.cpp +++ b/alc/backends/sndio.cpp @@ -135,72 +135,75 @@ bool SndioPlayback::reset() SioPar par; auto tryfmt = mDevice->FmtType; -retry_params: - switch(tryfmt) + while(true) { - case DevFmtByte: - par.bits = 8; - par.sig = 1; - break; - case DevFmtUByte: - par.bits = 8; - par.sig = 0; - break; - case DevFmtShort: - par.bits = 16; - par.sig = 1; - break; - case DevFmtUShort: - par.bits = 16; - par.sig = 0; - break; - case DevFmtFloat: - case DevFmtInt: - par.bits = 32; - par.sig = 1; - break; - case DevFmtUInt: - par.bits = 32; - par.sig = 0; - break; - } - par.bps = SIO_BPS(par.bits); - par.le = SIO_LE_NATIVE; - par.msb = 1; - - par.rate = mDevice->Frequency; - par.pchan = mDevice->channelsFromFmt(); - - par.round = mDevice->UpdateSize; - par.appbufsz = mDevice->BufferSize - mDevice->UpdateSize; - if(!par.appbufsz) par.appbufsz = mDevice->UpdateSize; + switch(tryfmt) + { + case DevFmtByte: + par.bits = 8; + par.sig = 1; + break; + case DevFmtUByte: + par.bits = 8; + par.sig = 0; + break; + case DevFmtShort: + par.bits = 16; + par.sig = 1; + break; + case DevFmtUShort: + par.bits = 16; + par.sig = 0; + break; + case DevFmtFloat: + case DevFmtInt: + par.bits = 32; + par.sig = 1; + break; + case DevFmtUInt: + par.bits = 32; + par.sig = 0; + break; + } + par.bps = SIO_BPS(par.bits); + par.le = SIO_LE_NATIVE; + par.msb = 1; + + par.rate = mDevice->Frequency; + par.pchan = mDevice->channelsFromFmt(); + + par.round = mDevice->UpdateSize; + par.appbufsz = mDevice->BufferSize - mDevice->UpdateSize; + if(!par.appbufsz) par.appbufsz = mDevice->UpdateSize; + + try { + if(!sio_setpar(mSndHandle, &par)) + throw al::backend_exception{al::backend_error::DeviceError, + "Failed to set device parameters"}; + + par.clear(); + if(!sio_getpar(mSndHandle, &par)) + throw al::backend_exception{al::backend_error::DeviceError, + "Failed to get device parameters"}; + + if(par.bps > 1 && par.le != SIO_LE_NATIVE) + throw al::backend_exception{al::backend_error::DeviceError, + "%s-endian samples not supported", par.le ? "Little" : "Big"}; + if(par.bits < par.bps*8 && !par.msb) + throw al::backend_exception{al::backend_error::DeviceError, + "MSB-padded samples not supported (%u of %u bits)", par.bits, par.bps*8}; + if(par.pchan < 1) + throw al::backend_exception{al::backend_error::DeviceError, + "No playback channels on device"}; - try { - if(!sio_setpar(mSndHandle, &par)) - throw al::backend_exception{al::backend_error::DeviceError, - "Failed to set device parameters"}; - - par.clear(); - if(!sio_getpar(mSndHandle, &par)) - throw al::backend_exception{al::backend_error::DeviceError, - "Failed to get device parameters"}; - - if(par.bps > 1 && par.le != SIO_LE_NATIVE) - throw al::backend_exception{al::backend_error::DeviceError, - "%s-endian samples not supported", par.le ? "Little" : "Big"}; - if(par.bits < par.bps*8 && !par.msb) - throw al::backend_exception{al::backend_error::DeviceError, - "MSB-padded samples not supported (%u of %u bits)", par.bits, par.bps*8}; - if(par.pchan < 1) - throw al::backend_exception{al::backend_error::DeviceError, - "No playback channels on device"}; - } - catch(al::backend_exception &e) { - if(tryfmt == DevFmtShort) - throw; - par.clear(); - tryfmt = DevFmtShort; - goto retry_params; + break; + } + catch(al::backend_exception &e) { + if(tryfmt == DevFmtShort) + throw; + par.clear(); + tryfmt = DevFmtShort; + } } if(par.bps == 1) diff --git a/examples/alconvolve.c b/examples/alconvolve.c index d194d270..597d6ea2 100644 --- a/examples/alconvolve.c +++ b/examples/alconvolve.c @@ -361,10 +361,10 @@ static ALuint LoadSound(const char *filename) } namepart = strrchr(filename, '/'); - if(namepart || (namepart=strrchr(filename, '\\'))) - namepart++; - else - namepart = filename; + if(!namepart) namepart = strrchr(filename, '\\'); + if(!namepart) namepart = filename; + else namepart++; + printf("Loading: %s (%s, %dhz, %" PRId64 " samples / %.2f seconds)\n", namepart, FormatName(format), sfinfo.samplerate, sfinfo.frames, (double)sfinfo.frames / sfinfo.samplerate); @@ -557,10 +557,9 @@ int main(int argc, char **argv) continue; namepart = strrchr(argv[i], '/'); - if(namepart || (namepart=strrchr(argv[i], '\\'))) - namepart++; - else - namepart = argv[i]; + if(!namepart) namepart = strrchr(argv[i], '\\'); + if(!namepart) namepart = argv[i]; + else namepart++; printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format), player->sfinfo.samplerate); diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index a7633bfa..cfbe5564 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -1297,7 +1297,7 @@ int AudioState::handler() mSamplesLen = decodeFrame(); mSamplesPos = mSamplesLen; } while(mSamplesLen > 0); - goto finish; + break; } ALenum state; @@ -1378,7 +1378,6 @@ int AudioState::handler() mSrcCond.wait_for(srclock, sleep_time); } -finish: alSourceRewind(mSource); alSourcei(mSource, AL_BUFFER, 0); diff --git a/examples/almultireverb.c b/examples/almultireverb.c index d1deae5a..f9a46c4f 100644 --- a/examples/almultireverb.c +++ b/examples/almultireverb.c @@ -108,7 +108,8 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb) * the needed panning vectors). */ alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB); - if((err=alGetError()) != AL_NO_ERROR) + err = alGetError(); + if(err != AL_NO_ERROR) { fprintf(stderr, "Failed to set EAX Reverb: %s (0x%04x)\n", alGetString(err), err); return 0; @@ -140,7 +141,8 @@ static int LoadEffect(ALuint effect, const EFXEAXREVERBPROPERTIES *reverb) alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit); /* Check if an error occurred, and return failure if so. */ - if((err=alGetError()) != AL_NO_ERROR) + err = alGetError(); + if(err != AL_NO_ERROR) { fprintf(stderr, "Error setting up reverb: %s\n", alGetString(err)); return 0; diff --git a/examples/alstream.c b/examples/alstream.c index c781f3d7..028290f5 100644 --- a/examples/alstream.c +++ b/examples/alstream.c @@ -294,7 +294,7 @@ static int OpenPlayerFile(StreamPlayer *player, const char *filename) player->block_count = player->sfinfo.samplerate / player->sampleblockalign; player->block_count = player->block_count * BufferMillisec / 1000; - player->membuf = malloc((size_t)(player->block_count * player->byteblockalign)); + player->membuf = malloc((size_t)player->block_count * (size_t)player->byteblockalign); return 1; } @@ -488,10 +488,9 @@ int main(int argc, char **argv) /* Get the name portion, without the path, for display. */ namepart = strrchr(argv[i], '/'); - if(namepart || (namepart=strrchr(argv[i], '\\'))) - namepart++; - else - namepart = argv[i]; + if(!namepart) namepart = strrchr(argv[i], '\\'); + if(!namepart) namepart = argv[i]; + else namepart++; printf("Playing: %s (%s, %dhz)\n", namepart, FormatName(player->format), player->sfinfo.samplerate); diff --git a/examples/alstreamcb.cpp b/examples/alstreamcb.cpp index 2da33b21..ebf3d3b0 100644 --- a/examples/alstreamcb.cpp +++ b/examples/alstreamcb.cpp @@ -195,12 +195,12 @@ struct StreamPlayer { if(mSampleFormat == SampleType::Int16) { mSamplesPerBlock = 1; - mBytesPerBlock = static_cast(mSfInfo.channels * 2); + mBytesPerBlock = static_cast(mSfInfo.channels) * 2; } else if(mSampleFormat == SampleType::Float) { mSamplesPerBlock = 1; - mBytesPerBlock = static_cast(mSfInfo.channels * 4); + mBytesPerBlock = static_cast(mSfInfo.channels) * 4; } else { diff --git a/utils/makemhr/loaddef.cpp b/utils/makemhr/loaddef.cpp index b33dbc75..05bcfd2e 100644 --- a/utils/makemhr/loaddef.cpp +++ b/utils/makemhr/loaddef.cpp @@ -907,7 +907,8 @@ static int ReadWaveList(std::istream &istream, const SourceRefT *src, const Byte fprintf(stderr, "\nError: Bad read from file '%s'.\n", src->mPath.data()); return 0; } - istream.seekg(static_cast(src->mOffset * block), std::ios::cur); + using off_type = std::istream::off_type; + istream.seekg(off_type(src->mOffset) * off_type(block), std::ios::cur); if(!ReadWaveData(istream, src, order, n, &hrir[0])) return 0; return 1; @@ -939,7 +940,8 @@ static int ReadWaveList(std::istream &istream, const SourceRefT *src, const Byte count = chunkSize / block; if(count > skip) { - istream.seekg(static_cast(skip * block), std::ios::cur); + using off_type = std::istream::off_type; + istream.seekg(off_type(skip) * off_type(block), std::ios::cur); chunkSize -= skip * block; count -= skip; skip = 0; -- cgit v1.2.3 From 10ecdff7d1dfcc16bd2a090f089781310ea9a93d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 29 Dec 2023 03:39:58 -0800 Subject: Handle pointer ownership a bit better --- al/auxeffectslot.cpp | 30 ++++++++++++------------- al/buffer.cpp | 10 ++++----- al/effect.cpp | 10 ++++----- al/filter.cpp | 10 ++++----- al/source.cpp | 15 +++++++------ alc/alc.cpp | 46 ++++++++++++++++++++++++++------------ alc/backends/wave.cpp | 6 ++--- alc/context.cpp | 4 ++-- alc/context.h | 5 +++-- alc/device.h | 7 +++--- core/mastering.cpp | 13 ++++++----- utils/alsoft-config/mainwindow.cpp | 16 ++++++------- utils/makemhr/makemhr.cpp | 31 +++++++++++++------------ utils/uhjdecoder.cpp | 3 ++- 14 files changed, 115 insertions(+), 91 deletions(-) (limited to 'al/source.cpp') diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index ea41a842..695c5788 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -96,7 +96,7 @@ inline ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id) noexcept EffectSlotSubList &sublist{context->mEffectSlotList[lidx]}; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.EffectSlots + slidx; + return al::to_address(sublist.EffectSlots->begin() + slidx); } inline ALeffect *LookupEffect(ALCdevice *device, ALuint id) noexcept @@ -109,7 +109,7 @@ inline ALeffect *LookupEffect(ALCdevice *device, ALuint id) noexcept EffectSubList &sublist = device->EffectList[lidx]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Effects + slidx; + return al::to_address(sublist.Effects->begin() + slidx); } inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) noexcept @@ -122,7 +122,7 @@ inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) noexcept BufferSubList &sublist = device->BufferList[lidx]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Buffers + slidx; + return al::to_address(sublist.Buffers->begin() + slidx); } @@ -246,8 +246,8 @@ bool EnsureEffectSlots(ALCcontext *context, size_t needed) context->mEffectSlotList.emplace_back(); auto sublist = context->mEffectSlotList.end() - 1; sublist->FreeMask = ~0_u64; - sublist->EffectSlots = static_cast>( - al_calloc(alignof(ALeffectslot), sizeof(ALeffectslot)*64)); + sublist->EffectSlots = static_cast*>>( + al_calloc(alignof(ALeffectslot), sizeof(*sublist->EffectSlots))); if(!sublist->EffectSlots) UNLIKELY { context->mEffectSlotList.pop_back(); @@ -267,7 +267,8 @@ ALeffectslot *AllocEffectSlot(ALCcontext *context) auto slidx = static_cast(al::countr_zero(sublist->FreeMask)); ASSUME(slidx < 64); - ALeffectslot *slot{al::construct_at(sublist->EffectSlots + slidx, context)}; + ALeffectslot *slot{al::construct_at(al::to_address(sublist->EffectSlots->begin() + slidx), + context)}; aluInitEffectPanning(slot->mSlot, context); /* Add 1 to avoid ID 0. */ @@ -875,12 +876,9 @@ ALeffectslot::~ALeffectslot() DecrementRef(Buffer->ref); Buffer = nullptr; - if(EffectSlotProps *props{mSlot->Update.exchange(nullptr)}) - { + if(std::unique_ptr props{mSlot->Update.exchange(nullptr)}) TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", - decltype(std::declval()){props}); - delete props; - } + decltype(std::declval()){props.get()}); mSlot->mEffectState = nullptr; mSlot->InUse = false; @@ -983,12 +981,12 @@ void UpdateAllEffectSlotProps(ALCcontext *context) uint64_t usemask{~sublist.FreeMask}; while(usemask) { - const int idx{al::countr_zero(usemask)}; + const auto idx = static_cast(al::countr_zero(usemask)); usemask &= ~(1_u64 << idx); - ALeffectslot *slot{sublist.EffectSlots + idx}; + auto &slot = (*sublist.EffectSlots)[idx]; - if(slot->mState != SlotState::Stopped && std::exchange(slot->mPropsDirty, false)) - slot->updateProps(context); + if(slot.mState != SlotState::Stopped && std::exchange(slot.mPropsDirty, false)) + slot.updateProps(context); } } } @@ -1002,7 +1000,7 @@ EffectSlotSubList::~EffectSlotSubList() while(usemask) { const int idx{al::countr_zero(usemask)}; - std::destroy_at(EffectSlots+idx); + std::destroy_at(al::to_address(EffectSlots->begin() + idx)); usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; diff --git a/al/buffer.cpp b/al/buffer.cpp index c0f3f348..46ef8ece 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -186,8 +186,8 @@ bool EnsureBuffers(ALCdevice *device, size_t needed) device->BufferList.emplace_back(); auto sublist = device->BufferList.end() - 1; sublist->FreeMask = ~0_u64; - sublist->Buffers = static_cast>(al_calloc(alignof(ALbuffer), - sizeof(ALbuffer)*64)); + sublist->Buffers = static_cast*>>(al_calloc( + alignof(ALbuffer), sizeof(*sublist->Buffers))); if(!sublist->Buffers) UNLIKELY { device->BufferList.pop_back(); @@ -207,7 +207,7 @@ ALbuffer *AllocBuffer(ALCdevice *device) auto slidx = static_cast(al::countr_zero(sublist->FreeMask)); ASSUME(slidx < 64); - ALbuffer *buffer{al::construct_at(sublist->Buffers + slidx)}; + ALbuffer *buffer{al::construct_at(al::to_address(sublist->Buffers->begin() + slidx))}; /* Add 1 to avoid buffer ID 0. */ buffer->id = ((lidx<<6) | slidx) + 1; @@ -244,7 +244,7 @@ inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) BufferSubList &sublist = device->BufferList[lidx]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Buffers + slidx; + return al::to_address(sublist.Buffers->begin() + slidx); } @@ -1486,7 +1486,7 @@ BufferSubList::~BufferSubList() while(usemask) { const int idx{al::countr_zero(usemask)}; - std::destroy_at(Buffers+idx); + std::destroy_at(al::to_address(Buffers->begin() + idx)); usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; diff --git a/al/effect.cpp b/al/effect.cpp index c2a2d1b1..1024de80 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -134,8 +134,8 @@ bool EnsureEffects(ALCdevice *device, size_t needed) device->EffectList.emplace_back(); auto sublist = device->EffectList.end() - 1; sublist->FreeMask = ~0_u64; - sublist->Effects = static_cast>(al_calloc(alignof(ALeffect), - sizeof(ALeffect)*64)); + sublist->Effects = static_cast*>>(al_calloc( + alignof(ALeffect), sizeof(*sublist->Effects))); if(!sublist->Effects) UNLIKELY { device->EffectList.pop_back(); @@ -155,7 +155,7 @@ ALeffect *AllocEffect(ALCdevice *device) auto slidx = static_cast(al::countr_zero(sublist->FreeMask)); ASSUME(slidx < 64); - ALeffect *effect{al::construct_at(sublist->Effects + slidx)}; + ALeffect *effect{al::construct_at(al::to_address(sublist->Effects->begin() + slidx))}; InitEffectParams(effect, AL_EFFECT_NULL); /* Add 1 to avoid effect ID 0. */ @@ -189,7 +189,7 @@ inline ALeffect *LookupEffect(ALCdevice *device, ALuint id) EffectSubList &sublist = device->EffectList[lidx]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Effects + slidx; + return al::to_address(sublist.Effects->begin() + slidx); } } // namespace @@ -568,7 +568,7 @@ EffectSubList::~EffectSubList() while(usemask) { const int idx{al::countr_zero(usemask)}; - std::destroy_at(Effects+idx); + std::destroy_at(al::to_address(Effects->begin()+idx)); usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; diff --git a/al/filter.cpp b/al/filter.cpp index ce37b0aa..4a24a38f 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -129,8 +129,8 @@ bool EnsureFilters(ALCdevice *device, size_t needed) device->FilterList.emplace_back(); auto sublist = device->FilterList.end() - 1; sublist->FreeMask = ~0_u64; - sublist->Filters = static_cast>(al_calloc(alignof(ALfilter), - sizeof(ALfilter)*64)); + sublist->Filters = static_cast*>>(al_calloc( + alignof(ALfilter), sizeof(*sublist->Filters))); if(!sublist->Filters) UNLIKELY { device->FilterList.pop_back(); @@ -151,7 +151,7 @@ ALfilter *AllocFilter(ALCdevice *device) auto slidx = static_cast(al::countr_zero(sublist->FreeMask)); ASSUME(slidx < 64); - ALfilter *filter{al::construct_at(sublist->Filters + slidx)}; + ALfilter *filter{al::construct_at(al::to_address(sublist->Filters->begin() + slidx))}; InitFilterParams(filter, AL_FILTER_NULL); /* Add 1 to avoid filter ID 0. */ @@ -186,7 +186,7 @@ inline ALfilter *LookupFilter(ALCdevice *device, ALuint id) FilterSubList &sublist = device->FilterList[lidx]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Filters + slidx; + return al::to_address(sublist.Filters->begin() + slidx); } } // namespace @@ -696,7 +696,7 @@ FilterSubList::~FilterSubList() while(usemask) { const int idx{al::countr_zero(usemask)}; - std::destroy_at(Filters+idx); + std::destroy_at(al::to_address(Filters->begin() + idx)); usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; diff --git a/al/source.cpp b/al/source.cpp index bf96a769..1ac3bf28 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -729,7 +729,8 @@ bool EnsureSources(ALCcontext *context, size_t needed) context->mSourceList.emplace_back(); auto sublist = context->mSourceList.end() - 1; sublist->FreeMask = ~0_u64; - sublist->Sources = static_cast(al_calloc(alignof(ALsource), sizeof(ALsource)*64)); + sublist->Sources = static_cast*>>(al_calloc( + alignof(ALsource), sizeof(*sublist->Sources))); if(!sublist->Sources) UNLIKELY { context->mSourceList.pop_back(); @@ -749,7 +750,7 @@ ALsource *AllocSource(ALCcontext *context) auto slidx = static_cast(al::countr_zero(sublist->FreeMask)); ASSUME(slidx < 64); - ALsource *source{al::construct_at(sublist->Sources + slidx)}; + ALsource *source{al::construct_at(al::to_address(sublist->Sources->begin() + slidx))}; /* Add 1 to avoid source ID 0. */ source->id = ((lidx<<6) | slidx) + 1; @@ -797,7 +798,7 @@ inline ALsource *LookupSource(ALCcontext *context, ALuint id) noexcept SourceSubList &sublist{context->mSourceList[lidx]}; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Sources + slidx; + return al::to_address(sublist.Sources->begin() + slidx); } auto LookupBuffer = [](ALCdevice *device, auto id) noexcept -> ALbuffer* @@ -810,7 +811,7 @@ auto LookupBuffer = [](ALCdevice *device, auto id) noexcept -> ALbuffer* BufferSubList &sublist = device->BufferList[static_cast(lidx)]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Buffers + static_cast(slidx); + return al::to_address(sublist.Buffers->begin() + static_cast(slidx)); }; auto LookupFilter = [](ALCdevice *device, auto id) noexcept -> ALfilter* @@ -823,7 +824,7 @@ auto LookupFilter = [](ALCdevice *device, auto id) noexcept -> ALfilter* FilterSubList &sublist = device->FilterList[static_cast(lidx)]; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.Filters + static_cast(slidx); + return al::to_address(sublist.Filters->begin() + static_cast(slidx)); }; auto LookupEffectSlot = [](ALCcontext *context, auto id) noexcept -> ALeffectslot* @@ -836,7 +837,7 @@ auto LookupEffectSlot = [](ALCcontext *context, auto id) noexcept -> ALeffectslo EffectSlotSubList &sublist{context->mEffectSlotList[static_cast(lidx)]}; if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY return nullptr; - return sublist.EffectSlots + static_cast(slidx); + return al::to_address(sublist.EffectSlots->begin() + static_cast(slidx)); }; @@ -3639,7 +3640,7 @@ SourceSubList::~SourceSubList() { const int idx{al::countr_zero(usemask)}; usemask &= ~(1_u64 << idx); - std::destroy_at(Sources+idx); + std::destroy_at(al::to_address(Sources->begin() + idx)); } FreeMask = ~usemask; al_free(Sources); diff --git a/alc/alc.cpp b/alc/alc.cpp index 64b77080..ece65430 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -1682,16 +1682,16 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) uint64_t usemask{~sublist.FreeMask}; while(usemask) { - const int idx{al::countr_zero(usemask)}; - ALeffectslot *slot{sublist.EffectSlots + idx}; + const auto idx = static_cast(al::countr_zero(usemask)); + auto &slot = (*sublist.EffectSlots)[idx]; usemask &= ~(1_u64 << idx); - aluInitEffectPanning(slot->mSlot, context); + aluInitEffectPanning(slot.mSlot, context); - EffectState *state{slot->Effect.State.get()}; + EffectState *state{slot.Effect.State.get()}; state->mOutTarget = device->Dry.Buffer; - state->deviceUpdate(device, slot->Buffer); - slot->updateProps(context); + state->deviceUpdate(device, slot.Buffer); + slot.updateProps(context); } } slotlock.unlock(); @@ -1703,8 +1703,8 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) uint64_t usemask{~sublist.FreeMask}; while(usemask) { - const int idx{al::countr_zero(usemask)}; - ALsource *source{sublist.Sources + idx}; + const auto idx = static_cast(al::countr_zero(usemask)); + auto &source = (*sublist.Sources)[idx]; usemask &= ~(1_u64 << idx); auto clear_send = [](ALsource::SendData &send) -> void @@ -1718,10 +1718,10 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) send.GainLF = 1.0f; send.LFReference = HIGHPASSFREQREF; }; - auto send_begin = source->Send.begin() + static_cast(num_sends); - std::for_each(send_begin, source->Send.end(), clear_send); + auto send_begin = source.Send.begin() + static_cast(num_sends); + std::for_each(send_begin, source.Send.end(), clear_send); - source->mPropsDirty = true; + source.mPropsDirty = true; } } @@ -2905,7 +2905,13 @@ ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName) noexcep uint{DefaultSendCount} }; - DeviceRef device{new ALCdevice{DeviceType::Playback}}; + DeviceRef device{new(std::nothrow) ALCdevice{DeviceType::Playback}}; + if(!device) + { + WARN("Failed to create playback device handle\n"); + alcSetError(nullptr, ALC_OUT_OF_MEMORY); + return nullptr; + } /* Set output format */ device->FmtChans = DevFmtChannelsDefault; @@ -3040,7 +3046,13 @@ ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, else TRACE("Opening default capture device\n"); - DeviceRef device{new ALCdevice{DeviceType::Capture}}; + DeviceRef device{new(std::nothrow) ALCdevice{DeviceType::Capture}}; + if(!device) + { + WARN("Failed to create capture device handle\n"); + alcSetError(nullptr, ALC_OUT_OF_MEMORY); + return nullptr; + } auto decompfmt = DecomposeDevFormat(format); if(!decompfmt) @@ -3220,7 +3232,13 @@ ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceN uint{DefaultSendCount} }; - DeviceRef device{new ALCdevice{DeviceType::Loopback}}; + DeviceRef device{new(std::nothrow) ALCdevice{DeviceType::Loopback}}; + if(!device) + { + WARN("Failed to create loopback device handle\n"); + alcSetError(nullptr, ALC_OUT_OF_MEMORY); + return nullptr; + } device->SourcesMax = 256; device->AuxiliaryEffectSlotMax = 64; diff --git a/alc/backends/wave.cpp b/alc/backends/wave.cpp index 60cebd7f..985bb539 100644 --- a/alc/backends/wave.cpp +++ b/alc/backends/wave.cpp @@ -56,7 +56,7 @@ using ubyte = unsigned char; using ushort = unsigned short; struct FileDeleter { - void operator()(FILE *f) { fclose(f); } + void operator()(gsl::owner f) { fclose(f); } }; using FilePtr = std::unique_ptr; @@ -211,10 +211,10 @@ void WaveBackend::open(std::string_view name) #ifdef _WIN32 { std::wstring wname{utf8_to_wstr(fname.value())}; - mFile.reset(_wfopen(wname.c_str(), L"wb")); + mFile = FilePtr{_wfopen(wname.c_str(), L"wb")}; } #else - mFile.reset(fopen(fname->c_str(), "wb")); + mFile = FilePtr{fopen(fname->c_str(), "wb")}; #endif if(!mFile) throw al::backend_exception{al::backend_error::DeviceError, "Could not open file '%s': %s", diff --git a/alc/context.cpp b/alc/context.cpp index b5db03f9..0f6dbbae 100644 --- a/alc/context.cpp +++ b/alc/context.cpp @@ -338,10 +338,10 @@ void ForEachSource(ALCcontext *context, F func) uint64_t usemask{~sublist.FreeMask}; while(usemask) { - const int idx{al::countr_zero(usemask)}; + const auto idx = static_cast(al::countr_zero(usemask)); usemask &= ~(1_u64 << idx); - func(sublist.Sources[idx]); + func((*sublist.Sources)[idx]); } } } diff --git a/alc/context.h b/alc/context.h index d5e5e78b..d033c08e 100644 --- a/alc/context.h +++ b/alc/context.h @@ -1,6 +1,7 @@ #ifndef ALC_CONTEXT_H #define ALC_CONTEXT_H +#include #include #include #include @@ -70,7 +71,7 @@ struct DebugLogEntry { struct SourceSubList { uint64_t FreeMask{~0_u64}; - gsl::owner Sources{nullptr}; /* 64 */ + gsl::owner*> Sources{nullptr}; SourceSubList() noexcept = default; SourceSubList(const SourceSubList&) = delete; @@ -85,7 +86,7 @@ struct SourceSubList { struct EffectSlotSubList { uint64_t FreeMask{~0_u64}; - gsl::owner EffectSlots{nullptr}; /* 64 */ + gsl::owner*> EffectSlots{nullptr}; EffectSlotSubList() noexcept = default; EffectSlotSubList(const EffectSlotSubList&) = delete; diff --git a/alc/device.h b/alc/device.h index e5e9b3d2..fe9dff67 100644 --- a/alc/device.h +++ b/alc/device.h @@ -1,6 +1,7 @@ #ifndef ALC_DEVICE_H #define ALC_DEVICE_H +#include #include #include #include @@ -35,7 +36,7 @@ using uint = unsigned int; struct BufferSubList { uint64_t FreeMask{~0_u64}; - gsl::owner Buffers{nullptr}; /* 64 */ + gsl::owner*> Buffers{nullptr}; BufferSubList() noexcept = default; BufferSubList(const BufferSubList&) = delete; @@ -50,7 +51,7 @@ struct BufferSubList { struct EffectSubList { uint64_t FreeMask{~0_u64}; - gsl::owner Effects{nullptr}; /* 64 */ + gsl::owner*> Effects{nullptr}; /* 64 */ EffectSubList() noexcept = default; EffectSubList(const EffectSubList&) = delete; @@ -65,7 +66,7 @@ struct EffectSubList { struct FilterSubList { uint64_t FreeMask{~0_u64}; - gsl::owner Filters{nullptr}; /* 64 */ + gsl::owner*> Filters{nullptr}; FilterSubList() noexcept = default; FilterSubList(const FilterSubList&) = delete; diff --git a/core/mastering.cpp b/core/mastering.cpp index e9b079d6..5fecdb3f 100644 --- a/core/mastering.cpp +++ b/core/mastering.cpp @@ -318,10 +318,10 @@ std::unique_ptr Compressor::Create(const size_t NumChans, const floa const float PostGainDb, const float ThresholdDb, const float Ratio, const float KneeDb, const float AttackTime, const float ReleaseTime) { - const auto lookAhead = static_cast( - clampf(std::round(LookAheadTime*SampleRate), 0.0f, BufferLineSize-1)); - const auto hold = static_cast( - clampf(std::round(HoldTime*SampleRate), 0.0f, BufferLineSize-1)); + const auto lookAhead = static_cast(clampf(std::round(LookAheadTime*SampleRate), 0.0f, + BufferLineSize-1)); + const auto hold = static_cast(clampf(std::round(HoldTime*SampleRate), 0.0f, + BufferLineSize-1)); size_t size{sizeof(Compressor)}; if(lookAhead > 0) @@ -335,7 +335,10 @@ std::unique_ptr Compressor::Create(const size_t NumChans, const floa size += sizeof(*Compressor::mHold); } - auto Comp = CompressorPtr{al::construct_at(static_cast(al_calloc(16, size)))}; + gsl::owner storage{al_calloc(16, size)}; + if(!storage) throw std::bad_alloc{}; + + auto Comp = CompressorPtr{::new(storage) Compressor{}}; Comp->mNumChans = NumChans; Comp->mAuto.Knee = AutoKnee; Comp->mAuto.Attack = AutoAttack; diff --git a/utils/alsoft-config/mainwindow.cpp b/utils/alsoft-config/mainwindow.cpp index b2412c73..9a65f79c 100644 --- a/utils/alsoft-config/mainwindow.cpp +++ b/utils/alsoft-config/mainwindow.cpp @@ -20,6 +20,7 @@ #include #endif +#include "almalloc.h" #include "alspan.h" namespace { @@ -1283,11 +1284,10 @@ void MainWindow::addHrtfFile() void MainWindow::removeHrtfFile() { - QList selected{ui->hrtfFileList->selectedItems()}; + QList> selected{ui->hrtfFileList->selectedItems()}; if(!selected.isEmpty()) { - foreach(QListWidgetItem *item, selected) - delete item; + std::for_each(selected.begin(), selected.end(), std::default_delete{}); enableApplyButton(); } } @@ -1321,9 +1321,8 @@ void MainWindow::showEnabledBackendMenu(QPoint pt) QAction *gotAction{ctxmenu.exec(pt)}; if(gotAction == removeAction) { - QList selected{ui->enabledBackendList->selectedItems()}; - foreach(QListWidgetItem *item, selected) - delete item; + QList> selected{ui->enabledBackendList->selectedItems()}; + std::for_each(selected.begin(), selected.end(), std::default_delete{}); enableApplyButton(); } else if(gotAction != nullptr) @@ -1359,9 +1358,8 @@ void MainWindow::showDisabledBackendMenu(QPoint pt) QAction *gotAction{ctxmenu.exec(pt)}; if(gotAction == removeAction) { - QList selected{ui->disabledBackendList->selectedItems()}; - foreach(QListWidgetItem *item, selected) - delete item; + QList> selected{ui->disabledBackendList->selectedItems()}; + std::for_each(selected.begin(), selected.end(), std::default_delete{}); enableApplyButton(); } else if(gotAction != nullptr) diff --git a/utils/makemhr/makemhr.cpp b/utils/makemhr/makemhr.cpp index f14110c0..0a9b71e7 100644 --- a/utils/makemhr/makemhr.cpp +++ b/utils/makemhr/makemhr.cpp @@ -104,6 +104,11 @@ HrirDataT::~HrirDataT() = default; namespace { +struct FileDeleter { + void operator()(gsl::owner f) { fclose(f); } +}; +using FilePtr = std::unique_ptr; + using namespace std::placeholders; // The epsilon used to maintain signal stability. @@ -312,7 +317,6 @@ static int WriteAscii(const char *out, FILE *fp, const char *filename) len = strlen(out); if(fwrite(out, 1, len, fp) != len) { - fclose(fp); fprintf(stderr, "\nError: Bad write to file '%s'.\n", filename); return 0; } @@ -343,33 +347,33 @@ static int StoreMhr(const HrirDataT *hData, const char *filename) uint dither_seed{22222}; uint fi, ei, ai, i; - FILE *fp{fopen(filename, "wb")}; + FilePtr fp{fopen(filename, "wb")}; if(!fp) { fprintf(stderr, "\nError: Could not open MHR file '%s'.\n", filename); return 0; } - if(!WriteAscii(MHRFormat, fp, filename)) + if(!WriteAscii(MHRFormat, fp.get(), filename)) return 0; - if(!WriteBin4(4, hData->mIrRate, fp, filename)) + if(!WriteBin4(4, hData->mIrRate, fp.get(), filename)) return 0; - if(!WriteBin4(1, static_cast(hData->mChannelType), fp, filename)) + if(!WriteBin4(1, static_cast(hData->mChannelType), fp.get(), filename)) return 0; - if(!WriteBin4(1, hData->mIrPoints, fp, filename)) + if(!WriteBin4(1, hData->mIrPoints, fp.get(), filename)) return 0; - if(!WriteBin4(1, static_cast(hData->mFds.size()), fp, filename)) + if(!WriteBin4(1, static_cast(hData->mFds.size()), fp.get(), filename)) return 0; for(fi = static_cast(hData->mFds.size()-1);fi < hData->mFds.size();fi--) { auto fdist = static_cast(std::round(1000.0 * hData->mFds[fi].mDistance)); - if(!WriteBin4(2, fdist, fp, filename)) + if(!WriteBin4(2, fdist, fp.get(), filename)) return 0; - if(!WriteBin4(1, static_cast(hData->mFds[fi].mEvs.size()), fp, filename)) + if(!WriteBin4(1, static_cast(hData->mFds[fi].mEvs.size()), fp.get(), filename)) return 0; for(ei = 0;ei < hData->mFds[fi].mEvs.size();ei++) { const auto &elev = hData->mFds[fi].mEvs[ei]; - if(!WriteBin4(1, static_cast(elev.mAzs.size()), fp, filename)) + if(!WriteBin4(1, static_cast(elev.mAzs.size()), fp.get(), filename)) return 0; } } @@ -392,7 +396,7 @@ static int StoreMhr(const HrirDataT *hData, const char *filename) for(i = 0;i < (channels * n);i++) { const auto v = static_cast(Clamp(out[i], -scale-1.0, scale)); - if(!WriteBin4(bps, static_cast(v), fp, filename)) + if(!WriteBin4(bps, static_cast(v), fp.get(), filename)) return 0; } } @@ -407,16 +411,15 @@ static int StoreMhr(const HrirDataT *hData, const char *filename) for(const auto &azd : hData->mFds[fi].mEvs[ei].mAzs) { auto v = static_cast(std::round(azd.mDelays[0]*DelayPrecScale)); - if(!WriteBin4(1, v, fp, filename)) return 0; + if(!WriteBin4(1, v, fp.get(), filename)) return 0; if(hData->mChannelType == CT_STEREO) { v = static_cast(std::round(azd.mDelays[1]*DelayPrecScale)); - if(!WriteBin4(1, v, fp, filename)) return 0; + if(!WriteBin4(1, v, fp.get(), filename)) return 0; } } } } - fclose(fp); return 1; } diff --git a/utils/uhjdecoder.cpp b/utils/uhjdecoder.cpp index 8f0d2006..970d9f82 100644 --- a/utils/uhjdecoder.cpp +++ b/utils/uhjdecoder.cpp @@ -35,6 +35,7 @@ #include "albit.h" #include "alcomplex.h" +#include "almalloc.h" #include "alnumbers.h" #include "alspan.h" #include "vector.h" @@ -47,7 +48,7 @@ struct FileDeleter { - void operator()(FILE *file) { fclose(file); } + void operator()(gsl::owner file) { fclose(file); } }; using FilePtr = std::unique_ptr; -- cgit v1.2.3 From 37171b7d9afd571820bd168fdad596a42bf04561 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 29 Dec 2023 20:27:33 -0800 Subject: Replace some macros --- al/filter.cpp | 16 ++++++------ al/filter.h | 8 +++--- al/source.cpp | 24 ++++++++--------- alc/alc.cpp | 4 +-- common/pffft.cpp | 80 +++++++++++++++++++++++++++++++------------------------- 5 files changed, 70 insertions(+), 62 deletions(-) (limited to 'al/source.cpp') diff --git a/al/filter.cpp b/al/filter.cpp index 4a24a38f..0a999169 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -80,36 +80,36 @@ void InitFilterParams(ALfilter *filter, ALenum type) { filter->Gain = AL_LOWPASS_DEFAULT_GAIN; filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF; - filter->HFReference = LOWPASSFREQREF; + filter->HFReference = LowPassFreqRef; filter->GainLF = 1.0f; - filter->LFReference = HIGHPASSFREQREF; + filter->LFReference = HighPassFreqRef; filter->mTypeVariant.emplace(); } else if(type == AL_FILTER_HIGHPASS) { filter->Gain = AL_HIGHPASS_DEFAULT_GAIN; filter->GainHF = 1.0f; - filter->HFReference = LOWPASSFREQREF; + filter->HFReference = LowPassFreqRef; filter->GainLF = AL_HIGHPASS_DEFAULT_GAINLF; - filter->LFReference = HIGHPASSFREQREF; + filter->LFReference = HighPassFreqRef; filter->mTypeVariant.emplace(); } else if(type == AL_FILTER_BANDPASS) { filter->Gain = AL_BANDPASS_DEFAULT_GAIN; filter->GainHF = AL_BANDPASS_DEFAULT_GAINHF; - filter->HFReference = LOWPASSFREQREF; + filter->HFReference = LowPassFreqRef; filter->GainLF = AL_BANDPASS_DEFAULT_GAINLF; - filter->LFReference = HIGHPASSFREQREF; + filter->LFReference = HighPassFreqRef; filter->mTypeVariant.emplace(); } else { filter->Gain = 1.0f; filter->GainHF = 1.0f; - filter->HFReference = LOWPASSFREQREF; + filter->HFReference = LowPassFreqRef; filter->GainLF = 1.0f; - filter->LFReference = HIGHPASSFREQREF; + filter->LFReference = HighPassFreqRef; filter->mTypeVariant.emplace(); } filter->type = type; diff --git a/al/filter.h b/al/filter.h index 20d5e012..dffe8c6e 100644 --- a/al/filter.h +++ b/al/filter.h @@ -13,9 +13,9 @@ #include "almalloc.h" #include "alnumeric.h" -#define LOWPASSFREQREF 5000.0f -#define HIGHPASSFREQREF 250.0f +inline constexpr float LowPassFreqRef{5000.0f}; +inline constexpr float HighPassFreqRef{250.0f}; template struct FilterTable { @@ -41,9 +41,9 @@ struct ALfilter { float Gain{1.0f}; float GainHF{1.0f}; - float HFReference{LOWPASSFREQREF}; + float HFReference{LowPassFreqRef}; float GainLF{1.0f}; - float LFReference{HIGHPASSFREQREF}; + float LFReference{HighPassFreqRef}; using TableTypes = std::variant; diff --git a/al/source.cpp b/al/source.cpp index 1ac3bf28..af58379b 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -1796,9 +1796,9 @@ NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, con { Source->Direct.Gain = 1.0f; Source->Direct.GainHF = 1.0f; - Source->Direct.HFReference = LOWPASSFREQREF; + Source->Direct.HFReference = LowPassFreqRef; Source->Direct.GainLF = 1.0f; - Source->Direct.LFReference = HIGHPASSFREQREF; + Source->Direct.LFReference = HighPassFreqRef; } return UpdateSourceProps(Source, Context); } @@ -1954,9 +1954,9 @@ NOINLINE void SetProperty(ALsource *const Source, ALCcontext *const Context, con /* Disable filter */ send.Gain = 1.0f; send.GainHF = 1.0f; - send.HFReference = LOWPASSFREQREF; + send.HFReference = LowPassFreqRef; send.GainLF = 1.0f; - send.LFReference = HIGHPASSFREQREF; + send.LFReference = HighPassFreqRef; } /* We must force an update if the current auxiliary slot is valid @@ -3573,17 +3573,17 @@ ALsource::ALsource() { Direct.Gain = 1.0f; Direct.GainHF = 1.0f; - Direct.HFReference = LOWPASSFREQREF; + Direct.HFReference = LowPassFreqRef; Direct.GainLF = 1.0f; - Direct.LFReference = HIGHPASSFREQREF; + Direct.LFReference = HighPassFreqRef; for(auto &send : Send) { send.Slot = nullptr; send.Gain = 1.0f; send.GainHF = 1.0f; - send.HFReference = LOWPASSFREQREF; + send.HFReference = LowPassFreqRef; send.GainLF = 1.0f; - send.LFReference = HIGHPASSFREQREF; + send.LFReference = HighPassFreqRef; } } @@ -4078,9 +4078,9 @@ void ALsource::eax_update_direct_filter() const auto& direct_param = eax_create_direct_filter_param(); Direct.Gain = direct_param.gain; Direct.GainHF = direct_param.gain_hf; - Direct.HFReference = LOWPASSFREQREF; + Direct.HFReference = LowPassFreqRef; Direct.GainLF = 1.0f; - Direct.LFReference = HIGHPASSFREQREF; + Direct.LFReference = HighPassFreqRef; mPropsDirty = true; } @@ -4854,9 +4854,9 @@ void ALsource::eax_set_al_source_send(ALeffectslot *slot, size_t sendidx, const auto &send = Send[sendidx]; send.Gain = filter.gain; send.GainHF = filter.gain_hf; - send.HFReference = LOWPASSFREQREF; + send.HFReference = LowPassFreqRef; send.GainLF = 1.0f; - send.LFReference = HIGHPASSFREQREF; + send.LFReference = HighPassFreqRef; if(slot != nullptr) IncrementRef(slot->ref); diff --git a/alc/alc.cpp b/alc/alc.cpp index ece65430..af68e4d0 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -1714,9 +1714,9 @@ ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList) send.Slot = nullptr; send.Gain = 1.0f; send.GainHF = 1.0f; - send.HFReference = LOWPASSFREQREF; + send.HFReference = LowPassFreqRef; send.GainLF = 1.0f; - send.LFReference = HIGHPASSFREQREF; + send.LFReference = HighPassFreqRef; }; auto send_begin = source.Send.begin() + static_cast(num_sends); std::for_each(send_begin, source.Send.end(), clear_send); diff --git a/common/pffft.cpp b/common/pffft.cpp index 38380261..46d97918 100644 --- a/common/pffft.cpp +++ b/common/pffft.cpp @@ -92,7 +92,7 @@ using uint = unsigned int; */ #if defined(__ppc__) || defined(__ppc64__) || defined(__powerpc__) || defined(__powerpc64__) using v4sf = vector float; -#define SIMD_SZ 4 +constexpr uint SimdSize{4}; #define VZERO() ((vector float) vec_splat_u8(0)) #define VMUL(a,b) vec_madd(a,b, VZERO()) #define VADD vec_add @@ -144,18 +144,26 @@ force_inline void vtranspose4(v4sf &x0, v4sf &x1, v4sf &x2, v4sf &x3) noexcept #include using v4sf = __m128; -#define SIMD_SZ 4 // 4 floats by simd vector -- this is pretty much hardcoded in the preprocess/finalize functions anyway so you will have to work if you want to enable AVX with its 256-bit vectors. +/* 4 floats by simd vector -- this is pretty much hardcoded in the preprocess/ + * finalize functions anyway so you will have to work if you want to enable AVX + * with its 256-bit vectors. + */ +constexpr uint SimdSize{4}; #define VZERO _mm_setzero_ps #define VMUL _mm_mul_ps #define VADD _mm_add_ps -#define VMADD(a,b,c) _mm_add_ps(_mm_mul_ps(a,b), c) +force_inline v4sf vmadd(const v4sf a, const v4sf b, const v4sf c) noexcept +{ return _mm_add_ps(_mm_mul_ps(a,b), c); } +#define VMADD vmadd #define VSUB _mm_sub_ps #define LD_PS1 _mm_set1_ps #define VSET4 _mm_setr_ps -#define VINSERT0(v, a) _mm_move_ss((v), _mm_set_ss(a)) +force_inline v4sf vinsert0(const v4sf v, const float a) noexcept +{ return _mm_move_ss(v, _mm_set_ss(a)); } +#define VINSERT0 vinsert0 #define VEXTRACT0 _mm_cvtss_f32 -force_inline void interleave2(v4sf in1, v4sf in2, v4sf &out1, v4sf &out2) noexcept +force_inline void interleave2(const v4sf in1, const v4sf in2, v4sf &out1, v4sf &out2) noexcept { v4sf tmp{_mm_unpacklo_ps(in1, in2)}; out2 = _mm_unpackhi_ps(in1, in2); @@ -171,7 +179,7 @@ force_inline void uninterleave2(v4sf in1, v4sf in2, v4sf &out1, v4sf &out2) noex force_inline void vtranspose4(v4sf &x0, v4sf &x1, v4sf &x2, v4sf &x3) noexcept { _MM_TRANSPOSE4_PS(x0, x1, x2, x3); } -#define VSWAPHL(a,b) _mm_shuffle_ps(b, a, _MM_SHUFFLE(3,2,1,0)) +#define VSWAPHL(a,b) _mm_shuffle_ps((b), (a), _MM_SHUFFLE(3,2,1,0)) /* * ARM NEON support macros @@ -180,7 +188,7 @@ force_inline void vtranspose4(v4sf &x0, v4sf &x1, v4sf &x2, v4sf &x3) noexcept #include using v4sf = float32x4_t; -#define SIMD_SZ 4 +constexpr uint SimdSize{4}; #define VZERO() vdupq_n_f32(0) #define VMUL vmulq_f32 #define VADD vaddq_f32 @@ -239,7 +247,7 @@ force_inline void vtranspose4(v4sf &x0, v4sf &x1, v4sf &x2, v4sf &x3) noexcept #elif defined(__GNUC__) using v4sf [[gnu::vector_size(16), gnu::aligned(16)]] = float; -#define SIMD_SZ 4 +constexpr uint SimdSize{4}; #define VZERO() v4sf{0,0,0,0} #define VMUL(a,b) ((a) * (b)) #define VADD(a,b) ((a) + (b)) @@ -299,7 +307,7 @@ force_inline v4sf vswaphl(v4sf a, v4sf b) noexcept // fallback mode for situations where SIMD is not available, use scalar mode instead #ifdef PFFFT_SIMD_DISABLE using v4sf = float; -#define SIMD_SZ 1 +constexpr uint SimdSize{1}; #define VZERO() 0.f #define VMUL(a,b) ((a)*(b)) #define VADD(a,b) ((a)+(b)) @@ -310,7 +318,7 @@ using v4sf = float; inline bool valigned(const float *ptr) noexcept { - static constexpr uintptr_t alignmask{SIMD_SZ*4 - 1}; + static constexpr uintptr_t alignmask{SimdSize*4 - 1}; return (reinterpret_cast(ptr) & alignmask) == 0; } @@ -1430,11 +1438,11 @@ gsl::owner pffft_new_setup(unsigned int N, pffft_transform_t trans * handle other cases (or maybe just switch to a scalar fft, I don't know..) */ if(transform == PFFFT_REAL) - assert((N%(2*SIMD_SZ*SIMD_SZ)) == 0); + assert((N%(2*SimdSize*SimdSize)) == 0); else - assert((N%(SIMD_SZ*SIMD_SZ)) == 0); + assert((N%(SimdSize*SimdSize)) == 0); - const uint Ncvec{(transform == PFFFT_REAL ? N/2 : N)/SIMD_SZ}; + const uint Ncvec{(transform == PFFFT_REAL ? N/2 : N) / SimdSize}; const size_t storelen{std::max(offsetof(PFFFT_Setup, end) + 2_zu*Ncvec*sizeof(v4sf), sizeof(PFFFT_Setup))}; @@ -1446,37 +1454,37 @@ gsl::owner pffft_new_setup(unsigned int N, pffft_transform_t trans s->transform = transform; s->Ncvec = Ncvec; - const size_t ecount{2_zu*Ncvec*(SIMD_SZ-1)/SIMD_SZ}; + const size_t ecount{2_zu*Ncvec*(SimdSize-1)/SimdSize}; s->e = {std::launder(reinterpret_cast(extrastore.data())), ecount}; s->twiddle = std::launder(reinterpret_cast(&extrastore[ecount*sizeof(v4sf)])); - if constexpr(SIMD_SZ > 1) + if constexpr(SimdSize > 1) { - auto e = std::vector(s->e.size()*SIMD_SZ, 0.0f); + auto e = std::vector(s->e.size()*SimdSize, 0.0f); for(size_t k{0};k < s->Ncvec;++k) { - const size_t i{k / SIMD_SZ}; - const size_t j{k % SIMD_SZ}; - for(size_t m{0};m < SIMD_SZ-1;++m) + const size_t i{k / SimdSize}; + const size_t j{k % SimdSize}; + for(size_t m{0};m < SimdSize-1;++m) { const double A{-2.0*al::numbers::pi*static_cast((m+1)*k) / N}; - e[((i*3 + m)*2 + 0)*SIMD_SZ + j] = static_cast(std::cos(A)); - e[((i*3 + m)*2 + 1)*SIMD_SZ + j] = static_cast(std::sin(A)); + e[((i*3 + m)*2 + 0)*SimdSize + j] = static_cast(std::cos(A)); + e[((i*3 + m)*2 + 1)*SimdSize + j] = static_cast(std::sin(A)); } } std::memcpy(s->e.data(), e.data(), e.size()*sizeof(float)); } if(transform == PFFFT_REAL) - rffti1_ps(N/SIMD_SZ, s->twiddle, s->ifac); + rffti1_ps(N/SimdSize, s->twiddle, s->ifac); else - cffti1_ps(N/SIMD_SZ, s->twiddle, s->ifac); + cffti1_ps(N/SimdSize, s->twiddle, s->ifac); /* check that N is decomposable with allowed prime factors */ size_t m{1}; for(size_t k{0};k < s->ifac[1];++k) m *= s->ifac[2+k]; - if(m != N/SIMD_SZ) + if(m != N/SimdSize) { pffft_destroy_setup(s); s = nullptr; @@ -1539,7 +1547,7 @@ void pffft_cplx_finalize(const size_t Ncvec, const v4sf *in, v4sf *RESTRICT out, { assert(in != out); - const size_t dk{Ncvec/SIMD_SZ}; // number of 4x4 matrix blocks + const size_t dk{Ncvec/SimdSize}; // number of 4x4 matrix blocks for(size_t k{0};k < dk;++k) { v4sf r0{in[8*k+0]}, i0{in[8*k+1]}; @@ -1583,7 +1591,7 @@ void pffft_cplx_preprocess(const size_t Ncvec, const v4sf *in, v4sf *RESTRICT ou { assert(in != out); - const size_t dk{Ncvec/SIMD_SZ}; // number of 4x4 matrix blocks + const size_t dk{Ncvec/SimdSize}; // number of 4x4 matrix blocks for(size_t k{0};k < dk;++k) { v4sf r0{in[8*k+0]}, i0{in[8*k+1]}; @@ -1676,12 +1684,12 @@ NOINLINE void pffft_real_finalize(const size_t Ncvec, const v4sf *in, v4sf *REST static constexpr float s{al::numbers::sqrt2_v/2.0f}; assert(in != out); - const size_t dk{Ncvec/SIMD_SZ}; // number of 4x4 matrix blocks + const size_t dk{Ncvec/SimdSize}; // number of 4x4 matrix blocks /* fftpack order is f0r f1r f1i f2r f2i ... f(n-1)r f(n-1)i f(n)r */ const v4sf zero{VZERO()}; - const auto cr = al::bit_cast>(in[0]); - const auto ci = al::bit_cast>(in[Ncvec*2-1]); + const auto cr = al::bit_cast>(in[0]); + const auto ci = al::bit_cast>(in[Ncvec*2-1]); pffft_real_finalize_4x4(&zero, &zero, in+1, e, out); /* [cr0 cr1 cr2 cr3 ci0 ci1 ci2 ci3] @@ -1767,11 +1775,11 @@ NOINLINE void pffft_real_preprocess(const size_t Ncvec, const v4sf *in, v4sf *RE static constexpr float sqrt2{al::numbers::sqrt2_v}; assert(in != out); - const size_t dk{Ncvec/SIMD_SZ}; // number of 4x4 matrix blocks + const size_t dk{Ncvec/SimdSize}; // number of 4x4 matrix blocks /* fftpack order is f0r f1r f1i f2r f2i ... f(n-1)r f(n-1)i f(n)r */ - std::array Xr, Xi; - for(size_t k{0};k < SIMD_SZ;++k) + std::array Xr, Xi; + for(size_t k{0};k < SimdSize;++k) { Xr[k] = VEXTRACT0(in[2*k]); Xi[k] = VEXTRACT0(in[2*k + 1]); @@ -1901,8 +1909,8 @@ void pffft_zreorder(const PFFFT_Setup *setup, const float *in, float *out, interleave2(vin[k*8 + 0], vin[k*8 + 1], vout[2*(0*dk + k) + 0], vout[2*(0*dk + k) + 1]); interleave2(vin[k*8 + 4], vin[k*8 + 5], vout[2*(2*dk + k) + 0], vout[2*(2*dk + k) + 1]); } - reversed_copy(dk, vin+2, 8, vout + N/SIMD_SZ/2); - reversed_copy(dk, vin+6, 8, vout + N/SIMD_SZ); + reversed_copy(dk, vin+2, 8, vout + N/SimdSize/2); + reversed_copy(dk, vin+6, 8, vout + N/SimdSize); } else { @@ -1911,8 +1919,8 @@ void pffft_zreorder(const PFFFT_Setup *setup, const float *in, float *out, uninterleave2(vin[2*(0*dk + k) + 0], vin[2*(0*dk + k) + 1], vout[k*8 + 0], vout[k*8 + 1]); uninterleave2(vin[2*(2*dk + k) + 0], vin[2*(2*dk + k) + 1], vout[k*8 + 4], vout[k*8 + 5]); } - unreversed_copy(dk, vin + N/SIMD_SZ/4, vout + N/SIMD_SZ - 6, -8); - unreversed_copy(dk, vin + 3*N/SIMD_SZ/4, vout + N/SIMD_SZ - 2, -8); + unreversed_copy(dk, vin + N/SimdSize/4, vout + N/SimdSize - 6, -8); + unreversed_copy(dk, vin + 3_uz*N/SimdSize/4, vout + N/SimdSize - 2, -8); } } else -- cgit v1.2.3 From 2f2edb326128c56e269a171961d991d8d7936e4f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 1 Jan 2024 00:39:33 -0800 Subject: Use standard operator new[] and delete[] for aligned allocations --- CMakeLists.txt | 2 -- al/auxeffectslot.cpp | 2 +- al/buffer.cpp | 2 +- al/effect.cpp | 2 +- al/filter.cpp | 2 +- al/source.cpp | 2 +- common/almalloc.cpp | 50 ++------------------------------------------------ common/almalloc.h | 5 ++--- config.h.in | 6 ------ 9 files changed, 9 insertions(+), 64 deletions(-) (limited to 'al/source.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index f53236fb..14ee18f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -565,8 +565,6 @@ if(HAVE_INTRIN_H) }" HAVE_CPUID_INTRINSIC) endif() -check_symbol_exists(posix_memalign stdlib.h HAVE_POSIX_MEMALIGN) -check_symbol_exists(_aligned_malloc malloc.h HAVE__ALIGNED_MALLOC) check_symbol_exists(proc_pidpath libproc.h HAVE_PROC_PIDPATH) if(NOT WIN32) diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index 695c5788..d95748d7 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -1004,7 +1004,7 @@ EffectSlotSubList::~EffectSlotSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(EffectSlots); + al_free(alignof(ALeffectslot), EffectSlots); EffectSlots = nullptr; } diff --git a/al/buffer.cpp b/al/buffer.cpp index 46ef8ece..646ec8ea 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -1490,7 +1490,7 @@ BufferSubList::~BufferSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(Buffers); + al_free(alignof(ALbuffer), Buffers); Buffers = nullptr; } diff --git a/al/effect.cpp b/al/effect.cpp index 1024de80..2f5422fd 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -572,7 +572,7 @@ EffectSubList::~EffectSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(Effects); + al_free(alignof(ALeffect), Effects); Effects = nullptr; } diff --git a/al/filter.cpp b/al/filter.cpp index 0a999169..b67a65f7 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -700,6 +700,6 @@ FilterSubList::~FilterSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(Filters); + al_free(alignof(ALfilter), Filters); Filters = nullptr; } diff --git a/al/source.cpp b/al/source.cpp index af58379b..425acbfa 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -3643,7 +3643,7 @@ SourceSubList::~SourceSubList() std::destroy_at(al::to_address(Sources->begin() + idx)); } FreeMask = ~usemask; - al_free(Sources); + al_free(alignof(ALsource), Sources); Sources = nullptr; } diff --git a/common/almalloc.cpp b/common/almalloc.cpp index 71953d8b..2249b988 100644 --- a/common/almalloc.cpp +++ b/common/almalloc.cpp @@ -3,59 +3,13 @@ #include "almalloc.h" -#include -#include -#include +#include #include -#include -#ifdef HAVE_MALLOC_H -#include -#endif -gsl::owner al_malloc(size_t alignment, size_t size) -{ - assert((alignment & (alignment-1)) == 0); - alignment = std::max(alignment, alignof(std::max_align_t)); - -#if defined(HAVE_POSIX_MEMALIGN) - gsl::owner ret{}; - if(posix_memalign(&ret, alignment, size) == 0) - return ret; - return nullptr; -#elif defined(HAVE__ALIGNED_MALLOC) - return _aligned_malloc(size, alignment); -#else - size_t total_size{size + alignment-1 + sizeof(void*)}; - void *base{std::malloc(total_size)}; - if(base != nullptr) - { - void *aligned_ptr{static_cast(base) + sizeof(void*)}; - total_size -= sizeof(void*); - - std::align(alignment, size, aligned_ptr, total_size); - *(static_cast(aligned_ptr)-1) = base; - base = aligned_ptr; - } - return base; -#endif -} - gsl::owner al_calloc(size_t alignment, size_t size) { - gsl::owner ret{al_malloc(alignment, size)}; + gsl::owner ret{::operator new[](size, std::align_val_t{alignment}, std::nothrow)}; if(ret) std::memset(ret, 0, size); return ret; } - -void al_free(gsl::owner ptr) noexcept -{ -#if defined(HAVE_POSIX_MEMALIGN) - std::free(ptr); -#elif defined(HAVE__ALIGNED_MALLOC) - _aligned_free(ptr); -#else - if(ptr != nullptr) - std::free(*(static_cast*>(ptr) - 1)); -#endif -} diff --git a/common/almalloc.h b/common/almalloc.h index 82f050e4..a03a0f43 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -17,9 +17,8 @@ namespace gsl { template using owner = T; }; -void al_free(gsl::owner ptr) noexcept; -[[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]] -gsl::owner al_malloc(size_t alignment, size_t size); +inline void al_free(size_t alignment, gsl::owner ptr) noexcept +{ ::operator delete[](ptr, std::align_val_t{alignment}); } [[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]] gsl::owner al_calloc(size_t alignment, size_t size); diff --git a/config.h.in b/config.h.in index 20df5b46..19cb79e0 100644 --- a/config.h.in +++ b/config.h.in @@ -7,12 +7,6 @@ /* Define if HRTF data is embedded in the library */ #cmakedefine ALSOFT_EMBED_HRTF_DATA -/* Define if we have the posix_memalign function */ -#cmakedefine HAVE_POSIX_MEMALIGN - -/* Define if we have the _aligned_malloc function */ -#cmakedefine HAVE__ALIGNED_MALLOC - /* Define if we have the proc_pidpath function */ #cmakedefine HAVE_PROC_PIDPATH -- cgit v1.2.3 From 45fca6a301a6d424bd0d856d0ae29ce56c5dc9ac Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 1 Jan 2024 05:11:53 -0800 Subject: Use an allocator to allocate uninitilized sublists --- CMakeLists.txt | 1 - al/auxeffectslot.cpp | 31 ++++++++++++++++--------------- al/buffer.cpp | 31 ++++++++++++++++--------------- al/effect.cpp | 31 ++++++++++++++++--------------- al/filter.cpp | 31 ++++++++++++++++--------------- al/source.cpp | 31 +++++++++++++++---------------- common/almalloc.cpp | 15 --------------- common/almalloc.h | 19 +++++++------------ 8 files changed, 86 insertions(+), 104 deletions(-) delete mode 100644 common/almalloc.cpp (limited to 'al/source.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 14ee18f5..b4ced226 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -610,7 +610,6 @@ set(COMMON_OBJS common/alcomplex.h common/alfstream.cpp common/alfstream.h - common/almalloc.cpp common/almalloc.h common/alnumbers.h common/alnumeric.h diff --git a/al/auxeffectslot.cpp b/al/auxeffectslot.cpp index d95748d7..5eea3425 100644 --- a/al/auxeffectslot.cpp +++ b/al/auxeffectslot.cpp @@ -53,6 +53,8 @@ namespace { +using SubListAllocator = typename al::allocator>; + struct FactoryItem { EffectSlotType Type; EffectStateFactory* (&GetFactory)(); @@ -238,22 +240,21 @@ bool EnsureEffectSlots(ALCcontext *context, size_t needed) [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t { return cur + static_cast(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(context->mEffectSlotList.size() >= 1<<25) UNLIKELY - return false; - - context->mEffectSlotList.emplace_back(); - auto sublist = context->mEffectSlotList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->EffectSlots = static_cast*>>( - al_calloc(alignof(ALeffectslot), sizeof(*sublist->EffectSlots))); - if(!sublist->EffectSlots) UNLIKELY + try { + while(needed > count) { - context->mEffectSlotList.pop_back(); - return false; + if(context->mEffectSlotList.size() >= 1<<25) UNLIKELY + return false; + + EffectSlotSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.EffectSlots = SubListAllocator{}.allocate(1); + context->mEffectSlotList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -1004,7 +1005,7 @@ EffectSlotSubList::~EffectSlotSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALeffectslot), EffectSlots); + SubListAllocator{}.deallocate(EffectSlots, 1); EffectSlots = nullptr; } diff --git a/al/buffer.cpp b/al/buffer.cpp index 646ec8ea..2aafbf2d 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -68,6 +68,8 @@ namespace { +using SubListAllocator = typename al::allocator>; + std::optional AmbiLayoutFromEnum(ALenum layout) { switch(layout) @@ -178,22 +180,21 @@ bool EnsureBuffers(ALCdevice *device, size_t needed) [](size_t cur, const BufferSubList &sublist) noexcept -> size_t { return cur + static_cast(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(device->BufferList.size() >= 1<<25) UNLIKELY - return false; - - device->BufferList.emplace_back(); - auto sublist = device->BufferList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Buffers = static_cast*>>(al_calloc( - alignof(ALbuffer), sizeof(*sublist->Buffers))); - if(!sublist->Buffers) UNLIKELY + try { + while(needed > count) { - device->BufferList.pop_back(); - return false; + if(device->BufferList.size() >= 1<<25) UNLIKELY + return false; + + BufferSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Buffers = SubListAllocator{}.allocate(1); + device->BufferList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -1490,7 +1491,7 @@ BufferSubList::~BufferSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALbuffer), Buffers); + SubListAllocator{}.deallocate(Buffers, 1); Buffers = nullptr; } diff --git a/al/effect.cpp b/al/effect.cpp index 2f5422fd..071b32c6 100644 --- a/al/effect.cpp +++ b/al/effect.cpp @@ -89,6 +89,8 @@ effect_exception::~effect_exception() = default; namespace { +using SubListAllocator = typename al::allocator>; + auto GetDefaultProps(ALenum type) -> const EffectProps& { switch(type) @@ -126,22 +128,21 @@ bool EnsureEffects(ALCdevice *device, size_t needed) [](size_t cur, const EffectSubList &sublist) noexcept -> size_t { return cur + static_cast(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(device->EffectList.size() >= 1<<25) UNLIKELY - return false; - - device->EffectList.emplace_back(); - auto sublist = device->EffectList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Effects = static_cast*>>(al_calloc( - alignof(ALeffect), sizeof(*sublist->Effects))); - if(!sublist->Effects) UNLIKELY + try { + while(needed > count) { - device->EffectList.pop_back(); - return false; + if(device->EffectList.size() >= 1<<25) UNLIKELY + return false; + + EffectSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Effects = SubListAllocator{}.allocate(1); + device->EffectList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -572,7 +573,7 @@ EffectSubList::~EffectSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALeffect), Effects); + SubListAllocator{}.deallocate(Effects, 1); Effects = nullptr; } diff --git a/al/filter.cpp b/al/filter.cpp index b67a65f7..528d6059 100644 --- a/al/filter.cpp +++ b/al/filter.cpp @@ -49,6 +49,8 @@ namespace { +using SubListAllocator = typename al::allocator>; + class filter_exception final : public al::base_exception { ALenum mErrorCode; @@ -121,22 +123,21 @@ bool EnsureFilters(ALCdevice *device, size_t needed) [](size_t cur, const FilterSubList &sublist) noexcept -> size_t { return cur + static_cast(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(device->FilterList.size() >= 1<<25) UNLIKELY - return false; - - device->FilterList.emplace_back(); - auto sublist = device->FilterList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Filters = static_cast*>>(al_calloc( - alignof(ALfilter), sizeof(*sublist->Filters))); - if(!sublist->Filters) UNLIKELY + try { + while(needed > count) { - device->FilterList.pop_back(); - return false; + if(device->FilterList.size() >= 1<<25) UNLIKELY + return false; + + FilterSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Filters = SubListAllocator{}.allocate(1); + device->FilterList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -700,6 +701,6 @@ FilterSubList::~FilterSubList() usemask &= ~(1_u64 << idx); } FreeMask = ~usemask; - al_free(alignof(ALfilter), Filters); + SubListAllocator{}.deallocate(Filters, 1); Filters = nullptr; } diff --git a/al/source.cpp b/al/source.cpp index 425acbfa..27144389 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -80,7 +80,7 @@ namespace { -using namespace std::placeholders; +using SubListAllocator = typename al::allocator>; using std::chrono::nanoseconds; using seconds_d = std::chrono::duration; @@ -721,22 +721,21 @@ bool EnsureSources(ALCcontext *context, size_t needed) [](size_t cur, const SourceSubList &sublist) noexcept -> size_t { return cur + static_cast(al::popcount(sublist.FreeMask)); })}; - while(needed > count) - { - if(context->mSourceList.size() >= 1<<25) UNLIKELY - return false; - - context->mSourceList.emplace_back(); - auto sublist = context->mSourceList.end() - 1; - sublist->FreeMask = ~0_u64; - sublist->Sources = static_cast*>>(al_calloc( - alignof(ALsource), sizeof(*sublist->Sources))); - if(!sublist->Sources) UNLIKELY + try { + while(needed > count) { - context->mSourceList.pop_back(); - return false; + if(context->mSourceList.size() >= 1<<25) UNLIKELY + return false; + + SourceSubList sublist{}; + sublist.FreeMask = ~0_u64; + sublist.Sources = SubListAllocator{}.allocate(1); + context->mSourceList.emplace_back(std::move(sublist)); + count += 64; } - count += 64; + } + catch(...) { + return false; } return true; } @@ -3643,7 +3642,7 @@ SourceSubList::~SourceSubList() std::destroy_at(al::to_address(Sources->begin() + idx)); } FreeMask = ~usemask; - al_free(alignof(ALsource), Sources); + SubListAllocator{}.deallocate(Sources, 1); Sources = nullptr; } diff --git a/common/almalloc.cpp b/common/almalloc.cpp deleted file mode 100644 index 2249b988..00000000 --- a/common/almalloc.cpp +++ /dev/null @@ -1,15 +0,0 @@ - -#include "config.h" - -#include "almalloc.h" - -#include -#include - - -gsl::owner al_calloc(size_t alignment, size_t size) -{ - gsl::owner ret{::operator new[](size, std::align_val_t{alignment}, std::nothrow)}; - if(ret) std::memset(ret, 0, size); - return ret; -} diff --git a/common/almalloc.h b/common/almalloc.h index a03a0f43..3b9965e6 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -17,11 +17,6 @@ namespace gsl { template using owner = T; }; -inline void al_free(size_t alignment, gsl::owner ptr) noexcept -{ ::operator delete[](ptr, std::align_val_t{alignment}); } -[[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]] -gsl::owner al_calloc(size_t alignment, size_t size); - #define DISABLE_ALLOC \ void *operator new(size_t) = delete; \ @@ -61,18 +56,18 @@ struct allocator { static constexpr auto Alignment = std::max(AlignV, alignof(T)); static constexpr auto AlignVal = std::align_val_t{Alignment}; - using value_type = T; - using reference = T&; - using const_reference = const T&; - using pointer = T*; - using const_pointer = const T*; + using value_type = std::remove_cv_t>; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type*; + using const_pointer = const value_type*; using size_type = std::size_t; using difference_type = std::ptrdiff_t; using is_always_equal = std::true_type; - template + template = true> struct rebind { - using other = std::enable_if_t>; + using other = allocator; }; constexpr explicit allocator() noexcept = default; -- cgit v1.2.3 From 744bd1e289502f3ab413a4026f3dc9a6b6e07a72 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 3 Jan 2024 19:46:59 -0800 Subject: Avoid redundant definitions --- al/source.cpp | 3 --- alc/alc.cpp | 5 +++-- alc/backends/sdl2.cpp | 26 ++++++++++++++++---------- alc/context.h | 1 - 4 files changed, 19 insertions(+), 16 deletions(-) (limited to 'al/source.cpp') diff --git a/al/source.cpp b/al/source.cpp index 27144389..b3af9428 100644 --- a/al/source.cpp +++ b/al/source.cpp @@ -3648,9 +3648,6 @@ SourceSubList::~SourceSubList() #ifdef ALSOFT_EAX -constexpr const ALsource::EaxFxSlotIds ALsource::eax4_fx_slot_ids; -constexpr const ALsource::EaxFxSlotIds ALsource::eax5_fx_slot_ids; - void ALsource::eaxInitialize(ALCcontext *context) noexcept { assert(context != nullptr); diff --git a/alc/alc.cpp b/alc/alc.cpp index b63317e7..b629a3fc 100644 --- a/alc/alc.cpp +++ b/alc/alc.cpp @@ -2057,7 +2057,7 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum para ProbeAllDevicesList(); /* Copy first entry as default. */ - alcDefaultAllDevicesSpecifier = alcAllDevicesList.c_str(); + alcDefaultAllDevicesSpecifier = alcAllDevicesList.substr(0, alcAllDevicesList.find('\0')); value = alcDefaultAllDevicesSpecifier.c_str(); break; @@ -2066,7 +2066,8 @@ ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum para ProbeCaptureDeviceList(); /* Copy first entry as default. */ - alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList.c_str(); + alcCaptureDefaultDeviceSpecifier = alcCaptureDeviceList.substr(0, + alcCaptureDeviceList.find('\0')); value = alcCaptureDefaultDeviceSpecifier.c_str(); break; diff --git a/alc/backends/sdl2.cpp b/alc/backends/sdl2.cpp index 49b9713e..b69f17fd 100644 --- a/alc/backends/sdl2.cpp +++ b/alc/backends/sdl2.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include "almalloc.h" #include "alnumeric.h" @@ -46,8 +47,9 @@ namespace { #define DEVNAME_PREFIX "" #endif -/* NOLINTNEXTLINE(*-avoid-c-arrays) */ -constexpr char defaultDeviceName[]{DEVNAME_PREFIX "Default Device"}; +constexpr auto getDevicePrefix() noexcept -> std::string_view { return DEVNAME_PREFIX; } +constexpr auto getDefaultDeviceName() noexcept -> std::string_view +{ return DEVNAME_PREFIX "Default Device"; } struct Sdl2Backend final : public BackendBase { Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { } @@ -107,6 +109,7 @@ void Sdl2Backend::open(std::string_view name) /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't * necessarily the first in the list. */ + const auto defaultDeviceName = getDefaultDeviceName(); SDL_AudioDeviceID devid; if(name.empty() || name == defaultDeviceName) { @@ -115,13 +118,13 @@ void Sdl2Backend::open(std::string_view name) } else { - const size_t prefix_len = strlen(DEVNAME_PREFIX); - if(name.length() >= prefix_len && strncmp(name.data(), DEVNAME_PREFIX, prefix_len) == 0) + const auto namePrefix = getDevicePrefix(); + if(name.size() >= namePrefix.size() && name.substr(0, namePrefix.size()) == namePrefix) { /* Copy the string_view to a string to ensure it's null terminated * for this call. */ - const std::string devname{name.substr(prefix_len)}; + const std::string devname{name.substr(namePrefix.size())}; devid = SDL_OpenAudioDevice(devname.c_str(), SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE); } @@ -216,13 +219,16 @@ std::string SDL2BackendFactory::probe(BackendType type) int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)}; /* Includes null char. */ - outnames.append(defaultDeviceName, sizeof(defaultDeviceName)); + outnames += getDefaultDeviceName(); + outnames += '\0'; for(int i{0};i < num_devices;++i) { - std::string name{DEVNAME_PREFIX}; - name += SDL_GetAudioDeviceName(i, SDL_FALSE); - if(!name.empty()) - outnames.append(name.c_str(), name.length()+1); + outnames += getDevicePrefix(); + if(const char *name = SDL_GetAudioDeviceName(i, SDL_FALSE)) + outnames += name; + else + outnames += "Unknown Device Name #"+std::to_string(i); + outnames += '\0'; } return outnames; } diff --git a/alc/context.h b/alc/context.h index 86ef140d..e27d10d3 100644 --- a/alc/context.h +++ b/alc/context.h @@ -203,7 +203,6 @@ public: static ALeffect sDefaultEffect; #ifdef ALSOFT_EAX -public: bool hasEax() const noexcept { return mEaxIsInitialized; } bool eaxIsCapable() const noexcept; -- cgit v1.2.3