diff options
author | Chris Robinson <[email protected]> | 2018-12-27 18:33:49 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-12-27 18:33:49 -0800 |
commit | 0f3645902886b52102ffec51636c2fd8f1e7c3c5 (patch) | |
tree | cd93a3cfbcfbe38cb207f56506012b6141374781 /Alc | |
parent | 465ab11748cab17536ae6487027e2743134ab26f (diff) |
Use vectors instead of malloc'd buffers
Diffstat (limited to 'Alc')
-rw-r--r-- | Alc/backends/sndio.cpp | 46 | ||||
-rw-r--r-- | Alc/backends/solaris.cpp | 28 |
2 files changed, 32 insertions, 42 deletions
diff --git a/Alc/backends/sndio.cpp b/Alc/backends/sndio.cpp index a37c1cd3..e4d1e9f0 100644 --- a/Alc/backends/sndio.cpp +++ b/Alc/backends/sndio.cpp @@ -31,21 +31,23 @@ #include "alMain.h" #include "alu.h" #include "threads.h" +#include "vector.h" #include "ringbuffer.h" #include <sndio.h> +namespace { + static const ALCchar sndio_device[] = "SndIO Default"; struct SndioPlayback final : public ALCbackend { sio_hdl *mSndHandle{nullptr}; - ALvoid *mMixData{nullptr}; - ALsizei mDataSize{0}; + al::vector<ALubyte> mBuffer; - std::atomic<ALenum> mKillNow{AL_TRUE}; + std::atomic<bool> mKillNow{true}; std::thread mThread; SndioPlayback(ALCdevice *device) noexcept : ALCbackend{device} { } @@ -81,9 +83,6 @@ static void SndioPlayback_Destruct(SndioPlayback *self) sio_close(self->mSndHandle); self->mSndHandle = nullptr; - al_free(self->mMixData); - self->mMixData = nullptr; - self->~SndioPlayback(); } @@ -91,26 +90,24 @@ static void SndioPlayback_Destruct(SndioPlayback *self) static int SndioPlayback_mixerProc(SndioPlayback *self) { ALCdevice *device{self->mDevice}; - ALsizei frameSize; - size_t wrote; SetRTPriority(); althrd_setname(MIXER_THREAD_NAME); - frameSize = device->frameSizeFromFmt(); + const ALsizei frameSize{device->frameSizeFromFmt()}; while(!self->mKillNow.load(std::memory_order_acquire) && device->Connected.load(std::memory_order_acquire)) { - ALsizei len = self->mDataSize; - ALubyte *WritePtr = static_cast<ALubyte*>(self->mMixData); + auto WritePtr = static_cast<ALubyte*>(self->mBuffer.data()); + size_t len{self->mBuffer.size()}; SndioPlayback_lock(self); aluMixData(device, WritePtr, len/frameSize); SndioPlayback_unlock(self); while(len > 0 && !self->mKillNow.load(std::memory_order_acquire)) { - wrote = sio_write(self->mSndHandle, WritePtr, len); + size_t wrote{sio_write(self->mSndHandle, WritePtr, len)}; if(wrote == 0) { ERR("sio_write failed\n"); @@ -226,22 +223,19 @@ static ALCboolean SndioPlayback_reset(SndioPlayback *self) return ALC_FALSE; } + SetDefaultChannelOrder(device); + device->UpdateSize = par.round; device->NumUpdates = (par.bufsz/par.round) + 1; - SetDefaultChannelOrder(device); + self->mBuffer.resize(device->UpdateSize * device->frameSizeFromFmt()); + std::fill(self->mBuffer.begin(), self->mBuffer.end(), 0); return ALC_TRUE; } static ALCboolean SndioPlayback_start(SndioPlayback *self) { - ALCdevice *device{self->mDevice}; - - self->mDataSize = device->UpdateSize * device->frameSizeFromFmt(); - al_free(self->mMixData); - self->mMixData = al_calloc(16, self->mDataSize); - if(!sio_start(self->mSndHandle)) { ERR("Error starting playback\n"); @@ -249,7 +243,7 @@ static ALCboolean SndioPlayback_start(SndioPlayback *self) } try { - self->mKillNow.store(AL_FALSE, std::memory_order_release); + self->mKillNow.store(false, std::memory_order_release); self->mThread = std::thread(SndioPlayback_mixerProc, self); return ALC_TRUE; } @@ -264,15 +258,12 @@ static ALCboolean SndioPlayback_start(SndioPlayback *self) static void SndioPlayback_stop(SndioPlayback *self) { - if(self->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel) || !self->mThread.joinable()) + if(self->mKillNow.exchange(true, std::memory_order_acq_rel) || !self->mThread.joinable()) return; self->mThread.join(); if(!sio_stop(self->mSndHandle)) ERR("Error stopping device\n"); - - al_free(self->mMixData); - self->mMixData = nullptr; } @@ -281,7 +272,7 @@ struct SndioCapture final : public ALCbackend { RingBufferPtr mRing; - std::atomic<ALenum> mKillNow{AL_TRUE}; + std::atomic<bool> mKillNow{true}; std::thread mThread; SndioCapture(ALCdevice *device) noexcept : ALCbackend{device} { } @@ -488,7 +479,7 @@ static ALCboolean SndioCapture_start(SndioCapture *self) } try { - self->mKillNow.store(AL_FALSE, std::memory_order_release); + self->mKillNow.store(false, std::memory_order_release); self->mThread = std::thread(SndioCapture_recordProc, self); return ALC_TRUE; } @@ -503,7 +494,7 @@ static ALCboolean SndioCapture_start(SndioCapture *self) static void SndioCapture_stop(SndioCapture *self) { - if(self->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel) || !self->mThread.joinable()) + if(self->mKillNow.exchange(true, std::memory_order_acq_rel) || !self->mThread.joinable()) return; self->mThread.join(); @@ -524,6 +515,7 @@ static ALCuint SndioCapture_availableSamples(SndioCapture *self) return ring->readSpace(); } +} // namespace BackendFactory &SndIOBackendFactory::getFactory() { diff --git a/Alc/backends/solaris.cpp b/Alc/backends/solaris.cpp index ab622927..e56b1c81 100644 --- a/Alc/backends/solaris.cpp +++ b/Alc/backends/solaris.cpp @@ -41,18 +41,20 @@ #include "alu.h" #include "alconfig.h" #include "threads.h" +#include "vector.h" #include "compat.h" #include <sys/audioio.h> +namespace { + struct ALCsolarisBackend final : public ALCbackend { int mFd{-1}; - ALubyte *mMixData{nullptr}; - int mDataSize{0}; + al::vector<ALubyte> mBuffer; - std::atomic<ALenum> mKillNow{AL_TRUE}; + std::atomic<bool> mKillNow{true}; std::thread mThread; ALCsolarisBackend(ALCdevice *device) noexcept : ALCbackend{device} { } @@ -93,10 +95,6 @@ static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self) close(self->mFd); self->mFd = -1; - free(self->mMixData); - self->mMixData = nullptr; - self->mDataSize = 0; - self->~ALCsolarisBackend(); } @@ -135,10 +133,10 @@ static int ALCsolarisBackend_mixerProc(ALCsolarisBackend *self) continue; } - ALubyte *write_ptr{self->mMixData}; - int to_write{self->mDataSize}; + ALubyte *write_ptr{self->mBuffer.data()}; + size_t to_write{self->mBuffer.size()}; aluMixData(device, write_ptr, to_write/frame_size); - while(to_write > 0 && !self->mKillNow.load()) + while(to_write > 0 && !self->mKillNow.load(std::memory_order_acquire)) { ssize_t wrote{write(self->mFd, write_ptr, to_write)}; if(wrote < 0) @@ -252,9 +250,8 @@ static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self) SetDefaultChannelOrder(device); - free(self->mMixData); - self->mDataSize = device->UpdateSize * device->frameSizeFromFmt(); - self->mMixData = static_cast<ALubyte*>(calloc(1, self->mDataSize)); + self->mBuffer.resize(device->UpdateSize * device->frameSizeFromFmt()); + std::fill(self->mBuffer.begin(), self->mBuffer.end(), 0); return ALC_TRUE; } @@ -262,7 +259,7 @@ static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self) static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self) { try { - self->mKillNow.store(AL_FALSE); + self->mKillNow.store(false, std::memory_order_release); self->mThread = std::thread(ALCsolarisBackend_mixerProc, self); return ALC_TRUE; } @@ -276,7 +273,7 @@ static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self) static void ALCsolarisBackend_stop(ALCsolarisBackend *self) { - if(self->mKillNow.exchange(AL_TRUE) || !self->mThread.joinable()) + if(self->mKillNow.exchange(true, std::memory_order_acq_rel) || !self->mThread.joinable()) return; self->mThread.join(); @@ -285,6 +282,7 @@ static void ALCsolarisBackend_stop(ALCsolarisBackend *self) ERR("Error draining device: %s\n", strerror(errno)); } +} // namespace BackendFactory &SolarisBackendFactory::getFactory() { |