int tplg_save_channels(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
const char *s;
/* Save Access */
static int tplg_save_access(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
- struct snd_soc_tplg_ctl_hdr *hdr, char **dst,
- const char *pfx)
+ struct snd_soc_tplg_ctl_hdr *hdr,
+ struct tplg_buf *dst, const char *pfx)
{
const char *last;
unsigned int j, count, access, cval;
/* save TLV data */
int tplg_save_tlv(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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_tlv_dbscale *scale;
/* save control bytes */
int tplg_save_control_bytes(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
char pfx2[16];
/* save control eunm */
int tplg_save_control_enum(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
char pfx2[16];
}
int tplg_save_control_mixer(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
- struct tplg_elem *elem, char **dst,
- const char *pfx)
+ struct tplg_elem *elem,
+ struct tplg_buf *dst, const char *pfx)
{
struct snd_soc_tplg_mixer_control *mc = elem->mixer_ctrl;
char pfx2[16];
}
/* 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 list_head *pos;
/* save DAPM widget */
int tplg_save_dapm_widget(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
const char *s;
/* save references */
int tplg_save_refs(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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 list_head *pos;
/* save tuple set */
static int tplg_save_tuple_set(struct tplg_vendor_tuples *tuples,
unsigned int set_index,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct tplg_tuple_set *set;
struct tplg_tuple *tuple;
/* save tuple sets */
int tplg_save_tuple_sets(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct tplg_vendor_tuples *tuples = elem->tuples;
unsigned int i;
/* save vendor tokens */
int tplg_save_tokens(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct tplg_vendor_tokens *tokens = elem->tokens;
unsigned int i;
/* save vendor tuples */
int tplg_save_tuples(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
char pfx2[16];
int err;
/* save manifest data */
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)
{
struct list_head *pos;
/* save data element */
int tplg_save_data(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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 list_head *pos;
/* save control operations */
int tplg_save_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
- struct snd_soc_tplg_ctl_hdr *hdr, char **dst,
- const char *pfx)
+ struct snd_soc_tplg_ctl_hdr *hdr,
+ struct tplg_buf *dst, const char *pfx)
{
const char *s;
int err;
/* save external control operations */
int tplg_save_ext_ops(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct snd_soc_tplg_bytes_control *be,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
const char *s;
int err;
/* save stream caps */
int tplg_save_stream_caps(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
const char *s;
/* Save the caps and config of a pcm stream */
int tplg_save_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
static const char *stream_ids[2] = {
"playback",
/* Save the caps and config of a pcm stream */
int tplg_save_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct snd_soc_tplg_pcm *pcm = elem->pcm;
int err = 0;
}
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] = {
SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES,
/* save PCM */
int tplg_save_pcm(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct snd_soc_tplg_pcm *pcm = elem->pcm;
char pfx2[16];
/* save DAI */
int tplg_save_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct snd_soc_tplg_dai *dai = elem->dai;
char pfx2[16];
/* save physical link */
int tplg_save_link(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
char pfx2[16];
/* save CC */
int tplg_save_cc(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
char pfx2[16];
/* save hw config */
int tplg_save_hw_config(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
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;
int err;
#include "tplg_local.h"
#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 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;
- char *buf, *s;
+ char *s;
size_t n, l, t, pl;
int ret = 0;
- buf = malloc(PRINT_BUF_SIZE);
- if (!buf)
- return -ENOMEM;
-
if (pfx == NULL)
pfx = "";
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);
if (n >= PRINT_BUF_SIZE_MAX) {
goto end;
}
- if (n >= PRINT_BUF_SIZE) {
- char *tmp = realloc(buf, n + 1);
- if (!tmp) {
+ if (n >= dst->printf_buf_size) {
+ t = NEXT_CHUNK(n + 1, PRINT_ALLOC_SHIFT);
+ s = realloc(dst->printf_buf, t);
+ if (!s) {
ret = -ENOMEM;
goto end;
}
- buf = tmp;
+ dst->printf_buf = s;
+ dst->printf_buf_size = t;
va_start(va, fmt);
- n = vsnprintf(buf, n + 1, fmt, va);
+ n = vsnprintf(dst->printf_buf, n + 1, fmt, va);
va_end(va);
}
pl = strlen(pfx);
- l = *dst ? strlen(*dst) : 0;
+ l = dst->dst_len;
t = l + pl + n + 1;
/* allocate chunks */
- if (*dst == NULL ||
+ if (dst->dst == NULL ||
(l >> SAVE_ALLOC_SHIFT) != (t >> SAVE_ALLOC_SHIFT)) {
- s = realloc(*dst, ((t >> SAVE_ALLOC_SHIFT) + 1) <<
- SAVE_ALLOC_SHIFT);
+ s = realloc(dst->dst, NEXT_CHUNK(t, SAVE_ALLOC_SHIFT));
if (s == NULL) {
- free(*dst);
- *dst = NULL;
ret = -ENOMEM;
goto end;
}
} else {
- s = *dst;
+ s = dst->dst;
}
if (pl > 0)
strcpy(s + l, pfx);
- strcpy(s + l + pl, buf);
- *dst = s;
+ strcpy(s + l + pl, dst->printf_buf);
+ dst->dst = s;
+ dst->dst_len = t - 1;
end:
- free(buf);
return ret;
}
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";
unsigned char *p, *d, *t;
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;
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_t *s;
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_elem *elem;
return 0;
_err:
- free(*dst);
- *dst = NULL;
return err;
}
int snd_tplg_save(snd_tplg_t *tplg, char **dst, int flags)
{
+ struct tplg_buf buf, buf2;
snd_input_t *in;
snd_config_t *top, *top2;
- char *dst2;
int *indexes, *a;
int err;
assert(dst);
*dst = NULL;
+ tplg_buf_init(&buf);
+
if (flags & SND_TPLG_SAVE_GROUPS) {
err = tplg_index_groups(tplg, &indexes);
if (err < 0)
return err;
for (a = indexes; err >= 0 && *a >= 0; a++) {
- err = tplg_save_printf(dst, NULL,
+ err = tplg_save_printf(&buf, NULL,
"IndexGroup.%d {\n",
*a);
if (err >= 0)
- err = tplg_save(tplg, dst, *a, "\t");
+ err = tplg_save(tplg, &buf, *a, "\t");
if (err >= 0)
- err = tplg_save_printf(dst, NULL, "}\n");
+ err = tplg_save_printf(&buf, NULL, "}\n");
}
free(indexes);
} else {
- err = tplg_save(tplg, dst, -1, NULL);
+ err = tplg_save(tplg, &buf, -1, NULL);
}
if (err < 0)
goto _err;
- if (*dst == NULL)
- return -EINVAL;
+ if (buf.dst == NULL) {
+ err = -EINVAL;
+ goto _err;
+ }
- if (flags & SND_TPLG_SAVE_NOCHECK)
+ if (flags & SND_TPLG_SAVE_NOCHECK) {
+ *dst = tplg_buf_detach(&buf);
return 0;
+ }
/* 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) {
SNDERR("could not create input buffer");
goto _err;
top = top2;
}
- dst2 = NULL;
- err = save_config(&dst2, 0, NULL, top);
+ tplg_buf_init(&buf2);
+ err = save_config(&buf2, 0, NULL, top);
snd_config_delete(top);
if (err < 0) {
SNDERR("could not save configuration");
goto _err;
}
- free(*dst);
- *dst = dst2;
+ tplg_buf_free(&buf);
+ *dst = tplg_buf_detach(&buf2);
return 0;
_err:
- free(*dst);
+ tplg_buf_free(&buf);
*dst = NULL;
return err;
}
/* save text data */
int tplg_save_text(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem,
- char **dst, const char *pfx)
+ struct tplg_buf *dst, const char *pfx)
{
struct tplg_texts *texts = elem->texts;
unsigned int i;
int id;
};
+/* output buffer */
+struct tplg_buf {
+ char *dst;
+ size_t dst_len;
+ char *printf_buf;
+ size_t printf_buf_size;
+};
+
/* mapping table */
struct tplg_table {
const char *name;
void (*free)(void *);
int (*parse)(snd_tplg_t *tplg, snd_config_t *cfg, void *priv);
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,
- char **dst, const char *prefix);
+ struct tplg_buf *dst, const char *prefix);
int (*decod)(snd_tplg_t *tplg, size_t pos,
struct snd_soc_tplg_hdr *hdr,
void *bin, size_t size);
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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- char **dst, const char *pfx);
+ struct tplg_buf *dst, const char *pfx);
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,
- char **dst, const char *pfx);
+ struct tplg_buf *dst, const char *pfx);
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,
- char **dst, const char *pfx);
+ struct tplg_buf *dst, const char *pfx);
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,
- 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,
- char **dst, const char *pfx);
+ struct tplg_buf *dst, const char *pfx);
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,
- char **dst, const char *pfx);
+ struct tplg_buf *dst, const char *pfx);
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,
- 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,
- char **dst, const char *pfx);
+ struct tplg_buf *dst, const char *pfx);
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,
size_t pos,