// 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) { // Keep freeing sound effects that aren't currently being played, // until there is enough space for the new sound. while (allocated_sounds_size + len > CACHESIZE) { // Free a sound. If there is nothing more to free, stop. if (!FindAndFreeSound()) break; } }
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. do { snd = malloc(sizeof(allocated_sound_t) + len); // Out of memory? Try to free an old sound, then loop round // and try again. if (snd == NULL && !FindAndFreeSound()) { return NULL; } } while (snd == NULL); // Skip past the chunk structure for the audio buffer snd->chunk.abuf = (byte *) (snd + 1); snd->chunk.alen = len; snd->chunk.allocated = 1; snd->chunk.volume = MIX_MAX_VOLUME; 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; AllocatedSoundLink(snd); return &snd->chunk; }