audio: Clean up some CloseDevice() interface details.

- It's now always called if device->hidden isn't NULL, even if OpenDevice()
  failed halfway through. This lets implementation code not have to clean up
  itself on every possible failure point; just return an error and SDL will
  handle it for you.

- Implementations can assume this->hidden != NULL and not check for it.

- implementations don't have to set this->hidden = NULL when done, because
  the caller is always about to free(this).

- Don't reset other fields that are in a block of memory about to be free()'d.

- Implementations all now free things like internal mix buffers last, after
  closing devices and such, to guarantee they definitely aren't in use anymore
  at the point of deallocation.
This commit is contained in:
Ryan C. Gordon
2016-08-05 01:44:41 -04:00
parent 30a9139bc3
commit 9b64772775
24 changed files with 171 additions and 373 deletions

View File

@@ -228,16 +228,11 @@ PAUDIO_GetDeviceBuf(_THIS)
static void
PAUDIO_CloseDevice(_THIS)
{
if (this->hidden != NULL) {
SDL_FreeAudioMem(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
if (this->hidden->audio_fd >= 0) {
close(this->hidden->audio_fd);
this->hidden->audio_fd = -1;
}
SDL_free(this->hidden);
this->hidden = NULL;
if (this->hidden->audio_fd >= 0) {
close(this->hidden->audio_fd);
}
SDL_FreeAudioMem(this->hidden->mixbuf);
SDL_free(this->hidden);
}
static int
@@ -268,7 +263,6 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
fd = OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
this->hidden->audio_fd = fd;
if (fd < 0) {
PAUDIO_CloseDevice(this);
return SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
}
@@ -277,7 +271,6 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
* that we can have.
*/
if (ioctl(fd, AUDIO_BUFFER, &paud_bufinfo) < 0) {
PAUDIO_CloseDevice(this);
return SDL_SetError("Couldn't get audio buffer information");
}
@@ -391,7 +384,6 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
#ifdef DEBUG_AUDIO
fprintf(stderr, "Couldn't find any hardware audio formats\n");
#endif
PAUDIO_CloseDevice(this);
return SDL_SetError("Couldn't find any hardware audio formats");
}
this->spec.format = test_format;
@@ -449,7 +441,6 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
}
if (err != NULL) {
PAUDIO_CloseDevice(this);
return SDL_SetError("Paudio: %s", err);
}
@@ -457,7 +448,6 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
this->hidden->mixlen = this->spec.size;
this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
if (this->hidden->mixbuf == NULL) {
PAUDIO_CloseDevice(this);
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
@@ -492,7 +482,6 @@ PAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
paud_control.ioctl_request = AUDIO_START;
paud_control.position = 0;
if (ioctl(fd, AUDIO_CONTROL, &paud_control) < 0) {
PAUDIO_CloseDevice(this);
#ifdef DEBUG_AUDIO
fprintf(stderr, "Can't start audio play\n");
#endif