From 8f08ca9cbafcce05879e8a254e6888edf3eead12 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 25 Nov 2015 06:06:27 -0800 Subject: [PATCH] Revert "Avoid unsafe sizeof(x)/sizeof(x[0]) constructs for array counting" This reverts commit 0d4fea896c549edb109587d42f9630af1f7816f9. --- apps/openmw/mwsound/openal_output.cpp | 98 +++++++++++++++------------ 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 419bb76b7..bafd272af 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -23,17 +23,13 @@ #define MAKE_PTRID(id) ((void*)(uintptr_t)id) #define GET_PTRID(ptr) ((ALuint)(uintptr_t)ptr) -namespace +namespace MWSound { -template -inline size_t countof(const T(&)[N]) -{ return N; } - -void fail(const std::string &msg) +static void fail(const std::string &msg) { throw std::runtime_error("OpenAL exception: " + msg); } -void throwALCerror(ALCdevice *device) +static void throwALCerror(ALCdevice *device) { ALCenum err = alcGetError(device); if(err != ALC_NO_ERROR) @@ -43,7 +39,7 @@ void throwALCerror(ALCdevice *device) } } -void throwALerror() +static void throwALerror() { ALenum err = alGetError(); if(err != AL_NO_ERROR) @@ -54,39 +50,21 @@ void throwALerror() } -ALenum getALFormat(MWSound::ChannelConfig chans, MWSound::SampleType type) +static ALenum getALFormat(ChannelConfig chans, SampleType type) { static const struct { ALenum format; - MWSound::ChannelConfig chans; - MWSound::SampleType type; + ChannelConfig chans; + SampleType type; } fmtlist[] = { - { AL_FORMAT_MONO16, MWSound::ChannelConfig_Mono, MWSound::SampleType_Int16 }, - { AL_FORMAT_MONO8, MWSound::ChannelConfig_Mono, MWSound::SampleType_UInt8 }, - { AL_FORMAT_STEREO16, MWSound::ChannelConfig_Stereo, MWSound::SampleType_Int16 }, - { AL_FORMAT_STEREO8, MWSound::ChannelConfig_Stereo, MWSound::SampleType_UInt8 }, - }; - static const struct { - char name[32]; - MWSound::ChannelConfig chans; - MWSound::SampleType type; - } mcfmtlist[] = { - { "AL_FORMAT_QUAD16", MWSound::ChannelConfig_Quad, MWSound::SampleType_Int16 }, - { "AL_FORMAT_QUAD8", MWSound::ChannelConfig_Quad, MWSound::SampleType_UInt8 }, - { "AL_FORMAT_51CHN16", MWSound::ChannelConfig_5point1, MWSound::SampleType_Int16 }, - { "AL_FORMAT_51CHN8", MWSound::ChannelConfig_5point1, MWSound::SampleType_UInt8 }, - { "AL_FORMAT_71CHN16", MWSound::ChannelConfig_7point1, MWSound::SampleType_Int16 }, - { "AL_FORMAT_71CHN8", MWSound::ChannelConfig_7point1, MWSound::SampleType_UInt8 }, - }, fltfmtlist[] = { - { "AL_FORMAT_MONO_FLOAT32", MWSound::ChannelConfig_Mono, MWSound::SampleType_Float32 }, - { "AL_FORMAT_STEREO_FLOAT32", MWSound::ChannelConfig_Stereo, MWSound::SampleType_Float32 }, - }, fltmcfmtlist[] = { - { "AL_FORMAT_QUAD32", MWSound::ChannelConfig_Quad, MWSound::SampleType_Float32 }, - { "AL_FORMAT_51CHN32", MWSound::ChannelConfig_5point1, MWSound::SampleType_Float32 }, - { "AL_FORMAT_71CHN32", MWSound::ChannelConfig_7point1, MWSound::SampleType_Float32 }, + { AL_FORMAT_MONO16, ChannelConfig_Mono, SampleType_Int16 }, + { AL_FORMAT_MONO8, ChannelConfig_Mono, SampleType_UInt8 }, + { AL_FORMAT_STEREO16, ChannelConfig_Stereo, SampleType_Int16 }, + { AL_FORMAT_STEREO8, ChannelConfig_Stereo, SampleType_UInt8 }, }; + static const size_t fmtlistsize = sizeof(fmtlist)/sizeof(fmtlist[0]); - for(size_t i = 0;i < countof(fmtlist);i++) + for(size_t i = 0;i < fmtlistsize;i++) { if(fmtlist[i].chans == chans && fmtlist[i].type == type) return fmtlist[i].format; @@ -94,7 +72,21 @@ ALenum getALFormat(MWSound::ChannelConfig chans, MWSound::SampleType type) if(alIsExtensionPresent("AL_EXT_MCFORMATS")) { - for(size_t i = 0;i < countof(mcfmtlist);i++) + static const struct { + char name[32]; + ChannelConfig chans; + SampleType type; + } mcfmtlist[] = { + { "AL_FORMAT_QUAD16", ChannelConfig_Quad, SampleType_Int16 }, + { "AL_FORMAT_QUAD8", ChannelConfig_Quad, SampleType_UInt8 }, + { "AL_FORMAT_51CHN16", ChannelConfig_5point1, SampleType_Int16 }, + { "AL_FORMAT_51CHN8", ChannelConfig_5point1, SampleType_UInt8 }, + { "AL_FORMAT_71CHN16", ChannelConfig_7point1, SampleType_Int16 }, + { "AL_FORMAT_71CHN8", ChannelConfig_7point1, SampleType_UInt8 }, + }; + static const size_t mcfmtlistsize = sizeof(mcfmtlist)/sizeof(mcfmtlist[0]); + + for(size_t i = 0;i < mcfmtlistsize;i++) { if(mcfmtlist[i].chans == chans && mcfmtlist[i].type == type) { @@ -106,7 +98,17 @@ ALenum getALFormat(MWSound::ChannelConfig chans, MWSound::SampleType type) } if(alIsExtensionPresent("AL_EXT_FLOAT32")) { - for(size_t i = 0;i < countof(fltfmtlist);i++) + static const struct { + char name[32]; + ChannelConfig chans; + SampleType type; + } fltfmtlist[] = { + { "AL_FORMAT_MONO_FLOAT32", ChannelConfig_Mono, SampleType_Float32 }, + { "AL_FORMAT_STEREO_FLOAT32", ChannelConfig_Stereo, SampleType_Float32 }, + }; + static const size_t fltfmtlistsize = sizeof(fltfmtlist)/sizeof(fltfmtlist[0]); + + for(size_t i = 0;i < fltfmtlistsize;i++) { if(fltfmtlist[i].chans == chans && fltfmtlist[i].type == type) { @@ -117,7 +119,18 @@ ALenum getALFormat(MWSound::ChannelConfig chans, MWSound::SampleType type) } if(alIsExtensionPresent("AL_EXT_MCFORMATS")) { - for(size_t i = 0;i < countof(fltmcfmtlist);i++) + static const struct { + char name[32]; + ChannelConfig chans; + SampleType type; + } fltmcfmtlist[] = { + { "AL_FORMAT_QUAD32", ChannelConfig_Quad, SampleType_Float32 }, + { "AL_FORMAT_51CHN32", ChannelConfig_5point1, SampleType_Float32 }, + { "AL_FORMAT_71CHN32", ChannelConfig_7point1, SampleType_Float32 }, + }; + static const size_t fltmcfmtlistsize = sizeof(fltmcfmtlist)/sizeof(fltmcfmtlist[0]); + + for(size_t i = 0;i < fltmcfmtlistsize;i++) { if(fltmcfmtlist[i].chans == chans && fltmcfmtlist[i].type == type) { @@ -129,10 +142,11 @@ ALenum getALFormat(MWSound::ChannelConfig chans, MWSound::SampleType type) } } - throw std::runtime_error(std::string("Unsupported sound format (")+MWSound::getChannelConfigName(chans)+", "+MWSound::getSampleTypeName(type)+")"); + fail(std::string("Unsupported sound format (")+getChannelConfigName(chans)+", "+getSampleTypeName(type)+")"); + return AL_NONE; } -double getBufferLength(ALuint buffer) +static double getBufferLength(ALuint buffer) { ALint bufferSize, frequency, channels, bitsPerSample; alGetBufferi(buffer, AL_SIZE, &bufferSize); @@ -144,10 +158,6 @@ double getBufferLength(ALuint buffer) return (8.0*bufferSize)/(frequency*channels*bitsPerSample); } -} - -namespace MWSound -{ // // A streaming OpenAL sound.