diff options
author | JP Cimalando <jpcima@users.noreply.github.com> | 2018-09-25 15:33:50 +0200 |
---|---|---|
committer | JP Cimalando <jpcima@users.noreply.github.com> | 2018-09-25 16:07:17 +0200 |
commit | 566b5208a52c3c6771651d98d9d67f371423980d (patch) | |
tree | 11d7a936b9aa7419d012e083e64480aa317083a3 /src/chips/dosbox | |
parent | 923ea2f4d2c26248d518c9af7cafb938cd5ee9d6 (diff) | |
download | libADLMIDI-566b5208a52c3c6771651d98d9d67f371423980d.tar.gz libADLMIDI-566b5208a52c3c6771651d98d9d67f371423980d.tar.bz2 libADLMIDI-566b5208a52c3c6771651d98d9d67f371423980d.zip |
dosbox: fix a potential cache access problem, and some warnings
Diffstat (limited to 'src/chips/dosbox')
-rw-r--r-- | src/chips/dosbox/dbopl.cpp | 48 |
1 files changed, 33 insertions, 15 deletions
diff --git a/src/chips/dosbox/dbopl.cpp b/src/chips/dosbox/dbopl.cpp index 2d31708..1931203 100644 --- a/src/chips/dosbox/dbopl.cpp +++ b/src/chips/dosbox/dbopl.cpp @@ -38,6 +38,7 @@ #include <stdlib.h> #include <string.h> #include <vector> +#include <memory> #include "dbopl.h" #if defined(__GNUC__) && __GNUC__ > 3 @@ -80,7 +81,8 @@ private: #if !defined(_WIN32) #include <pthread.h> struct Mutex : NoCopy { - Mutex() : m(PTHREAD_MUTEX_INITIALIZER) {} + Mutex() { pthread_mutex_init(&m, NULL);} + ~Mutex() { pthread_mutex_destroy(&m); } void lock() { pthread_mutex_lock(&m); } void unlock() { pthread_mutex_unlock(&m); } pthread_mutex_t m; @@ -1331,14 +1333,26 @@ struct CacheEntry { Bit32u linearRates[76]; Bit32u attackRates[76]; }; -static std::vector<CacheEntry> cache; -static Mutex cacheMutex; +struct Cache : NoCopy { + ~Cache(); + Mutex mutex; + std::vector<CacheEntry *> entries; +}; + +static Cache cache; + +Cache::~Cache() +{ + for ( size_t i = 0, n = entries.size(); i < n; ++i ) + delete entries[i]; +} static const CacheEntry *CacheLookupRateDependent( Bit32u rate ) { - for ( size_t i = 0, n = cache.size(); i < n; ++i ) { - if (cache[i].rate == rate) - return &cache[i]; + for ( size_t i = 0, n = cache.entries.size(); i < n; ++i ) { + const CacheEntry *entry = cache.entries[i]; + if (entry->rate == rate) + return entry; } return NULL; } @@ -1346,7 +1360,7 @@ static const CacheEntry *CacheLookupRateDependent( Bit32u rate ) static const CacheEntry &ComputeRateDependent( Bit32u rate ) { { - MutexHolder lock( cacheMutex ); + MutexHolder lock( cache.mutex ); if (const CacheEntry *entry = CacheLookupRateDependent( rate )) return *entry; } @@ -1354,11 +1368,15 @@ static const CacheEntry &ComputeRateDependent( Bit32u rate ) double original = OPLRATE; double scale = original / (double)rate; - CacheEntry entry; - entry.rate = rate; - Bit32u *freqMul = entry.freqMul; - Bit32u *linearRates = entry.linearRates; - Bit32u *attackRates = entry.attackRates; +#if __cplusplus >= 201103L + std::unique_ptr<CacheEntry> entry(new CacheEntry); +#else + std::auto_ptr<CacheEntry> entry(new CacheEntry); +#endif + entry->rate = rate; + Bit32u *freqMul = entry->freqMul; + Bit32u *linearRates = entry->linearRates; + Bit32u *attackRates = entry->attackRates; //With higher octave this gets shifted up //-1 since the freqCreateTable = *2 @@ -1434,12 +1452,12 @@ static const CacheEntry &ComputeRateDependent( Bit32u rate ) attackRates[i] = 8 << RATE_SH; } - MutexHolder lock( cacheMutex ); + MutexHolder lock( cache.mutex ); if (const CacheEntry *entry = CacheLookupRateDependent( rate )) return *entry; - cache.push_back(entry); - return cache.back(); + cache.entries.push_back(entry.get()); + return *entry.release(); } void Chip::Setup( Bit32u rate ) { |