topology: improve the printf buffer management

The commit d04e72c9a5 introduced
the dynamic printf buffer allocation for each tplg_save_printf()
call.

Introduce 'struct tplg_buf' which carries extra information about
the temporary printf buffer and the destination buffer to save allocation
requests.

The printf buffer is also allocated using 1024 bytes chunks.

A comparison between 'alloc everyting' and 'cache+chunk alloc' for the
random picked topology file:

  1: 18,620 allocs, 18,620 frees, 7,239,688 bytes allocated
  2: 12,490 allocs, 12,490 frees, 962,568 bytes allocated

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2020-08-31 19:44:46 +02:00
parent ab73253924
commit 472ab5db67
9 changed files with 130 additions and 97 deletions

View file

@ -138,7 +138,8 @@ int tplg_parse_channel(snd_tplg_t *tplg, snd_config_t *cfg,
int tplg_save_channels(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_channels(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct snd_soc_tplg_channel *channel, struct snd_soc_tplg_channel *channel,
unsigned int count, char **dst, const char *pfx) unsigned int count, struct tplg_buf *dst,
const char *pfx)
{ {
struct snd_soc_tplg_channel *c; struct snd_soc_tplg_channel *c;
const char *s; const char *s;

View file

@ -105,8 +105,8 @@ int parse_access(snd_config_t *cfg,
/* Save Access */ /* Save Access */
static int tplg_save_access(snd_tplg_t *tplg ATTRIBUTE_UNUSED, static int tplg_save_access(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct snd_soc_tplg_ctl_hdr *hdr, char **dst, struct snd_soc_tplg_ctl_hdr *hdr,
const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
const char *last; const char *last;
unsigned int j, count, access, cval; unsigned int j, count, access, cval;
@ -399,7 +399,7 @@ int tplg_parse_tlv(snd_tplg_t *tplg, snd_config_t *cfg,
/* save TLV data */ /* save TLV data */
int tplg_save_tlv(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_tlv(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_ctl_tlv *tlv = elem->tlv; struct snd_soc_tplg_ctl_tlv *tlv = elem->tlv;
struct snd_soc_tplg_tlv_dbscale *scale; struct snd_soc_tplg_tlv_dbscale *scale;
@ -557,7 +557,7 @@ int tplg_parse_control_bytes(snd_tplg_t *tplg,
/* save control bytes */ /* save control bytes */
int tplg_save_control_bytes(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_control_bytes(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_bytes_control *be = elem->bytes_ext; struct snd_soc_tplg_bytes_control *be = elem->bytes_ext;
char pfx2[16]; char pfx2[16];
@ -697,7 +697,7 @@ int tplg_parse_control_enum(snd_tplg_t *tplg, snd_config_t *cfg,
/* save control eunm */ /* save control eunm */
int tplg_save_control_enum(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_control_enum(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_enum_control *ec = elem->enum_ctrl; struct snd_soc_tplg_enum_control *ec = elem->enum_ctrl;
char pfx2[16]; char pfx2[16];
@ -858,8 +858,8 @@ int tplg_parse_control_mixer(snd_tplg_t *tplg,
} }
int tplg_save_control_mixer(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_control_mixer(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, char **dst, struct tplg_elem *elem,
const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_mixer_control *mc = elem->mixer_ctrl; struct snd_soc_tplg_mixer_control *mc = elem->mixer_ctrl;
char pfx2[16]; char pfx2[16];

View file

@ -416,7 +416,8 @@ int tplg_parse_dapm_graph(snd_tplg_t *tplg, snd_config_t *cfg,
} }
/* save DAPM graph */ /* save DAPM graph */
int tplg_save_dapm_graph(snd_tplg_t *tplg, int index, char **dst, const char *pfx) int tplg_save_dapm_graph(snd_tplg_t *tplg, int index,
struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_dapm_graph_elem *route; struct snd_soc_tplg_dapm_graph_elem *route;
struct list_head *pos; struct list_head *pos;
@ -669,7 +670,7 @@ int tplg_parse_dapm_widget(snd_tplg_t *tplg,
/* save DAPM widget */ /* save DAPM widget */
int tplg_save_dapm_widget(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_dapm_widget(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_dapm_widget *widget = elem->widget; struct snd_soc_tplg_dapm_widget *widget = elem->widget;
const char *s; const char *s;

View file

@ -121,7 +121,8 @@ int tplg_parse_refs(snd_config_t *cfg, struct tplg_elem *elem,
/* save references */ /* save references */
int tplg_save_refs(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_refs(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, unsigned int type, struct tplg_elem *elem, unsigned int type,
const char *id, char **dst, const char *pfx) const char *id, struct tplg_buf *dst,
const char *pfx)
{ {
struct tplg_ref *ref, *last; struct tplg_ref *ref, *last;
struct list_head *pos; struct list_head *pos;
@ -890,7 +891,7 @@ err:
/* save tuple set */ /* save tuple set */
static int tplg_save_tuple_set(struct tplg_vendor_tuples *tuples, static int tplg_save_tuple_set(struct tplg_vendor_tuples *tuples,
unsigned int set_index, unsigned int set_index,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct tplg_tuple_set *set; struct tplg_tuple_set *set;
struct tplg_tuple *tuple; struct tplg_tuple *tuple;
@ -1014,7 +1015,7 @@ static int parse_tuple_sets(snd_config_t *cfg,
/* save tuple sets */ /* save tuple sets */
int tplg_save_tuple_sets(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_tuple_sets(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct tplg_vendor_tuples *tuples = elem->tuples; struct tplg_vendor_tuples *tuples = elem->tuples;
unsigned int i; unsigned int i;
@ -1085,7 +1086,7 @@ int tplg_parse_tokens(snd_tplg_t *tplg, snd_config_t *cfg,
/* save vendor tokens */ /* save vendor tokens */
int tplg_save_tokens(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_tokens(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct tplg_vendor_tokens *tokens = elem->tokens; struct tplg_vendor_tokens *tokens = elem->tokens;
unsigned int i; unsigned int i;
@ -1156,7 +1157,7 @@ int tplg_parse_tuples(snd_tplg_t *tplg, snd_config_t *cfg,
/* save vendor tuples */ /* save vendor tuples */
int tplg_save_tuples(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_tuples(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
char pfx2[16]; char pfx2[16];
int err; int err;
@ -1242,7 +1243,7 @@ int tplg_parse_manifest_data(snd_tplg_t *tplg, snd_config_t *cfg,
/* save manifest data */ /* save manifest data */
int tplg_save_manifest_data(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_manifest_data(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, char **dst, struct tplg_elem *elem, struct tplg_buf *dst,
const char *pfx) const char *pfx)
{ {
struct list_head *pos; struct list_head *pos;
@ -1420,7 +1421,7 @@ int tplg_parse_data(snd_tplg_t *tplg, snd_config_t *cfg,
/* save data element */ /* save data element */
int tplg_save_data(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_data(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_private *priv = elem->data; struct snd_soc_tplg_private *priv = elem->data;
struct list_head *pos; struct list_head *pos;

View file

@ -105,8 +105,8 @@ int tplg_parse_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED, snd_config_t *cfg,
/* save control operations */ /* save control operations */
int tplg_save_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct snd_soc_tplg_ctl_hdr *hdr, char **dst, struct snd_soc_tplg_ctl_hdr *hdr,
const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
const char *s; const char *s;
int err; int err;
@ -191,7 +191,7 @@ int tplg_parse_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
/* save external control operations */ /* save external control operations */
int tplg_save_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct snd_soc_tplg_bytes_control *be, struct snd_soc_tplg_bytes_control *be,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
const char *s; const char *s;
int err; int err;

View file

@ -538,7 +538,7 @@ int tplg_parse_stream_caps(snd_tplg_t *tplg,
/* save stream caps */ /* save stream caps */
int tplg_save_stream_caps(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_stream_caps(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_stream_caps *sc = elem->stream_caps; struct snd_soc_tplg_stream_caps *sc = elem->stream_caps;
const char *s; const char *s;
@ -686,7 +686,7 @@ static int tplg_parse_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
/* Save the caps and config of a pcm stream */ /* Save the caps and config of a pcm stream */
int tplg_save_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
static const char *stream_ids[2] = { static const char *stream_ids[2] = {
"playback", "playback",
@ -778,7 +778,7 @@ static int tplg_parse_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
/* Save the caps and config of a pcm stream */ /* Save the caps and config of a pcm stream */
int tplg_save_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_pcm *pcm = elem->pcm; struct snd_soc_tplg_pcm *pcm = elem->pcm;
int err = 0; int err = 0;
@ -810,7 +810,7 @@ static int parse_flag(snd_config_t *n, unsigned int mask_in,
} }
static int save_flags(unsigned int flags, unsigned int mask, static int save_flags(unsigned int flags, unsigned int mask,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
static unsigned int flag_masks[3] = { static unsigned int flag_masks[3] = {
SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES, SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES,
@ -944,7 +944,7 @@ int tplg_parse_pcm(snd_tplg_t *tplg, snd_config_t *cfg,
/* save PCM */ /* save PCM */
int tplg_save_pcm(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_pcm(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_pcm *pcm = elem->pcm; struct snd_soc_tplg_pcm *pcm = elem->pcm;
char pfx2[16]; char pfx2[16];
@ -1081,7 +1081,7 @@ int tplg_parse_dai(snd_tplg_t *tplg, snd_config_t *cfg,
/* save DAI */ /* save DAI */
int tplg_save_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_dai *dai = elem->dai; struct snd_soc_tplg_dai *dai = elem->dai;
char pfx2[16]; char pfx2[16];
@ -1235,7 +1235,7 @@ int tplg_parse_link(snd_tplg_t *tplg, snd_config_t *cfg,
/* save physical link */ /* save physical link */
int tplg_save_link(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_link(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_link_config *link = elem->link; struct snd_soc_tplg_link_config *link = elem->link;
char pfx2[16]; char pfx2[16];
@ -1315,7 +1315,7 @@ int tplg_parse_cc(snd_tplg_t *tplg, snd_config_t *cfg,
/* save CC */ /* save CC */
int tplg_save_cc(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_cc(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_link_config *link = elem->link; struct snd_soc_tplg_link_config *link = elem->link;
char pfx2[16]; char pfx2[16];
@ -1611,7 +1611,7 @@ int tplg_parse_hw_config(snd_tplg_t *tplg, snd_config_t *cfg,
/* save hw config */ /* save hw config */
int tplg_save_hw_config(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_hw_config(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct snd_soc_tplg_hw_config *hc = elem->hw_cfg; struct snd_soc_tplg_hw_config *hc = elem->hw_cfg;
int err; int err;

View file

@ -19,25 +19,43 @@
#include "tplg_local.h" #include "tplg_local.h"
#define SAVE_ALLOC_SHIFT (13) /* 8192 bytes */ #define SAVE_ALLOC_SHIFT (13) /* 8192 bytes */
#define PRINT_BUF_SIZE (1024) #define PRINT_ALLOC_SHIFT (10) /* 1024 bytes */
#define PRINT_BUF_SIZE_MAX (1024 * 1024) #define PRINT_BUF_SIZE_MAX (1024 * 1024)
#define NEXT_CHUNK(val, shift) ((((val) >> (shift)) + 1) << (shift))
int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...) void tplg_buf_init(struct tplg_buf *buf)
{
buf->dst = NULL;
buf->dst_len = 0;
buf->printf_buf = NULL;
buf->printf_buf_size = 0;
}
void tplg_buf_free(struct tplg_buf *buf)
{
free(buf->dst);
free(buf->printf_buf);
}
char *tplg_buf_detach(struct tplg_buf *buf)
{
char *ret = buf->dst;
free(buf->printf_buf);
return ret;
}
int tplg_save_printf(struct tplg_buf *dst, const char *pfx, const char *fmt, ...)
{ {
va_list va; va_list va;
char *buf, *s; char *s;
size_t n, l, t, pl; size_t n, l, t, pl;
int ret = 0; int ret = 0;
buf = malloc(PRINT_BUF_SIZE);
if (!buf)
return -ENOMEM;
if (pfx == NULL) if (pfx == NULL)
pfx = ""; pfx = "";
va_start(va, fmt); va_start(va, fmt);
n = vsnprintf(buf, PRINT_BUF_SIZE, fmt, va); n = vsnprintf(dst->printf_buf, dst->printf_buf_size, fmt, va);
va_end(va); va_end(va);
if (n >= PRINT_BUF_SIZE_MAX) { if (n >= PRINT_BUF_SIZE_MAX) {
@ -45,42 +63,41 @@ int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
goto end; goto end;
} }
if (n >= PRINT_BUF_SIZE) { if (n >= dst->printf_buf_size) {
char *tmp = realloc(buf, n + 1); t = NEXT_CHUNK(n + 1, PRINT_ALLOC_SHIFT);
if (!tmp) { s = realloc(dst->printf_buf, t);
if (!s) {
ret = -ENOMEM; ret = -ENOMEM;
goto end; goto end;
} }
buf = tmp; dst->printf_buf = s;
dst->printf_buf_size = t;
va_start(va, fmt); va_start(va, fmt);
n = vsnprintf(buf, n + 1, fmt, va); n = vsnprintf(dst->printf_buf, n + 1, fmt, va);
va_end(va); va_end(va);
} }
pl = strlen(pfx); pl = strlen(pfx);
l = *dst ? strlen(*dst) : 0; l = dst->dst_len;
t = l + pl + n + 1; t = l + pl + n + 1;
/* allocate chunks */ /* allocate chunks */
if (*dst == NULL || if (dst->dst == NULL ||
(l >> SAVE_ALLOC_SHIFT) != (t >> SAVE_ALLOC_SHIFT)) { (l >> SAVE_ALLOC_SHIFT) != (t >> SAVE_ALLOC_SHIFT)) {
s = realloc(*dst, ((t >> SAVE_ALLOC_SHIFT) + 1) << s = realloc(dst->dst, NEXT_CHUNK(t, SAVE_ALLOC_SHIFT));
SAVE_ALLOC_SHIFT);
if (s == NULL) { if (s == NULL) {
free(*dst);
*dst = NULL;
ret = -ENOMEM; ret = -ENOMEM;
goto end; goto end;
} }
} else { } else {
s = *dst; s = dst->dst;
} }
if (pl > 0) if (pl > 0)
strcpy(s + l, pfx); strcpy(s + l, pfx);
strcpy(s + l + pl, buf); strcpy(s + l + pl, dst->printf_buf);
*dst = s; dst->dst = s;
dst->dst_len = t - 1;
end: end:
free(buf);
return ret; return ret;
} }
@ -209,7 +226,7 @@ static int tplg_check_quoted(const unsigned char *p)
return 0; return 0;
} }
static int tplg_save_quoted(char **dst, const char *str) static int tplg_save_quoted(struct tplg_buf *dst, const char *str)
{ {
static const char nibble[16] = "0123456789abcdef"; static const char nibble[16] = "0123456789abcdef";
unsigned char *p, *d, *t; unsigned char *p, *d, *t;
@ -263,7 +280,7 @@ static int tplg_save_quoted(char **dst, const char *str)
return tplg_save_printf(dst, NULL, "'%s'", d); return tplg_save_printf(dst, NULL, "'%s'", d);
} }
static int tplg_save_string(char **dst, const char *str, int id) static int tplg_save_string(struct tplg_buf *dst, const char *str, int id)
{ {
const unsigned char *p = (const unsigned char *)str; const unsigned char *p = (const unsigned char *)str;
@ -279,7 +296,7 @@ static int tplg_save_string(char **dst, const char *str, int id)
return tplg_save_printf(dst, NULL, "%s", str); return tplg_save_printf(dst, NULL, "%s", str);
} }
static int save_config(char **dst, int level, const char *delim, snd_config_t *src) static int save_config(struct tplg_buf *dst, int level, const char *delim, snd_config_t *src)
{ {
snd_config_iterator_t i, next; snd_config_iterator_t i, next;
snd_config_t *s; snd_config_t *s;
@ -400,7 +417,8 @@ retval:
return 0; return 0;
} }
static int tplg_save(snd_tplg_t *tplg, char **dst, int gindex, const char *prefix) static int tplg_save(snd_tplg_t *tplg, struct tplg_buf *dst,
int gindex, const char *prefix)
{ {
struct tplg_table *tptr; struct tplg_table *tptr;
struct tplg_elem *elem; struct tplg_elem *elem;
@ -484,8 +502,6 @@ static int tplg_save(snd_tplg_t *tplg, char **dst, int gindex, const char *prefi
return 0; return 0;
_err: _err:
free(*dst);
*dst = NULL;
return err; return err;
} }
@ -540,9 +556,9 @@ static int tplg_index_groups(snd_tplg_t *tplg, int **indexes)
int snd_tplg_save(snd_tplg_t *tplg, char **dst, int flags) int snd_tplg_save(snd_tplg_t *tplg, char **dst, int flags)
{ {
struct tplg_buf buf, buf2;
snd_input_t *in; snd_input_t *in;
snd_config_t *top, *top2; snd_config_t *top, *top2;
char *dst2;
int *indexes, *a; int *indexes, *a;
int err; int err;
@ -550,35 +566,41 @@ int snd_tplg_save(snd_tplg_t *tplg, char **dst, int flags)
assert(dst); assert(dst);
*dst = NULL; *dst = NULL;
tplg_buf_init(&buf);
if (flags & SND_TPLG_SAVE_GROUPS) { if (flags & SND_TPLG_SAVE_GROUPS) {
err = tplg_index_groups(tplg, &indexes); err = tplg_index_groups(tplg, &indexes);
if (err < 0) if (err < 0)
return err; return err;
for (a = indexes; err >= 0 && *a >= 0; a++) { for (a = indexes; err >= 0 && *a >= 0; a++) {
err = tplg_save_printf(dst, NULL, err = tplg_save_printf(&buf, NULL,
"IndexGroup.%d {\n", "IndexGroup.%d {\n",
*a); *a);
if (err >= 0) if (err >= 0)
err = tplg_save(tplg, dst, *a, "\t"); err = tplg_save(tplg, &buf, *a, "\t");
if (err >= 0) if (err >= 0)
err = tplg_save_printf(dst, NULL, "}\n"); err = tplg_save_printf(&buf, NULL, "}\n");
} }
free(indexes); free(indexes);
} else { } else {
err = tplg_save(tplg, dst, -1, NULL); err = tplg_save(tplg, &buf, -1, NULL);
} }
if (err < 0) if (err < 0)
goto _err; goto _err;
if (*dst == NULL) if (buf.dst == NULL) {
return -EINVAL; err = -EINVAL;
goto _err;
}
if (flags & SND_TPLG_SAVE_NOCHECK) if (flags & SND_TPLG_SAVE_NOCHECK) {
*dst = tplg_buf_detach(&buf);
return 0; return 0;
}
/* always load configuration - check */ /* always load configuration - check */
err = snd_input_buffer_open(&in, *dst, strlen(*dst)); err = snd_input_buffer_open(&in, buf.dst, strlen(buf.dst));
if (err < 0) { if (err < 0) {
SNDERR("could not create input buffer"); SNDERR("could not create input buffer");
goto _err; goto _err;
@ -610,20 +632,20 @@ int snd_tplg_save(snd_tplg_t *tplg, char **dst, int flags)
top = top2; top = top2;
} }
dst2 = NULL; tplg_buf_init(&buf2);
err = save_config(&dst2, 0, NULL, top); err = save_config(&buf2, 0, NULL, top);
snd_config_delete(top); snd_config_delete(top);
if (err < 0) { if (err < 0) {
SNDERR("could not save configuration"); SNDERR("could not save configuration");
goto _err; goto _err;
} }
free(*dst); tplg_buf_free(&buf);
*dst = dst2; *dst = tplg_buf_detach(&buf2);
return 0; return 0;
_err: _err:
free(*dst); tplg_buf_free(&buf);
*dst = NULL; *dst = NULL;
return err; return err;
} }

View file

@ -93,7 +93,7 @@ int tplg_parse_text(snd_tplg_t *tplg, snd_config_t *cfg,
/* save text data */ /* save text data */
int tplg_save_text(snd_tplg_t *tplg ATTRIBUTE_UNUSED, int tplg_save_text(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem, struct tplg_elem *elem,
char **dst, const char *pfx) struct tplg_buf *dst, const char *pfx)
{ {
struct tplg_texts *texts = elem->texts; struct tplg_texts *texts = elem->texts;
unsigned int i; unsigned int i;

View file

@ -200,6 +200,14 @@ struct map_elem {
int id; int id;
}; };
/* output buffer */
struct tplg_buf {
char *dst;
size_t dst_len;
char *printf_buf;
size_t printf_buf_size;
};
/* mapping table */ /* mapping table */
struct tplg_table { struct tplg_table {
const char *name; const char *name;
@ -214,9 +222,9 @@ struct tplg_table {
void (*free)(void *); void (*free)(void *);
int (*parse)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv); int (*parse)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
int (*save)(snd_tplg_t *tplg, struct tplg_elem *elem, int (*save)(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *prefix); struct tplg_buf *dst, const char *prefix);
int (*gsave)(snd_tplg_t *tplg, int index, int (*gsave)(snd_tplg_t *tplg, int index,
char **dst, const char *prefix); struct tplg_buf *dst, const char *prefix);
int (*decod)(snd_tplg_t *tplg, size_t pos, int (*decod)(snd_tplg_t *tplg, size_t pos,
struct snd_soc_tplg_hdr *hdr, struct snd_soc_tplg_hdr *hdr,
void *bin, size_t size); void *bin, size_t size);
@ -348,49 +356,49 @@ int tplg_add_dai_object(snd_tplg_t *tplg, snd_tplg_obj_template_t *t);
int tplg_nice_value_format(char *dst, size_t dst_size, unsigned int value); int tplg_nice_value_format(char *dst, size_t dst_size, unsigned int value);
int tplg_save_printf(char **dst, const char *prefix, const char *fmt, ...); int tplg_save_printf(struct tplg_buf *dst, const char *prefix, const char *fmt, ...);
int tplg_save_refs(snd_tplg_t *tplg, struct tplg_elem *elem, unsigned int type, int tplg_save_refs(snd_tplg_t *tplg, struct tplg_elem *elem, unsigned int type,
const char *id, char **dst, const char *pfx); const char *id, struct tplg_buf *dst, const char *pfx);
int tplg_save_channels(snd_tplg_t *tplg, struct snd_soc_tplg_channel *channel, int tplg_save_channels(snd_tplg_t *tplg, struct snd_soc_tplg_channel *channel,
unsigned int channel_count, char **dst, const char *pfx); unsigned int channel_count, struct tplg_buf *dst, const char *pfx);
int tplg_save_ops(snd_tplg_t *tplg, struct snd_soc_tplg_ctl_hdr *hdr, int tplg_save_ops(snd_tplg_t *tplg, struct snd_soc_tplg_ctl_hdr *hdr,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_ext_ops(snd_tplg_t *tplg, struct snd_soc_tplg_bytes_control *be, int tplg_save_ext_ops(snd_tplg_t *tplg, struct snd_soc_tplg_bytes_control *be,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_manifest_data(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_manifest_data(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_control_mixer(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_control_mixer(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_control_enum(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_control_enum(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_control_bytes(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_control_bytes(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_tlv(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_tlv(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_data(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_data(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_text(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_text(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_tokens(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_tokens(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_tuples(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_tuples(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_dapm_graph(snd_tplg_t *tplg, int index, int tplg_save_dapm_graph(snd_tplg_t *tplg, int index,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_dapm_widget(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_dapm_widget(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_link(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_link(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_cc(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_cc(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_pcm(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_pcm(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_hw_config(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_hw_config(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_stream_caps(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_stream_caps(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_save_dai(snd_tplg_t *tplg, struct tplg_elem *elem, int tplg_save_dai(snd_tplg_t *tplg, struct tplg_elem *elem,
char **dst, const char *pfx); struct tplg_buf *dst, const char *pfx);
int tplg_decode_template(snd_tplg_t *tplg, int tplg_decode_template(snd_tplg_t *tplg,
size_t pos, size_t pos,