shithub: choc

Download patch

ref: 97ddedc959ecf361cc7ff2d9055aa4899252bccb
parent: 43f6dc56eda1aa1c32d0af6dfdec8cf1b1f478eb
author: Simon Howard <fraggle@gmail.com>
date: Sat Mar 5 14:35:39 EST 2011

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

Subversion-branch: /branches/strife-branch
Subversion-revision: 2290

--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -169,11 +169,40 @@
     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.
--- 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 @@
     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);
--- a/src/i_sound.h
+++ b/src/i_sound.h
@@ -232,6 +232,7 @@
 extern int snd_sfxdevice;
 extern int snd_musicdevice;
 extern int snd_samplerate;
+extern int snd_cachesize;
 
 void I_BindSoundVariables(void);
 
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -697,6 +697,13 @@
     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.
     //