shithub: choc

Download patch

ref: f6deaba729b678fa92d3edddf1b1b1ea944f78fc
parent: ad116c05d87fe788cd459df2b1c229bfcf6928a4
author: Jonathan Dowland <jon+github@alcopop.org>
date: Wed Jun 24 16:59:59 EDT 2015

Replace ->driver_data with GetAllocatedSoundBySfxinfo

GetAllocatedSoundBySfxinfo is a linked-list look-up on the
list of allocated sounds, rather than the direct-access of the
driver_data pointer.

Previously we used the ->driver_data field of sfxinfo_t to point at the
corresponding allocated_sound_t, but this assumes a 1:1 mapping which
won't be true with pitch-shifted sounds.

The lookups are still cheap, but I did some instrumenting to see
how many iterations this results in. Note that when I ran these tests
there were a few more calls that I've since eliminated, so this is
slightly worse than current. For Doom 2 v1.9 demos:

        ____demo:___
 iter     1   2    3
    0   296 636 1691
    1   104 205 620
    2   57  134 488
    3   55  90  287
    4   58  56  204
    5   28  48  146
    6   18  33  119
    7   6   44  99
    8   4   33  86
    9   8   57  72
    10  6   30  62
    11  6   18  68
    12  6   22  46
    13  2   18  42
    14  4   18  48
    15  0   6   22
    16  4   12  16
    17  0   12  14
    18  0   8   12
    19  0   12  14
    20  0   16  12
    21  0   10  10
    22  0   2   20
    23  0   10  10
    24  0   2   6
    25  0   2   8
    26  0   0   10
    27  0   0   4
    28  0   0   6
    29  0   0   2
    30  0   0   12
    31  0   0   8
    32  0   0   4
    33  0   0   2
    34  0   0   4
    35  0   0   0
    36  0   0   0
    37  0   0   0

--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -136,10 +136,6 @@
 
     AllocatedSoundUnlink(snd);
 
-    // Unlink from higher-level code.
-
-    snd->sfxinfo->driver_data = NULL;
-
     // Keep track of the amount of allocated sound data:
 
     allocated_sounds_size -= snd->chunk.alen;
@@ -235,10 +231,6 @@
     snd->sfxinfo = sfxinfo;
     snd->use_count = 0;
 
-    // driver_data pointer points to the allocated_sound structure.
-
-    sfxinfo->driver_data = snd;
-
     // Keep track of how much memory all these cached sounds are using...
 
     allocated_sounds_size += len;
@@ -279,6 +271,24 @@
     //printf("-- %s: Use count=%i\n", snd->sfxinfo->name, snd->use_count);
 }
 
+// Search through the list of allocated sounds and return the one that matches
+// the supplied sfxinfo entry.
+
+static allocated_sound_t * GetAllocatedSoundBySfxInfo(sfxinfo_t *sfxinfo)
+{
+    allocated_sound_t * p = allocated_sounds_head;
+
+    while(p != NULL)
+    {
+        if(p->sfxinfo == sfxinfo)
+        {
+            return p;
+        }
+        p = p->next;
+    }
+    return NULL;
+}
+
 // When a sound stops, check if it is still playing.  If it is not,
 // we can mark the sound data as CACHE to be freed back for other
 // means.
@@ -787,8 +797,7 @@
 static boolean LockSound(sfxinfo_t *sfxinfo)
 {
     // If the sound isn't loaded, load it now
-
-    if (sfxinfo->driver_data == NULL)
+    if (GetAllocatedSoundBySfxInfo(sfxinfo) == NULL)
     {
         if (!CacheSFX(sfxinfo))
         {
@@ -796,7 +805,7 @@
         }
     }
 
-    LockAllocatedSound(sfxinfo->driver_data);
+    LockAllocatedSound(GetAllocatedSoundBySfxInfo(sfxinfo));
 
     return true;
 }
@@ -879,7 +888,7 @@
         return -1;
     }
 
-    snd = sfxinfo->driver_data;
+    snd = GetAllocatedSoundBySfxInfo(sfxinfo);
 
     // play sound