From 28b6ce39d3d88d0159b54c8ca558e993eac93056 Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 24 Nov 2014 19:56:01 +0100 Subject: audio: make mp_chmap_to_str() return a stack-allocated string Simplifies memory management. --- audio/audio.c | 7 +++---- audio/chmap.c | 26 ++++++++++++++------------ audio/chmap.h | 4 +++- audio/out/ao.c | 5 ++--- audio/out/ao_alsa.c | 8 ++------ player/command.c | 2 +- 6 files changed, 25 insertions(+), 27 deletions(-) diff --git a/audio/audio.c b/audio/audio.c index 0458e813e7..2cda1ba110 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -93,10 +93,9 @@ bool mp_audio_config_valid(const struct mp_audio *mpa) char *mp_audio_fmt_to_str(int srate, const struct mp_chmap *chmap, int format) { - char *chstr = mp_chmap_to_str(chmap); - char *res = talloc_asprintf(NULL, "%dHz %s %dch %s", srate, chstr, - chmap->num, af_fmt_to_str(format)); - talloc_free(chstr); + char *res = talloc_asprintf(NULL, "%dHz %s %dch %s", srate, + mp_chmap_to_str(chmap), chmap->num, + af_fmt_to_str(format)); return res; } diff --git a/audio/chmap.c b/audio/chmap.c index a4d06bb700..5396d40657 100644 --- a/audio/chmap.c +++ b/audio/chmap.c @@ -18,6 +18,7 @@ #include #include +#include "common/common.h" #include "common/msg.h" #include "chmap.h" @@ -376,34 +377,35 @@ void mp_chmap_get_reorder(int dst[MP_NUM_CHANNELS], const struct mp_chmap *from, // Returns something like "fl-fr-fc". If there's a standard layout in lavc // order, return that, e.g. "3.0" instead of "fl-fr-fc". // Unassigned but valid speakers get names like "sp28". -char *mp_chmap_to_str(const struct mp_chmap *src) +char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src) { - char *res = talloc_strdup(NULL, ""); + buf[0] = '\0'; - if (mp_chmap_is_unknown(src)) - return talloc_asprintf_append_buffer(res, "unknown%d", src->num); + if (mp_chmap_is_unknown(src)) { + snprintf(buf, buf_size, "unknown%d", src->num); + return buf; + } for (int n = 0; n < src->num; n++) { int sp = src->speaker[n]; const char *s = sp < MP_SPEAKER_ID_COUNT ? speaker_names[sp][0] : NULL; - char buf[10]; + char sp_buf[10]; if (!s) { - snprintf(buf, sizeof(buf), "sp%d", sp); - s = buf; + snprintf(sp_buf, sizeof(sp_buf), "sp%d", sp); + s = sp_buf; } - res = talloc_asprintf_append_buffer(res, "%s%s", n > 0 ? "-" : "", s); + mp_snprintf_cat(buf, buf_size, "%s%s", n > 0 ? "-" : "", s); } // To standard layout name for (int n = 0; std_layout_names[n][0]; n++) { - if (res && strcmp(res, std_layout_names[n][1]) == 0) { - talloc_free(res); - res = talloc_strdup(NULL, std_layout_names[n][0]); + if (strcmp(buf, std_layout_names[n][1]) == 0) { + snprintf(buf, buf_size, "%s", std_layout_names[n][0]); break; } } - return res; + return buf; } // If src can be parsed as channel map (as produced by mp_chmap_to_str()), diff --git a/audio/chmap.h b/audio/chmap.h index c7851a8657..d0b9806ff5 100644 --- a/audio/chmap.h +++ b/audio/chmap.h @@ -120,7 +120,9 @@ void mp_chmap_reorder_to_lavc(struct mp_chmap *map); void mp_chmap_get_reorder(int dst[MP_NUM_CHANNELS], const struct mp_chmap *from, const struct mp_chmap *to); -char *mp_chmap_to_str(const struct mp_chmap *src); +char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src); +#define mp_chmap_to_str(m) mp_chmap_to_str_buf((char[64]){0}, 64, (m)) + bool mp_chmap_from_str(struct mp_chmap *dst, bstr src); struct mp_log; diff --git a/audio/out/ao.c b/audio/out/ao.c index f0461b8c09..893699e2f5 100644 --- a/audio/out/ao.c +++ b/audio/out/ao.c @@ -214,10 +214,9 @@ static struct ao *ao_alloc_pb(bool probing, struct mpv_global *global, static int ao_init(struct ao *ao) { - char *chmap = mp_chmap_to_str(&ao->channels); MP_VERBOSE(ao, "requested format: %d Hz, %s channels, %s\n", - ao->samplerate, chmap, af_fmt_to_str(ao->format)); - talloc_free(chmap); + ao->samplerate, mp_chmap_to_str(&ao->channels), + af_fmt_to_str(ao->format)); ao->api = ao->driver->play ? &ao_api_push : &ao_api_pull; ao->api_priv = talloc_zero_size(ao, ao->api->priv_size); diff --git a/audio/out/ao_alsa.c b/audio/out/ao_alsa.c index 910ac98437..01414d63fc 100644 --- a/audio/out/ao_alsa.c +++ b/audio/out/ao_alsa.c @@ -317,10 +317,8 @@ static const char *select_chmap(struct ao *ao) return device_channel_layouts[n][0]; } - char *name = mp_chmap_to_str(&ao->channels); MP_ERR(ao, "channel layout %s (%d ch) not supported.\n", - name, ao->channels.num); - talloc_free(name); + mp_chmap_to_str(&ao->channels), ao->channels.num); return "default"; } @@ -587,9 +585,7 @@ static int init(struct ao *ao) for (int c = 0; c < chmap.num; c++) chmap.speaker[c] = find_mp_channel(alsa_chmap->pos[c]); - char *mp_map_str = mp_chmap_to_str(&chmap); - MP_VERBOSE(ao, "which we understand as: %s\n", mp_map_str); - talloc_free(mp_map_str); + MP_VERBOSE(ao, "which we understand as: %s\n", mp_chmap_to_str(&chmap)); if (mp_chmap_is_valid(&chmap) && chmap.num == ao->channels.num) { MP_VERBOSE(ao, "using the ALSA channel map.\n"); diff --git a/player/command.c b/player/command.c index 90adf003eb..ec4f2030bb 100644 --- a/player/command.c +++ b/player/command.c @@ -1571,7 +1571,7 @@ static int mp_property_channels(void *ctx, struct m_property *prop, return M_PROPERTY_UNAVAILABLE; switch (action) { case M_PROPERTY_PRINT: - *(char **) arg = mp_chmap_to_str(&fmt.channels); + *(char **) arg = talloc_strdup(NULL, mp_chmap_to_str(&fmt.channels)); return M_PROPERTY_OK; case M_PROPERTY_GET: *(int *)arg = fmt.channels.num; -- cgit v1.2.3