From b508a760c88e8af6e852561dd7cf50edd7c601b9 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 24 Nov 2018 10:07:48 -0800 Subject: Use a normal vector to store buffer data --- OpenAL32/Include/alBuffer.h | 43 ++++++++++++++++++++++--------------------- OpenAL32/alBuffer.cpp | 30 +++++++++++------------------- 2 files changed, 33 insertions(+), 40 deletions(-) (limited to 'OpenAL32') diff --git a/OpenAL32/Include/alBuffer.h b/OpenAL32/Include/alBuffer.h index a99c06fc..54c17670 100644 --- a/OpenAL32/Include/alBuffer.h +++ b/OpenAL32/Include/alBuffer.h @@ -7,6 +7,7 @@ #include "inprogext.h" #include "atomic.h" +#include "vector.h" /* User formats */ @@ -88,37 +89,37 @@ inline ALsizei FrameSizeFromFmt(enum FmtChannels chans, enum FmtType type) } -typedef struct ALbuffer { - ALvoid *data; +struct ALbuffer { + al::vector mData; - ALsizei Frequency; - ALbitfieldSOFT Access; - ALsizei SampleLen; + ALsizei Frequency{0}; + ALbitfieldSOFT Access{0u}; + ALsizei SampleLen{0}; - enum FmtChannels FmtChannels; - enum FmtType FmtType; - ALsizei BytesAlloc; + enum FmtChannels FmtChannels{}; + enum FmtType FmtType{}; + ALsizei BytesAlloc{0}; - enum UserFmtType OriginalType; - ALsizei OriginalSize; - ALsizei OriginalAlign; + enum UserFmtType OriginalType{}; + ALsizei OriginalSize{0}; + ALsizei OriginalAlign{0}; - ALsizei LoopStart; - ALsizei LoopEnd; + ALsizei LoopStart{0}; + ALsizei LoopEnd{0}; - ATOMIC(ALsizei) UnpackAlign; - ATOMIC(ALsizei) PackAlign; + std::atomic UnpackAlign{0}; + std::atomic PackAlign{0}; - ALbitfieldSOFT MappedAccess; - ALsizei MappedOffset; - ALsizei MappedSize; + ALbitfieldSOFT MappedAccess{0u}; + ALsizei MappedOffset{0}; + ALsizei MappedSize{0}; /* Number of times buffer was attached to a source (deletion can only occur when 0) */ - RefCount ref; + RefCount ref{0u}; /* Self ID */ - ALuint id; -} ALbuffer; + ALuint id{0}; +}; ALvoid ReleaseALBuffers(ALCdevice *device); diff --git a/OpenAL32/alBuffer.cpp b/OpenAL32/alBuffer.cpp index 196dcd22..0580b33a 100644 --- a/OpenAL32/alBuffer.cpp +++ b/OpenAL32/alBuffer.cpp @@ -110,8 +110,6 @@ void FreeBuffer(ALCdevice *device, ALbuffer *buffer) ALsizei lidx = id >> 6; ALsizei slidx = id & 0x3f; - al_free(buffer->data); - buffer->data = nullptr; buffer->~ALbuffer(); device->BufferList[lidx].FreeMask |= U64(1) << slidx; @@ -290,41 +288,37 @@ void LoadData(ALCcontext *context, ALbuffer *ALBuf, ALuint freq, ALsizei size, U newsize = (newsize+15) & ~0xf; if(newsize != ALBuf->BytesAlloc) { - void *temp{al_malloc(16, (size_t)newsize)}; - if(UNLIKELY(!temp && newsize)) - SETERR_RETURN(context, AL_OUT_OF_MEMORY,, "Failed to allocate %d bytes of storage", - newsize); + al::vector newdata(newsize); if((access&AL_PRESERVE_DATA_BIT_SOFT)) { ALsizei tocopy{std::min(newsize, ALBuf->BytesAlloc)}; - if(tocopy > 0) memcpy(temp, ALBuf->data, tocopy); + std::copy_n(ALBuf->mData.begin(), tocopy, newdata.begin()); } - al_free(ALBuf->data); - ALBuf->data = temp; + ALBuf->mData = std::move(newdata); ALBuf->BytesAlloc = newsize; } if(SrcType == UserFmtIMA4) { assert(DstType == FmtShort); - if(data != nullptr && ALBuf->data != nullptr) - Convert_ALshort_ALima4(static_cast(ALBuf->data), + if(data != nullptr && !ALBuf->mData.empty()) + Convert_ALshort_ALima4(reinterpret_cast(ALBuf->mData.data()), static_cast(data), NumChannels, frames, align); ALBuf->OriginalAlign = align; } else if(SrcType == UserFmtMSADPCM) { assert(DstType == FmtShort); - if(data != nullptr && ALBuf->data != nullptr) - Convert_ALshort_ALmsadpcm(static_cast(ALBuf->data), + if(data != nullptr && !ALBuf->mData.empty()) + Convert_ALshort_ALmsadpcm(reinterpret_cast(ALBuf->mData.data()), static_cast(data), NumChannels, frames, align); ALBuf->OriginalAlign = align; } else { assert((long)SrcType == (long)DstType); - if(data != nullptr && ALBuf->data != nullptr) - memcpy(ALBuf->data, data, frames*FrameSize); + if(data != nullptr && !ALBuf->mData.empty()) + std::copy_n(static_cast(data), frames*FrameSize, ALBuf->mData.begin()); ALBuf->OriginalAlign = 1; } ALBuf->OriginalSize = size; @@ -601,7 +595,7 @@ AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei offset, length, buffer); else { - void *retval = (ALbyte*)albuf->data + offset; + void *retval = albuf->mData.data() + offset; albuf->MappedAccess = access; albuf->MappedOffset = offset; albuf->MappedSize = length; @@ -730,7 +724,7 @@ AL_API ALvoid AL_APIENTRY alBufferSubDataSOFT(ALuint buffer, ALenum format, cons offset = offset/byte_align * align * frame_size; length = length/byte_align * align; - void *dst = static_cast(albuf->data) + offset; + void *dst = albuf->mData.data() + offset; if(srctype == UserFmtIMA4 && albuf->FmtType == FmtShort) Convert_ALshort_ALima4(static_cast(dst), static_cast(data), num_chans, length, align); @@ -1196,8 +1190,6 @@ ALvoid ReleaseALBuffers(ALCdevice *device) ALsizei idx = CTZ64(usemask); ALbuffer *buffer = sublist.Buffers + idx; - al_free(buffer->data); - buffer->data = nullptr; buffer->~ALbuffer(); ++leftover; -- cgit v1.2.3