summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Howard2011-03-05 19:35:39 +0000
committerSimon Howard2011-03-05 19:35:39 +0000
commit97ddedc959ecf361cc7ff2d9055aa4899252bccb (patch)
tree1ac40ce546c02a20e10ac58ea5e45487df24ee58
parent43f6dc56eda1aa1c32d0af6dfdec8cf1b1f478eb (diff)
downloadchocolate-doom-97ddedc959ecf361cc7ff2d9055aa4899252bccb.tar.gz
chocolate-doom-97ddedc959ecf361cc7ff2d9055aa4899252bccb.tar.bz2
chocolate-doom-97ddedc959ecf361cc7ff2d9055aa4899252bccb.zip
Add configuration parameter to limit the amount of memory used for
cached soundss. Subversion-branch: /branches/strife-branch Subversion-revision: 2290
-rw-r--r--src/i_sdlsound.c29
-rw-r--r--src/i_sound.c6
-rw-r--r--src/i_sound.h1
-rw-r--r--src/m_config.c7
4 files changed, 43 insertions, 0 deletions
diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c
index 7165f01b..03e9d85b 100644
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -169,12 +169,41 @@ static boolean FindAndFreeSound(void)
return false;
}
+// Enforce SFX cache size limit. We are just about to allocate "len"
+// bytes on the heap for a new sound effect, so free up some space
+// so that we keep allocated_sounds_size < snd_cachesize
+
+static void ReserveCacheSpace(size_t len)
+{
+ if (snd_cachesize <= 0)
+ {
+ return;
+ }
+
+ // Keep freeing sound effects that aren't currently being played,
+ // until there is enough space for the new sound.
+
+ while (allocated_sounds_size + len > snd_cachesize)
+ {
+ // Free a sound. If there is nothing more to free, stop.
+
+ if (!FindAndFreeSound())
+ {
+ break;
+ }
+ }
+}
+
// Allocate a block for a new sound effect.
static Mix_Chunk *AllocateSound(sfxinfo_t *sfxinfo, size_t len)
{
allocated_sound_t *snd;
+ // Keep allocated sounds within the cache size.
+
+ ReserveCacheSpace(len);
+
// Allocate the sound structure and data. The data will immediately
// follow the structure, which acts as a header.
diff --git a/src/i_sound.c b/src/i_sound.c
index 0c771dc2..2e9e71db 100644
--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -41,6 +41,11 @@
int snd_samplerate = 44100;
+// Maximum number of bytes to dedicate to allocated sound effects.
+// (Default: 64MB)
+
+int snd_cachesize = 64 * 1024 * 1024;
+
// Low-level sound and music modules we are using
static sound_module_t *sound_module;
@@ -412,6 +417,7 @@ void I_BindSoundVariables(void)
M_BindVariable("snd_sbdma", &snd_sbdma);
M_BindVariable("snd_mport", &snd_mport);
M_BindVariable("snd_samplerate", &snd_samplerate);
+ M_BindVariable("snd_cachesize", &snd_cachesize);
M_BindVariable("opl_io_port", &opl_io_port);
#ifdef FEATURE_SOUND
M_BindVariable("use_libsamplerate", &use_libsamplerate);
diff --git a/src/i_sound.h b/src/i_sound.h
index d8c4f38a..e4062959 100644
--- a/src/i_sound.h
+++ b/src/i_sound.h
@@ -232,6 +232,7 @@ boolean I_MusicIsPlaying(void);
extern int snd_sfxdevice;
extern int snd_musicdevice;
extern int snd_samplerate;
+extern int snd_cachesize;
void I_BindSoundVariables(void);
diff --git a/src/m_config.c b/src/m_config.c
index bdc72a0a..0772893a 100644
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -697,6 +697,13 @@ static default_t extra_defaults_list[] =
CONFIG_VARIABLE_INT(snd_samplerate),
//!
+ // Maximum number of bytes to allocate for caching converted sound
+ // effects in memory. If set to zero, there is no limit applied.
+ //
+
+ CONFIG_VARIABLE_INT(snd_cachesize),
+
+ //!
// The I/O port to use to access the OPL chip. Only relevant when
// using native OPL music playback.
//