Skip to content

Commit 235fb0e

Browse files
committed
Don't allocate cubeb_device on heap
Signed-off-by: Marcin Serwin <[email protected]>
1 parent d4b494c commit 235fb0e

File tree

8 files changed

+27
-46
lines changed

8 files changed

+27
-46
lines changed

include/cubeb/cubeb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ cubeb_stream_set_name(cubeb_stream * stream, char const * stream_name);
679679
@retval CUBEB_ERROR_NOT_SUPPORTED */
680680
CUBEB_EXPORT int
681681
cubeb_stream_get_current_device(cubeb_stream * stm,
682-
cubeb_device ** const device);
682+
cubeb_device * const device);
683683

684684
/** Set input mute state for this stream. Some platforms notify the user when an
685685
application is accessing audio input. When all inputs are muted they can

src/cubeb-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct cubeb_ops {
6363
int (*stream_set_volume)(cubeb_stream * stream, float volumes);
6464
int (*stream_set_name)(cubeb_stream * stream, char const * stream_name);
6565
int (*stream_get_current_device)(cubeb_stream * stream,
66-
cubeb_device ** const device);
66+
cubeb_device * const device);
6767
int (*stream_set_input_mute)(cubeb_stream * stream, int mute);
6868
int (*stream_set_input_processing_params)(
6969
cubeb_stream * stream, cubeb_input_processing_params params);

src/cubeb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ cubeb_stream_set_name(cubeb_stream * stream, char const * stream_name)
559559

560560
int
561561
cubeb_stream_get_current_device(cubeb_stream * stream,
562-
cubeb_device ** const device)
562+
cubeb_device * const device)
563563
{
564564
if (!stream || !device) {
565565
return CUBEB_ERROR_INVALID_PARAMETER;

src/cubeb_jack.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static int
127127
cbjack_stream_device_destroy(cubeb_stream * stream, cubeb_device * device);
128128
static int
129129
cbjack_stream_get_current_device(cubeb_stream * stm,
130-
cubeb_device ** const device);
130+
cubeb_device * const device);
131131
static int
132132
cbjack_enumerate_devices(cubeb * context, cubeb_device_type type,
133133
cubeb_device_collection * collection);
@@ -1075,25 +1075,21 @@ cbjack_stream_set_volume(cubeb_stream * stm, float volume)
10751075

10761076
static int
10771077
cbjack_stream_get_current_device(cubeb_stream * stm,
1078-
cubeb_device ** const device)
1078+
cubeb_device * const device)
10791079
{
1080-
*device = (cubeb_device *)calloc(1, sizeof(cubeb_device));
1081-
if (*device == NULL)
1082-
return CUBEB_ERROR;
1083-
10841080
const char * j_in = JACK_DEFAULT_IN;
10851081
const char * j_out = JACK_DEFAULT_OUT;
10861082
const char * empty = "";
10871083

10881084
if (stm->devs == DUPLEX) {
1089-
(*device)->input_name = j_in;
1090-
(*device)->output_name = j_out;
1085+
device->input_name = j_in;
1086+
device->output_name = j_out;
10911087
} else if (stm->devs == IN_ONLY) {
1092-
(*device)->input_name = j_in;
1093-
(*device)->output_name = empty;
1088+
device->input_name = j_in;
1089+
device->output_name = empty;
10941090
} else if (stm->devs == OUT_ONLY) {
1095-
(*device)->input_name = empty;
1096-
(*device)->output_name = j_out;
1091+
device->input_name = empty;
1092+
device->output_name = j_out;
10971093
}
10981094

10991095
return CUBEB_OK;
@@ -1102,7 +1098,6 @@ cbjack_stream_get_current_device(cubeb_stream * stm,
11021098
static int
11031099
cbjack_stream_device_destroy(cubeb_stream * /*stream*/, cubeb_device * device)
11041100
{
1105-
free(device);
11061101
return CUBEB_OK;
11071102
}
11081103

src/cubeb_oss.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,22 +1306,18 @@ oss_stream_set_volume(cubeb_stream * stream, float volume)
13061306
}
13071307

13081308
static int
1309-
oss_get_current_device(cubeb_stream * stream, cubeb_device ** const device)
1309+
oss_get_current_device(cubeb_stream * stream, cubeb_device * const device)
13101310
{
1311-
*device = calloc(1, sizeof(cubeb_device));
1312-
if (*device == NULL) {
1313-
return CUBEB_ERROR;
1314-
}
1315-
(*device)->input_name = stream->record.fd != -1 ? stream->record.name : NULL;
1316-
(*device)->output_name = stream->play.fd != -1 ? stream->play.name : NULL;
1311+
device->input_name = stream->record.fd != -1 ? stream->record.name : NULL;
1312+
device->output_name = stream->play.fd != -1 ? stream->play.name : NULL;
13171313
return CUBEB_OK;
13181314
}
13191315

13201316
static int
13211317
oss_stream_device_destroy(cubeb_stream * stream, cubeb_device * device)
13221318
{
13231319
(void)stream;
1324-
free(device);
1320+
(void)device;
13251321
return CUBEB_OK;
13261322
}
13271323

src/cubeb_pulse.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,21 +1526,15 @@ pulse_device_collection_destroy(cubeb * ctx,
15261526
}
15271527

15281528
static int
1529-
pulse_stream_get_current_device(cubeb_stream * stm,
1530-
cubeb_device ** const device)
1529+
pulse_stream_get_current_device(cubeb_stream * stm, cubeb_device * const device)
15311530
{
15321531
#if PA_CHECK_VERSION(0, 9, 8)
1533-
*device = calloc(1, sizeof(cubeb_device));
1534-
if (*device == NULL)
1535-
return CUBEB_ERROR;
1536-
15371532
if (stm->input_stream) {
1538-
(*device)->input_name = WRAP(pa_stream_get_device_name)(stm->input_stream);
1533+
device->input_name = WRAP(pa_stream_get_device_name)(stm->input_stream);
15391534
}
15401535

15411536
if (stm->output_stream) {
1542-
(*device)->output_name =
1543-
WRAP(pa_stream_get_device_name)(stm->output_stream);
1537+
device->output_name = WRAP(pa_stream_get_device_name)(stm->output_stream);
15441538
}
15451539

15461540
return CUBEB_OK;
@@ -1553,7 +1547,7 @@ static int
15531547
pulse_stream_device_destroy(cubeb_stream * stream, cubeb_device * device)
15541548
{
15551549
(void)stream;
1556-
free(device);
1550+
(void)device;
15571551
return CUBEB_OK;
15581552
}
15591553

src/cubeb_sun.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -690,22 +690,18 @@ sun_stream_set_volume(cubeb_stream * stream, float volume)
690690
}
691691

692692
static int
693-
sun_get_current_device(cubeb_stream * stream, cubeb_device ** const device)
693+
sun_get_current_device(cubeb_stream * stream, cubeb_device * const device)
694694
{
695-
*device = calloc(1, sizeof(cubeb_device));
696-
if (*device == NULL) {
697-
return CUBEB_ERROR;
698-
}
699-
(*device)->input_name = stream->record.fd != -1 ? stream->record.name : NULL;
700-
(*device)->output_name = stream->play.fd != -1 ? stream->play.name : NULL;
695+
device->input_name = stream->record.fd != -1 ? stream->record.name : NULL;
696+
device->output_name = stream->play.fd != -1 ? stream->play.name : NULL;
701697
return CUBEB_OK;
702698
}
703699

704700
static int
705701
sun_stream_device_destroy(cubeb_stream * stream, cubeb_device * device)
706702
{
707703
(void)stream;
708-
free(device);
704+
(void)device;
709705
return CUBEB_OK;
710706
}
711707

test/test_devices.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ TEST(cubeb, stream_get_current_device)
247247
std::unique_ptr<cubeb_stream, decltype(&cubeb_stream_destroy)>
248248
cleanup_stream_at_exit(stream, cubeb_stream_destroy);
249249

250-
cubeb_device * device;
250+
cubeb_device device;
251251
r = cubeb_stream_get_current_device(stream, &device);
252252
if (r == CUBEB_ERROR_NOT_SUPPORTED) {
253253
fprintf(stderr, "Getting current device is not supported"
@@ -256,9 +256,9 @@ TEST(cubeb, stream_get_current_device)
256256
}
257257
ASSERT_EQ(r, CUBEB_OK) << "Error getting current devices";
258258

259-
fprintf(stdout, "Current output device: %s\n", device->output_name);
260-
fprintf(stdout, "Current input device: %s\n", device->input_name);
259+
fprintf(stdout, "Current output device: %s\n", device.output_name);
260+
fprintf(stdout, "Current input device: %s\n", device.input_name);
261261

262-
r = cubeb_stream_device_destroy(stream, device);
262+
r = cubeb_stream_device_destroy(stream, &device);
263263
ASSERT_EQ(r, CUBEB_OK) << "Error destroying current devices";
264264
}

0 commit comments

Comments
 (0)