diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 9e26e7261..3c62c80b2 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -521,7 +521,7 @@ class GnomeShutdown public: GnomeShutdown() { - mArgv[0] = strdup("Audacity"); + mArgv[0].reset(strdup("Audacity")); mGnomeui = dlopen("libgnomeui-2.so.0", RTLD_NOW); if (!mGnomeui) { @@ -550,11 +550,11 @@ class GnomeShutdown return; } - gnome_program_init(mArgv[0], + gnome_program_init(mArgv[0].get(), "1.0", libgnomeui_module_info_get(), 1, - mArgv, + reinterpret_cast(mArgv), NULL); mClient = gnome_master_client(); @@ -568,13 +568,11 @@ class GnomeShutdown virtual ~GnomeShutdown() { // Do not dlclose() the libraries here lest you want segfaults... - - free(mArgv[0]); } private: - char *mArgv[1]; + MallocString<> mArgv[1]; void *mGnomeui; void *mGnome; GnomeClient *mClient; diff --git a/src/FileFormats.cpp b/src/FileFormats.cpp index d24b269c3..071025499 100644 --- a/src/FileFormats.cpp +++ b/src/FileFormats.cpp @@ -15,6 +15,8 @@ information. *//*******************************************************************/ +#include "Audacity.h" +#include "MemoryX.h" #include #include #include "sndfile.h" @@ -119,7 +121,6 @@ wxString sf_header_name(int format) wxString sf_header_shortname(int format) { SF_FORMAT_INFO format_info; - char *tmp; int i; wxString s; @@ -127,8 +128,7 @@ wxString sf_header_shortname(int format) format_info.format = (format & SF_FORMAT_TYPEMASK); sf_command(NULL, SFC_GET_FORMAT_INFO, &format_info, sizeof(format_info)); - tmp = (char *)malloc(strlen(format_info.name)+1); - strcpy(tmp, format_info.name); + MallocString<> tmp { strdup( format_info.name ) }; i = 0; while(tmp[i]) { if (tmp[i]==' ') @@ -137,9 +137,7 @@ wxString sf_header_shortname(int format) i++; } - s = LAT1CTOWX(tmp); - - free(tmp); + s = LAT1CTOWX(tmp.get()); return s; } diff --git a/src/RealFFTf48x.cpp b/src/RealFFTf48x.cpp index b624026e9..189d176a4 100644 --- a/src/RealFFTf48x.cpp +++ b/src/RealFFTf48x.cpp @@ -34,6 +34,8 @@ * and BitReversed tables so they don't need to be reallocated * and recomputed on every call. * - Added Reorder* functions to undo the bit-reversal +* Modified 15 April 2016 Paul Licameli +* - C++11 smart pointers * * Copyright (C) 2009 Philip VanBaren * diff --git a/src/Resample.cpp b/src/Resample.cpp index a840edd6c..5abde49b7 100644 --- a/src/Resample.cpp +++ b/src/Resample.cpp @@ -41,13 +41,11 @@ Resample::Resample(const bool useBestMethod, const double dMinFactor, const doub mbWantConstRateResampling = false; // variable rate resampling q_spec = soxr_quality_spec(SOXR_HQ, SOXR_VR); } - mHandle = (void *)soxr_create(1, dMinFactor, 1, 0, 0, &q_spec, 0); + mHandle.reset(soxr_create(1, dMinFactor, 1, 0, 0, &q_spec, 0)); } Resample::~Resample() { - soxr_delete((soxr_t)mHandle); - mHandle = NULL; } int Resample::GetNumMethods() { return 4; } @@ -85,16 +83,16 @@ std::pair size_t idone, odone; if (mbWantConstRateResampling) { - soxr_process((soxr_t)mHandle, + soxr_process(mHandle.get(), inBuffer , (lastFlag? ~inBufferLen : inBufferLen), &idone, outBuffer, outBufferLen, &odone); } else { - soxr_set_io_ratio((soxr_t)mHandle, 1/factor, 0); + soxr_set_io_ratio(mHandle.get(), 1/factor, 0); inBufferLen = lastFlag? ~inBufferLen : inBufferLen; - soxr_process((soxr_t)mHandle, + soxr_process(mHandle.get(), inBuffer , inBufferLen , &idone, outBuffer, outBufferLen, &odone); } diff --git a/src/Resample.h b/src/Resample.h index e16e84af7..aa14ea842 100644 --- a/src/Resample.h +++ b/src/Resample.h @@ -14,11 +14,19 @@ #include "Audacity.h" +#include "MemoryX.h" #include #include #include "SampleFormat.h" +struct soxr; +extern "C" void soxr_delete(soxr*); +struct soxr_deleter { + void operator () (soxr *p) const { if (p) soxr_delete(p); } +}; +using soxrHandle = std::unique_ptr; + class Resample final { public: @@ -78,7 +86,7 @@ class Resample final protected: int mMethod; // resampler-specific enum for resampling method - void* mHandle; // constant-rate or variable-rate resampler (XOR per instance) + soxrHandle mHandle; // constant-rate or variable-rate resampler (XOR per instance) bool mbWantConstRateResampling; }; diff --git a/src/SampleFormat.cpp b/src/SampleFormat.cpp index 8f5aa9251..01ad23a25 100644 --- a/src/SampleFormat.cpp +++ b/src/SampleFormat.cpp @@ -74,16 +74,6 @@ const wxChar *GetSampleFormatStr(sampleFormat format) return wxT("Unknown format"); // compiler food } -AUDACITY_DLL_API samplePtr NewSamples(size_t count, sampleFormat format) -{ - return (samplePtr)malloc(count * SAMPLE_SIZE(format)); -} - -AUDACITY_DLL_API void DeleteSamples(samplePtr p) -{ - free(p); -} - // TODO: Risky? Assumes 0.0f is represented by 0x00000000; void ClearSamples(samplePtr src, sampleFormat format, size_t start, size_t len) diff --git a/src/SampleFormat.h b/src/SampleFormat.h index f41d881d0..8df843bf7 100644 --- a/src/SampleFormat.h +++ b/src/SampleFormat.h @@ -49,10 +49,6 @@ const wxChar *GetSampleFormatStr(sampleFormat format); // Allocating/Freeing Samples // -AUDACITY_DLL_API samplePtr NewSamples(size_t count, sampleFormat format); -AUDACITY_DLL_API void DeleteSamples(samplePtr p); - -// RAII version of above class SampleBuffer { public: @@ -60,7 +56,7 @@ public: : mPtr(0) {} SampleBuffer(size_t count, sampleFormat format) - : mPtr(NewSamples(count, format)) + : mPtr((samplePtr)malloc(count * SAMPLE_SIZE(format))) {} ~SampleBuffer() { @@ -71,14 +67,14 @@ public: SampleBuffer &Allocate(size_t count, sampleFormat format) { Free(); - mPtr = NewSamples(count, format); + mPtr = (samplePtr)malloc(count * SAMPLE_SIZE(format)); return *this; } void Free() { - DeleteSamples(mPtr); + free(mPtr); mPtr = 0; } diff --git a/src/Theme.cpp b/src/Theme.cpp index fabe42d32..466ab5f28 100644 --- a/src/Theme.cpp +++ b/src/Theme.cpp @@ -347,7 +347,9 @@ wxImage ThemeBase::MaskedImage( char const ** pXpm, char const ** pMask ) // unsigned char *src = Img1.GetData(); unsigned char *mk = Img2.GetData(); - unsigned char *alpha = (unsigned char*)malloc( nBytes ); + //wxImage::setAlpha requires memory allocated with malloc, not new + MallocString alpha{ + static_cast(malloc( nBytes )) }; // Extract alpha channel from second XPM. for(i=0;i samplePtr;//interleaved samples sampleCount start; size_t len; unsigned numChannels; @@ -391,6 +388,7 @@ int ODFFmpegDecoder::Decode(SampleBuffer & data, sampleFormat & format, sampleCo //if we've skipped over some samples, fill the gap with silence. This could happen often in the beginning of the file. if(actualDecodeStart>start && firstpass) { // find the number of samples for the leading silence + // UNSAFE_SAMPLE_COUNT_TRUNCATION // -- but used only experimentally as of this writing // Is there a proof size_t will not overflow size_t? @@ -402,7 +400,7 @@ int ODFFmpegDecoder::Decode(SampleBuffer & data, sampleFormat & format, sampleCo //put it in the cache so the other channels can use it. // wxASSERT(sc->m_stream->codec->channels > 0); - cache->numChannels = sc->m_stream->codec->channels; + cache->numChannels = std::max(0, sc->m_stream->codec->channels); cache->len = amt; cache->start=start; // 8 bit and 16 bit audio output from ffmpeg means @@ -413,9 +411,7 @@ int ODFFmpegDecoder::Decode(SampleBuffer & data, sampleFormat & format, sampleCo else cache->samplefmt = AV_SAMPLE_FMT_FLT; - cache->samplePtr = (uint8_t*) malloc(amt * cache->numChannels * SAMPLE_SIZE(format)); - - memset(cache->samplePtr, 0, amt * cache->numChannels * SAMPLE_SIZE(format)); + cache->samplePtr.reinit(amt * cache->numChannels * SAMPLE_SIZE(format), true); InsertCache(std::move(cache)); } @@ -538,27 +534,27 @@ int ODFFmpegDecoder::FillDataFromCache(samplePtr & data, sampleFormat outFormat, { case AV_SAMPLE_FMT_U8: //printf("u8 in %lu out %lu cachelen %lu outLen %lu\n", inIndex, outIndex, mDecodeCache[i]->len, len); - ((int16_t *)outBuf)[outIndex] = (int16_t) (((uint8_t*)mDecodeCache[i]->samplePtr)[inIndex] - 0x80) << 8; + ((int16_t *)outBuf)[outIndex] = (int16_t) (((uint8_t*)mDecodeCache[i]->samplePtr.get())[inIndex] - 0x80) << 8; break; case AV_SAMPLE_FMT_S16: //printf("u16 in %lu out %lu cachelen %lu outLen % lu\n", inIndex, outIndex, mDecodeCache[i]->len, len); - ((int16_t *)outBuf)[outIndex] = ((int16_t*)mDecodeCache[i]->samplePtr)[inIndex]; + ((int16_t *)outBuf)[outIndex] = ((int16_t*)mDecodeCache[i]->samplePtr.get())[inIndex]; break; case AV_SAMPLE_FMT_S32: //printf("s32 in %lu out %lu cachelen %lu outLen %lu\n", inIndex, outIndex, mDecodeCache[i]->len, len); - ((float *)outBuf)[outIndex] = (float) ((int32_t*)mDecodeCache[i]->samplePtr)[inIndex] * (1.0 / (1u << 31)); + ((float *)outBuf)[outIndex] = (float) ((int32_t*)mDecodeCache[i]->samplePtr.get())[inIndex] * (1.0 / (1u << 31)); break; case AV_SAMPLE_FMT_FLT: //printf("f in %lu out %lu cachelen %lu outLen %lu\n", inIndex, outIndex, mDecodeCache[i]->len, len); - ((float *)outBuf)[outIndex] = (float) ((float*)mDecodeCache[i]->samplePtr)[inIndex]; + ((float *)outBuf)[outIndex] = (float) ((float*)mDecodeCache[i]->samplePtr.get())[inIndex]; break; case AV_SAMPLE_FMT_DBL: //printf("dbl in %lu out %lu cachelen %lu outLen %lu\n", inIndex, outIndex, mDecodeCache[i]->len, len); - ((float *)outBuf)[outIndex] = (float) ((double*)mDecodeCache[i]->samplePtr)[inIndex]; + ((float *)outBuf)[outIndex] = (float) ((double*)mDecodeCache[i]->samplePtr.get())[inIndex]; break; default: @@ -614,13 +610,13 @@ int ODFFmpegDecoder::DecodeFrame(streamContext *sc, bool flushing) auto cache = make_movable(); //len is number of samples per channel // wxASSERT(sc->m_stream->codec->channels > 0); - cache->numChannels = sc->m_stream->codec->channels; + cache->numChannels = std::max(0, sc->m_stream->codec->channels); cache->len = (sc->m_decodedAudioSamplesValidSiz / sc->m_samplesize) / cache->numChannels; cache->start = mCurrentPos; - cache->samplePtr = (uint8_t*) malloc(sc->m_decodedAudioSamplesValidSiz); + cache->samplePtr.reinit(sc->m_decodedAudioSamplesValidSiz); cache->samplefmt = sc->m_samplefmt; - memcpy(cache->samplePtr, sc->m_decodedAudioSamples.get(), sc->m_decodedAudioSamplesValidSiz); + memcpy(cache->samplePtr.get(), sc->m_decodedAudioSamples.get(), sc->m_decodedAudioSamplesValidSiz); InsertCache(std::move(cache)); } diff --git a/src/ondemand/ODTaskThread.h b/src/ondemand/ODTaskThread.h index dd08a7b79..dd15fc155 100644 --- a/src/ondemand/ODTaskThread.h +++ b/src/ondemand/ODTaskThread.h @@ -110,7 +110,7 @@ class ODLock { private: friend class ODCondition; //needs friendship for wait() - pthread_mutex_t mutex ; + pthread_mutex_t mutex; }; class ODCondition diff --git a/src/widgets/Ruler.cpp b/src/widgets/Ruler.cpp index abd9edabf..04d59df1c 100644 --- a/src/widgets/Ruler.cpp +++ b/src/widgets/Ruler.cpp @@ -48,7 +48,7 @@ \brief An array of these created by the Ruler is used to determine what and where text annotations to the numbers on the Ruler get drawn. -\todo Check whether Ruler is costing too much time in malloc/free of +\todo Check whether Ruler is costing too much time in allocation/free of array of Ruler::Label. *//******************************************************************/