Skip to content

Commit 4be8b20

Browse files
committed
Revert "audio: plug leak and skip redundant copy in mixer add_stream"
This reverts commit 27ab9e7.
1 parent 9faa654 commit 4be8b20

4 files changed

Lines changed: 7 additions & 45 deletions

File tree

audio/audio_driver.c

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ bool audio_driver_mixer_add_stream(audio_mixer_stream_params_t *params)
14191419
void *buf = NULL;
14201420

14211421
if (params->stream_type == AUDIO_STREAM_TYPE_NONE)
1422-
goto error;
1422+
return false;
14231423

14241424
switch (params->slot_selection_type)
14251425
{
@@ -1436,27 +1436,17 @@ bool audio_driver_mixer_add_stream(audio_mixer_stream_params_t *params)
14361436
default:
14371437
if (!audio_driver_mixer_get_free_stream_slot(
14381438
&free_slot, params->stream_type))
1439-
goto error;
1439+
return false;
14401440
break;
14411441
}
14421442

14431443
if (params->state == AUDIO_STREAM_STATE_NONE)
1444-
goto error;
1444+
return false;
14451445

1446-
/* If the caller transferred buffer ownership, use it directly;
1447-
* otherwise make a private copy. The OGG/FLAC/MP3/MOD loaders
1448-
* retain the pointer for streaming decode, so taking ownership
1449-
* avoids a needless per-sound duplication and a matching leak
1450-
* (the original caller-owned buffer has nowhere to be freed,
1451-
* since the mixer frees only the internal copy). */
1452-
if (params->buf_owned)
1453-
buf = params->buf;
1454-
else
1455-
{
1456-
if (!(buf = malloc(params->bufsize)))
1457-
return false;
1458-
memcpy(buf, params->buf, params->bufsize);
1459-
}
1446+
if (!(buf = malloc(params->bufsize)))
1447+
return false;
1448+
1449+
memcpy(buf, params->buf, params->bufsize);
14601450

14611451
switch (params->type)
14621452
{
@@ -1531,14 +1521,6 @@ bool audio_driver_mixer_add_stream(audio_mixer_stream_params_t *params)
15311521
audio_driver_st.mixer_streams_playing++;
15321522

15331523
return true;
1534-
1535-
error:
1536-
/* On early failure (bad params / no slot), honor the ownership
1537-
* contract: if the caller transferred buf to us, we free it so
1538-
* the caller does not double-free and does not leak. */
1539-
if (params->buf_owned)
1540-
free(params->buf);
1541-
return false;
15421524
}
15431525

15441526
enum audio_mixer_state audio_driver_mixer_get_stream_state(unsigned i)

audio/audio_driver.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ typedef struct audio_mixer_stream_params
7373
enum audio_mixer_stream_type stream_type;
7474
enum audio_mixer_type type;
7575
enum audio_mixer_state state;
76-
/* If true, add_stream takes ownership of buf and the caller must
77-
* not free or otherwise reference it after the call returns. The
78-
* buffer will be free()d either directly (WAV, once converted to
79-
* PCM) or when the mixer sound is destroyed (OGG/FLAC/MP3/MOD,
80-
* which retain the buffer for streaming decode).
81-
* If false, add_stream copies buf internally and the caller retains
82-
* ownership of the original memory. */
83-
bool buf_owned;
8476
} audio_mixer_stream_params_t;
8577
#endif
8678

tasks/task_audio_mixer.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ static void task_audio_mixer_handle_upload_ogg(retro_task_t *task,
116116
params.bufsize = img->bufsize;
117117
params.cb = NULL;
118118
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
119-
params.buf_owned = true;
120119

121120
audio_driver_mixer_add_stream(&params);
122121

@@ -148,7 +147,6 @@ static void task_audio_mixer_handle_upload_ogg_and_play(retro_task_t *task,
148147
params.bufsize = img->bufsize;
149148
params.cb = NULL;
150149
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
151-
params.buf_owned = true;
152150

153151
audio_driver_mixer_add_stream(&params);
154152

@@ -180,7 +178,6 @@ static void task_audio_mixer_handle_upload_flac(retro_task_t *task,
180178
params.bufsize = img->bufsize;
181179
params.cb = NULL;
182180
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
183-
params.buf_owned = true;
184181

185182
audio_driver_mixer_add_stream(&params);
186183

@@ -212,7 +209,6 @@ static void task_audio_mixer_handle_upload_flac_and_play(retro_task_t *task,
212209
params.bufsize = img->bufsize;
213210
params.cb = NULL;
214211
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
215-
params.buf_owned = true;
216212

217213
audio_driver_mixer_add_stream(&params);
218214

@@ -244,7 +240,6 @@ static void task_audio_mixer_handle_upload_mp3(retro_task_t *task,
244240
params.bufsize = img->bufsize;
245241
params.cb = NULL;
246242
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
247-
params.buf_owned = true;
248243

249244
audio_driver_mixer_add_stream(&params);
250245

@@ -276,7 +271,6 @@ static void task_audio_mixer_handle_upload_mp3_and_play(retro_task_t *task,
276271
params.bufsize = img->bufsize;
277272
params.cb = NULL;
278273
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
279-
params.buf_owned = true;
280274

281275
audio_driver_mixer_add_stream(&params);
282276

@@ -308,7 +302,6 @@ static void task_audio_mixer_handle_upload_mod(retro_task_t *task,
308302
params.bufsize = img->bufsize;
309303
params.cb = NULL;
310304
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
311-
params.buf_owned = true;
312305

313306
audio_driver_mixer_add_stream(&params);
314307

@@ -340,7 +333,6 @@ static void task_audio_mixer_handle_upload_mod_and_play(retro_task_t *task,
340333
params.bufsize = img->bufsize;
341334
params.cb = NULL;
342335
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
343-
params.buf_owned = true;
344336

345337
audio_driver_mixer_add_stream(&params);
346338

@@ -373,7 +365,6 @@ static void task_audio_mixer_handle_upload_wav(retro_task_t *task,
373365
params.bufsize = img->bufsize;
374366
params.cb = NULL;
375367
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
376-
params.buf_owned = true;
377368

378369
audio_driver_mixer_add_stream(&params);
379370

@@ -405,7 +396,6 @@ static void task_audio_mixer_handle_upload_wav_and_play(retro_task_t *task,
405396
params.bufsize = img->bufsize;
406397
params.cb = NULL;
407398
params.basename = (img->path && *img->path) ? strdup(path_basename_nocompression(img->path)) : NULL;
408-
params.buf_owned = true;
409399

410400
audio_driver_mixer_add_stream(&params);
411401

tasks/task_translation.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,6 @@ static void handle_translation_response(
558558
params.bufsize = response->sound_size;
559559
params.cb = NULL;
560560
params.basename = NULL;
561-
/* Caller frees response->sound_data below; request a copy. */
562-
params.buf_owned = false;
563561

564562
audio_driver_mixer_add_stream(&params);
565563
}

0 commit comments

Comments
 (0)