Use a sorted list for unused buffers

Helps ensure the buffers being unloaded due to cache limits are not likely to
be needed anytime soon.
openmw-38
Chris Robinson 9 years ago
parent 669b7a2295
commit caae305ddd

@ -223,16 +223,15 @@ namespace MWSound
std::cerr<< "No unused sound buffers to free, using "<<mBufferCacheSize<<" bytes!" <<std::endl; std::cerr<< "No unused sound buffers to free, using "<<mBufferCacheSize<<" bytes!" <<std::endl;
break; break;
} }
SoundSet::iterator iter = mUnusedBuffers.begin(); Sound_Buffer *unused = &mSoundBuffers[mUnusedBuffers.back()];
Sound_Buffer *unused = &mSoundBuffers[*iter];
mBufferCacheSize -= mOutput->getSoundDataSize(unused->mHandle); mBufferCacheSize -= mOutput->getSoundDataSize(unused->mHandle);
mOutput->unloadSound(unused->mHandle); mOutput->unloadSound(unused->mHandle);
unused->mHandle = 0; unused->mHandle = 0;
mUnusedBuffers.erase(iter); mUnusedBuffers.pop_back();
} }
mUnusedBuffers.insert(sfxid); mUnusedBuffers.push_front(sfxid);
} }
return sfx; return sfx;
@ -520,7 +519,11 @@ namespace MWSound
volume * sfx->mVolume, basevol, pitch, mode|type, offset volume * sfx->mVolume, basevol, pitch, mode|type, offset
); );
if(sfx->mReferences++ == 0) if(sfx->mReferences++ == 0)
mUnusedBuffers.erase(sfxid); {
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfxid);
if(iter != mUnusedBuffers.end())
mUnusedBuffers.erase(iter);
}
mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, sfxid)); mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, sfxid));
} }
catch(std::exception&) catch(std::exception&)
@ -552,7 +555,11 @@ namespace MWSound
objpos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, mode|type, offset objpos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, mode|type, offset
); );
if(sfx->mReferences++ == 0) if(sfx->mReferences++ == 0)
mUnusedBuffers.erase(sfxid); {
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfxid);
if(iter != mUnusedBuffers.end())
mUnusedBuffers.erase(iter);
}
if((mode&Play_NoTrack)) if((mode&Play_NoTrack))
mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, sfxid)); mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, sfxid));
else else
@ -582,7 +589,11 @@ namespace MWSound
initialPos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, mode|type, offset initialPos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, mode|type, offset
); );
if(sfx->mReferences++ == 0) if(sfx->mReferences++ == 0)
mUnusedBuffers.erase(sfxid); {
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfxid);
if(iter != mUnusedBuffers.end())
mUnusedBuffers.erase(iter);
}
mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, sfxid)); mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, sfxid));
} }
catch(std::exception &) catch(std::exception &)
@ -829,7 +840,7 @@ namespace MWSound
{ {
Sound_Buffer *sfx = &mSoundBuffers[sndidx->second]; Sound_Buffer *sfx = &mSoundBuffers[sndidx->second];
if(sfx->mReferences-- == 1) if(sfx->mReferences-- == 1)
mUnusedBuffers.insert(sndidx->second); mUnusedBuffers.push_front(sndidx->second);
sndidx = snditer->second.erase(sndidx); sndidx = snditer->second.erase(sndidx);
} }
else else
@ -1042,7 +1053,7 @@ namespace MWSound
sndidx->first->stop(); sndidx->first->stop();
Sound_Buffer *sfx = &mSoundBuffers[sndidx->second]; Sound_Buffer *sfx = &mSoundBuffers[sndidx->second];
if(sfx->mReferences-- == 1) if(sfx->mReferences-- == 1)
mUnusedBuffers.insert(sndidx->second); mUnusedBuffers.push_front(sndidx->second);
} }
} }
mActiveSounds.clear(); mActiveSounds.clear();

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <deque>
#include <map> #include <map>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
@ -62,8 +63,9 @@ namespace MWSound
typedef std::map<std::string,Sound_Loudness> NameLoudnessMap; typedef std::map<std::string,Sound_Loudness> NameLoudnessMap;
NameLoudnessMap mVoiceLipBuffers; NameLoudnessMap mVoiceLipBuffers;
typedef std::set<size_t> SoundSet; // NOTE: unused buffers are stored in front-newest order.
SoundSet mUnusedBuffers; typedef std::deque<size_t> SoundList;
SoundList mUnusedBuffers;
boost::shared_ptr<Sound> mMusic; boost::shared_ptr<Sound> mMusic;
std::string mCurrentPlaylist; std::string mCurrentPlaylist;

Loading…
Cancel
Save