Use a standard bitset for bitfield flags

This commit is contained in:
Chris Robinson
2021-01-22 00:00:10 -08:00
parent 5729e1004d
commit 5ff5fd8ecc
9 changed files with 56 additions and 100 deletions
+41 -39
View File
@@ -1804,9 +1804,9 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
/* If a context is already running on the device, stop playback so the
* device attributes can be updated.
*/
if(device->Flags.get<DeviceRunning>())
if(device->Flags.test(DeviceRunning))
device->Backend->stop();
device->Flags.unset<DeviceRunning>();
device->Flags.reset(DeviceRunning);
UpdateClockBase(device);
@@ -1833,7 +1833,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
freq = ConfigValueUInt(devname, nullptr, "frequency").value_or(freq);
if(freq < 1)
device->Flags.unset<FrequencyRequest>();
device->Flags.reset(FrequencyRequest);
else
{
freq = clampu(freq, MIN_OUTPUT_RATE, MAX_OUTPUT_RATE);
@@ -1843,7 +1843,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
device->BufferSize = static_cast<uint>(device->BufferSize*scale + 0.5);
device->Frequency = freq;
device->Flags.set<FrequencyRequest>();
device->Flags.set(FrequencyRequest);
}
if(auto persizeopt = ConfigValueUInt(devname, nullptr, "period_size"))
@@ -1878,7 +1878,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
new_sends = numSends;
}
if(device->Flags.get<DeviceRunning>())
if(device->Flags.test(DeviceRunning))
return ALC_NO_ERROR;
device->AvgSpeakerDist = 0.0f;
@@ -1927,7 +1927,7 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
if(hrtf_userreq == Hrtf_Enable || (hrtf_userreq != Hrtf_Disable && hrtf_appreq == Hrtf_Enable))
{
device->FmtChans = DevFmtStereo;
device->Flags.set<ChannelsRequest>();
device->Flags.set(ChannelsRequest);
}
}
@@ -1936,9 +1936,9 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
oldType = device->FmtType;
TRACE("Pre-reset: %s%s, %s%s, %s%uhz, %u / %u buffer\n",
device->Flags.get<ChannelsRequest>()?"*":"", DevFmtChannelsString(device->FmtChans),
device->Flags.get<SampleTypeRequest>()?"*":"", DevFmtTypeString(device->FmtType),
device->Flags.get<FrequencyRequest>()?"*":"", device->Frequency,
device->Flags.test(ChannelsRequest)?"*":"", DevFmtChannelsString(device->FmtChans),
device->Flags.test(SampleTypeRequest)?"*":"", DevFmtTypeString(device->FmtType),
device->Flags.test(FrequencyRequest)?"*":"", device->Frequency,
device->UpdateSize, device->BufferSize);
try {
@@ -1951,22 +1951,22 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
return ALC_INVALID_DEVICE;
}
if(device->FmtChans != oldChans && device->Flags.get<ChannelsRequest>())
if(device->FmtChans != oldChans && device->Flags.test(ChannelsRequest))
{
ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
DevFmtChannelsString(device->FmtChans));
device->Flags.unset<ChannelsRequest>();
device->Flags.reset(ChannelsRequest);
}
if(device->FmtType != oldType && device->Flags.get<SampleTypeRequest>())
if(device->FmtType != oldType && device->Flags.test(SampleTypeRequest))
{
ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
DevFmtTypeString(device->FmtType));
device->Flags.unset<SampleTypeRequest>();
device->Flags.reset(SampleTypeRequest);
}
if(device->Frequency != oldFreq && device->Flags.get<FrequencyRequest>())
if(device->Frequency != oldFreq && device->Flags.test(FrequencyRequest))
{
WARN("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
device->Flags.unset<FrequencyRequest>();
device->Flags.reset(FrequencyRequest);
}
TRACE("Post-reset: %s, %s, %uhz, %u / %u buffer\n",
@@ -2263,12 +2263,12 @@ static ALCenum UpdateDeviceParams(ALCdevice *device, const int *attrList)
}
mixer_mode.leave();
if(!device->Flags.get<DevicePaused>())
if(!device->Flags.test(DevicePaused))
{
try {
auto backend = device->Backend.get();
backend->start();
device->Flags.set<DeviceRunning>();
device->Flags.set(DeviceRunning);
}
catch(al::backend_exception& e) {
device->handleDisconnect("%s", e.what());
@@ -3367,10 +3367,10 @@ START_API_FUNC
ALCdevice *Device{ctx->mDevice.get()};
std::lock_guard<std::mutex> _{Device->StateLock};
if(!ctx->deinit() && Device->Flags.get<DeviceRunning>())
if(!ctx->deinit() && Device->Flags.test(DeviceRunning))
{
Device->Backend->stop();
Device->Flags.unset<DeviceRunning>();
Device->Flags.reset(DeviceRunning);
}
}
END_API_FUNC
@@ -3545,7 +3545,7 @@ START_API_FUNC
{
device->FmtChans = iter->chans;
device->mAmbiOrder = iter->order;
device->Flags.set<ChannelsRequest>();
device->Flags.set(ChannelsRequest);
}
}
if(auto typeopt = ConfigValueStr(deviceName, nullptr, "sample-type"))
@@ -3573,7 +3573,7 @@ START_API_FUNC
else
{
device->FmtType = iter->type;
device->Flags.set<SampleTypeRequest>();
device->Flags.set(SampleTypeRequest);
}
}
@@ -3589,7 +3589,7 @@ START_API_FUNC
device->UpdateSize = static_cast<uint>(device->UpdateSize*scale + 0.5);
device->BufferSize = static_cast<uint>(device->BufferSize*scale + 0.5);
device->Frequency = freq;
device->Flags.set<FrequencyRequest>();
device->Flags.set(FrequencyRequest);
}
if(auto persizeopt = ConfigValueUInt(deviceName, nullptr, "period_size"))
@@ -3698,9 +3698,9 @@ START_API_FUNC
}
orphanctxs.clear();
if(dev->Flags.get<DeviceRunning>())
if(dev->Flags.test(DeviceRunning))
dev->Backend->stop();
dev->Flags.unset<DeviceRunning>();
dev->Flags.reset(DeviceRunning);
return ALC_TRUE;
}
@@ -3746,7 +3746,9 @@ START_API_FUNC
device->Frequency = frequency;
device->FmtChans = decompfmt->chans;
device->FmtType = decompfmt->type;
device->Flags.set<FrequencyRequest, ChannelsRequest, SampleTypeRequest>();
device->Flags.set(FrequencyRequest);
device->Flags.set(ChannelsRequest);
device->Flags.set(SampleTypeRequest);
device->UpdateSize = static_cast<uint>(samples);
device->BufferSize = static_cast<uint>(samples);
@@ -3800,9 +3802,9 @@ START_API_FUNC
listlock.unlock();
std::lock_guard<std::mutex> _{dev->StateLock};
if(dev->Flags.get<DeviceRunning>())
if(dev->Flags.test(DeviceRunning))
dev->Backend->stop();
dev->Flags.unset<DeviceRunning>();
dev->Flags.reset(DeviceRunning);
return ALC_TRUE;
}
@@ -3821,12 +3823,12 @@ START_API_FUNC
std::lock_guard<std::mutex> _{dev->StateLock};
if(!dev->Connected.load(std::memory_order_acquire))
alcSetError(dev.get(), ALC_INVALID_DEVICE);
else if(!dev->Flags.get<DeviceRunning>())
else if(!dev->Flags.test(DeviceRunning))
{
try {
auto backend = dev->Backend.get();
backend->start();
dev->Flags.set<DeviceRunning>();
dev->Flags.set(DeviceRunning);
}
catch(al::backend_exception& e) {
dev->handleDisconnect("%s", e.what());
@@ -3845,9 +3847,9 @@ START_API_FUNC
else
{
std::lock_guard<std::mutex> _{dev->StateLock};
if(dev->Flags.get<DeviceRunning>())
if(dev->Flags.test(DeviceRunning))
dev->Backend->stop();
dev->Flags.unset<DeviceRunning>();
dev->Flags.reset(DeviceRunning);
}
}
END_API_FUNC
@@ -4007,10 +4009,10 @@ START_API_FUNC
else
{
std::lock_guard<std::mutex> _{dev->StateLock};
if(dev->Flags.get<DeviceRunning>())
if(dev->Flags.test(DeviceRunning))
dev->Backend->stop();
dev->Flags.unset<DeviceRunning>();
dev->Flags.set<DevicePaused>();
dev->Flags.reset(DeviceRunning);
dev->Flags.set(DevicePaused);
}
}
END_API_FUNC
@@ -4027,16 +4029,16 @@ START_API_FUNC
}
std::lock_guard<std::mutex> _{dev->StateLock};
if(!dev->Flags.get<DevicePaused>())
if(!dev->Flags.test(DevicePaused))
return;
dev->Flags.unset<DevicePaused>();
dev->Flags.reset(DevicePaused);
if(dev->mContexts.load()->empty())
return;
try {
auto backend = dev->Backend.get();
backend->start();
dev->Flags.set<DeviceRunning>();
dev->Flags.set(DeviceRunning);
}
catch(al::backend_exception& e) {
dev->handleDisconnect("%s", e.what());
@@ -4092,9 +4094,9 @@ START_API_FUNC
/* Force the backend to stop mixing first since we're resetting. Also reset
* the connected state so lost devices can attempt recover.
*/
if(dev->Flags.get<DeviceRunning>())
if(dev->Flags.test(DeviceRunning))
dev->Backend->stop();
dev->Flags.unset<DeviceRunning>();
dev->Flags.reset(DeviceRunning);
if(!dev->Connected.load(std::memory_order_relaxed))
{
/* Make sure disconnection is finished before continuing on. */
+2 -2
View File
@@ -4,6 +4,7 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <chrono>
#include <cstdint>
#include <cstddef>
@@ -17,7 +18,6 @@
#include "AL/alc.h"
#include "AL/alext.h"
#include "albyte.h"
#include "almalloc.h"
#include "alnumeric.h"
#include "alspan.h"
@@ -201,7 +201,7 @@ struct ALCdevice : public al::intrusive_ref<ALCdevice> {
std::string DeviceName;
// Device flags
al::bitfield<DeviceFlagsCount> Flags{};
std::bitset<DeviceFlagsCount> Flags{};
// Maximum number of sources that can be created
uint SourcesMax{};
+2 -2
View File
@@ -746,8 +746,8 @@ bool AlsaPlayback::reset()
}
CHECK(snd_pcm_hw_params_set_channels(mPcmHandle, hp.get(), mDevice->channelsFromFmt()));
/* set rate (implicitly constrains period/buffer parameters) */
if(!GetConfigValueBool(mDevice->DeviceName.c_str(), "alsa", "allow-resampler", 0) ||
!mDevice->Flags.get<FrequencyRequest>())
if(!GetConfigValueBool(mDevice->DeviceName.c_str(), "alsa", "allow-resampler", 0)
|| !mDevice->Flags.test(FrequencyRequest))
{
if(snd_pcm_hw_params_set_rate_resample(mPcmHandle, hp.get(), 0) < 0)
ERR("Failed to disable ALSA resampler\n");
+2 -2
View File
@@ -377,7 +377,7 @@ bool DSoundPlayback::reset()
mDevice->FmtType = DevFmtUByte;
break;
case DevFmtFloat:
if(mDevice->Flags.get<SampleTypeRequest>())
if(mDevice->Flags.test(SampleTypeRequest))
break;
/* fall-through */
case DevFmtUShort:
@@ -398,7 +398,7 @@ bool DSoundPlayback::reset()
if(SUCCEEDED(hr))
{
speakers = DSSPEAKER_CONFIG(speakers);
if(!mDevice->Flags.get<ChannelsRequest>())
if(!mDevice->Flags.test(ChannelsRequest))
{
if(speakers == DSSPEAKER_MONO)
mDevice->FmtChans = DevFmtMono;
+3 -3
View File
@@ -87,9 +87,9 @@ bool OboePlayback::reset()
builder.setFormatConversionAllowed(false);
builder.setCallback(this);
if(mDevice->Flags.get<FrequencyRequest>())
if(mDevice->Flags.test(FrequencyRequest))
builder.setSampleRate(static_cast<int32_t>(mDevice->Frequency));
if(mDevice->Flags.get<ChannelsRequest>())
if(mDevice->Flags.test(ChannelsRequest))
{
/* Only use mono or stereo at user request. There's no telling what
* other counts may be inferred as.
@@ -98,7 +98,7 @@ bool OboePlayback::reset()
: (mDevice->FmtChans==DevFmtStereo) ? oboe::ChannelCount::Stereo
: oboe::ChannelCount::Unspecified);
}
if(mDevice->Flags.get<SampleTypeRequest>())
if(mDevice->Flags.test(SampleTypeRequest))
{
oboe::AudioFormat format{oboe::AudioFormat::Unspecified};
switch(mDevice->FmtType)
+3 -3
View File
@@ -793,7 +793,7 @@ void PulsePlayback::sinkInfoCallback(pa_context*, const pa_sink_info *info, int
);
if(chaniter != chanmaps.cend())
{
if(!mDevice->Flags.get<ChannelsRequest>())
if(!mDevice->Flags.test(ChannelsRequest))
mDevice->FmtChans = chaniter->fmt;
}
else
@@ -914,8 +914,8 @@ bool PulsePlayback::reset()
flags &= ~PA_STREAM_EARLY_REQUESTS;
flags |= PA_STREAM_ADJUST_LATENCY;
}
if(GetConfigValueBool(mDevice->DeviceName.c_str(), "pulse", "fix-rate", 0) ||
!mDevice->Flags.get<FrequencyRequest>())
if(GetConfigValueBool(mDevice->DeviceName.c_str(), "pulse", "fix-rate", 0)
|| !mDevice->Flags.test(FrequencyRequest))
flags |= PA_STREAM_FIX_RATE;
pa_channel_map chanmap{};
+2 -2
View File
@@ -924,9 +924,9 @@ HRESULT WasapiPlayback::resetProxy()
const ReferenceTime per_time{ReferenceTime{seconds{mDevice->UpdateSize}} / mDevice->Frequency};
const ReferenceTime buf_time{ReferenceTime{seconds{mDevice->BufferSize}} / mDevice->Frequency};
if(!mDevice->Flags.get<FrequencyRequest>())
if(!mDevice->Flags.test(FrequencyRequest))
mDevice->Frequency = OutputType.Format.nSamplesPerSec;
if(!mDevice->Flags.get<ChannelsRequest>())
if(!mDevice->Flags.test(ChannelsRequest))
{
const uint32_t chancount{OutputType.Format.nChannels};
const DWORD chanmask{OutputType.dwChannelMask};
+1
View File
@@ -41,6 +41,7 @@
#include <utility>
#include "albit.h"
#include "albyte.h"
#include "alcmain.h"
#include "alconfig.h"
#include "alfstream.h"
-47
View File
@@ -62,53 +62,6 @@ AL_DECL_OP(^, ^=)
constexpr al::byte operator~(al::byte b) noexcept
{ return al::byte(~to_integer<uint>(b)); }
namespace detail_ {
template<size_t> struct Elem { };
template<> struct Elem<1> { using type = uint8_t; };
template<> struct Elem<2> { using type = uint16_t; };
template<> struct Elem<3> { using type = uint32_t; };
template<> struct Elem<4> { using type = uint32_t; };
template<size_t N> using ElemT = typename Elem<N>::type;
} // namespace detail_
template<size_t N>
class bitfield {
static constexpr size_t bits_per_byte{std::numeric_limits<unsigned char>::digits};
static constexpr size_t NumElems{(N+bits_per_byte-1) / bits_per_byte};
using storage_type = detail_::ElemT<NumElems>;
storage_type vals{};
public:
template<size_t b>
inline void set() noexcept
{
static_assert(b < N, "Bit index out of range");
vals |= 1 << b;
}
template<size_t b>
inline void unset() noexcept
{
static_assert(b < N, "Bit index out of range");
vals &= static_cast<storage_type>(~(1 << b));
}
template<size_t b>
inline bool get() const noexcept
{
static_assert(b < N, "Bit index out of range");
return (vals & (1 << b)) != 0;
}
template<size_t b, size_t ...args>
std::enable_if_t<(sizeof...(args) > 0)> set() noexcept
{
set<b>();
set<args...>();
}
};
} // namespace al
#endif /* AL_BYTE_H */