foxygit / doom Log in
commit 97ddedc959ecf361cc7ff2d9055aa4899252bccb
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat Mar 5 19:35:39 2011 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat Mar 5 19:35:39 2011 +0000

    Add configuration parameter to limit the amount of memory used for
    cached soundss.

    Subversion-branch: /branches/strife-branch
    Subversion-revision: 2290
---
 src/i_sdlsound.c | 29 +++++++++++++++++++++++++++++
 src/i_sound.c    |  6 ++++++
 src/i_sound.h    |  1 +
 src/m_config.c   |  7 +++++++
 4 files changed, 43 insertions(+)

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
@@ -696,6 +696,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.