[alsa-devel] [PATCH 03/29] mixer: remove alloca() from elem_read_volume()

Takashi Sakamoto o-takashi at sakamocchi.jp
Fri Jul 15 02:23:07 CEST 2016


Both of alloca() and automatic variables keep storages on stack, while
the former generates more instructions than the latter. It's better to use
the latter if the size of storage is computable at pre-compile or compile
time; i.e. just for structures.

This commit obsolete usages of alloca() with automatic variables.

Signed-off-by: Takashi Sakamoto <o-takashi at sakamocchi.jp>
---
 src/mixer/simple_none.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/mixer/simple_none.c b/src/mixer/simple_none.c
index a90ee84..0f23a06 100644
--- a/src/mixer/simple_none.c
+++ b/src/mixer/simple_none.c
@@ -234,12 +234,11 @@ static long from_user(selem_none_t *s, int dir, selem_ctl_t *c, long value)
 
 static int elem_read_volume(selem_none_t *s, int dir, selem_ctl_type_t type)
 {
-	snd_ctl_elem_value_t *ctl;
+	snd_ctl_elem_value_t ctl = {0};
 	unsigned int idx;
 	int err;
 	selem_ctl_t *c = &s->ctls[type];
-	snd_ctl_elem_value_alloca(&ctl);
-	if ((err = snd_hctl_elem_read(c->elem, ctl)) < 0)
+	if ((err = snd_hctl_elem_read(c->elem, &ctl)) < 0)
 		return err;
 	for (idx = 0; idx < s->str[dir].channels; idx++) {
 		unsigned int idx1 = idx;
@@ -247,25 +246,24 @@ static int elem_read_volume(selem_none_t *s, int dir, selem_ctl_type_t type)
 			idx1 = 0;
 		s->str[dir].vol[idx] =
 			to_user(s, dir, c,
-				snd_ctl_elem_value_get_integer(ctl, idx1));
+				snd_ctl_elem_value_get_integer(&ctl, idx1));
 	}
 	return 0;
 }
 
 static int elem_read_switch(selem_none_t *s, int dir, selem_ctl_type_t type)
 {
-	snd_ctl_elem_value_t *ctl;
+	snd_ctl_elem_value_t ctl = {0};
 	unsigned int idx;
 	int err;
 	selem_ctl_t *c = &s->ctls[type];
-	snd_ctl_elem_value_alloca(&ctl);
-	if ((err = snd_hctl_elem_read(c->elem, ctl)) < 0)
+	if ((err = snd_hctl_elem_read(c->elem, &ctl)) < 0)
 		return err;
 	for (idx = 0; idx < s->str[dir].channels; idx++) {
 		unsigned int idx1 = idx;
 		if (idx >= c->values)
 			idx1 = 0;
-		if (!snd_ctl_elem_value_get_integer(ctl, idx1))
+		if (!snd_ctl_elem_value_get_integer(&ctl, idx1))
 			s->str[dir].sw &= ~(1 << idx);
 	}
 	return 0;
-- 
2.7.4



More information about the Alsa-devel mailing list