[sound] Check for allocation failures

They're currently treated as non-fatal, those sounds just won't ever
play. This allows ad_tears to at least load with only 32MB of locked
memory (it needs somewhere between 64 and 96).
This commit is contained in:
Bill Currie 2022-06-05 01:42:30 +09:00
parent 4694ee693b
commit 2298227546
3 changed files with 19 additions and 5 deletions

View file

@ -114,10 +114,14 @@ SND_Memory_AllocBuffer (unsigned samples)
// buffer (just the header), but Z_TagMalloc // does not
// +4 for sentinel
sfxbuffer_t *buffer = Z_TagMalloc (snd_zone, size + 4, 1);
// place a sentinel at the end of the buffer for added safety
memcpy (&buffer->data[samples], "\xde\xad\xbe\xef", 4);
// clear buffer header
memset (buffer, 0, sizeof (sfxbuffer_t));
if (buffer) {
// place a sentinel at the end of the buffer for added safety
memcpy (&buffer->data[samples], "\xde\xad\xbe\xef", 4);
// clear buffer header
memset (buffer, 0, sizeof (sfxbuffer_t));
} else {
Sys_Printf ("Sound: out of memory: %uMB exhausted\n", snd_mem_size);
}
return buffer;
}

View file

@ -146,6 +146,11 @@ SND_SFX_StreamOpen (sfx_t *sfx, void *file,
stream = calloc (1, sizeof (sfxstream_t));
new_sfx->data.stream = stream;
stream->buffer = SND_Memory_AllocBuffer (frames * info->channels);
if (!stream->buffer) {
free (stream);
free (new_sfx);
return 0;
}
stream->file = file;
stream->sfx = new_sfx;
stream->ll_read = read;

View file

@ -74,21 +74,26 @@ wav_callback_load (sfxblock_t *block)
fdata_ofs = (info->datalen + sizeof (float) - 1) & ~(sizeof (float) - 1);
len = fdata_ofs + info->frames * info->channels * sizeof (float);
data = malloc (len);
if (!data)
goto bail;
fdata = (float *) (data + fdata_ofs);
Qread (file, data, info->datalen);
Qclose (file);
SND_Convert (data, fdata, info->frames, info->channels, info->width);
unsigned buffer_frames = SND_ResamplerFrames (sfx);
buffer = SND_Memory_AllocBuffer (buffer_frames * info->channels);
if (!buffer)
goto bail;
buffer->size = buffer_frames * info->channels;
buffer->sfx = sfx;
SND_SetPaint (buffer);
SND_SetupResampler (buffer, 0);
SND_Resample (buffer, fdata, info->frames);
buffer->head = buffer->size;
bail:
free (data);
Qclose (file);
return buffer;
}