diff options
author | Eric Wong <normalperson@yhbt.net> | 2007-12-02 23:15:36 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-09-18 04:51:48 -0700 |
commit | 421acb4ec492cc01c94b16950db811b38376dd90 (patch) | |
tree | ff801c677f0314cc35715150e6bb1d974daa0492 | |
parent | 71ac281579c8b8b384cfc09d8627b549b1854fcd (diff) | |
download | mpd-421acb4ec492cc01c94b16950db811b38376dd90.tar.gz mpd-421acb4ec492cc01c94b16950db811b38376dd90.tar.xz mpd-421acb4ec492cc01c94b16950db811b38376dd90.zip |
oss: avoid heap allocations when configuring mixer
Noticed-by: Courtney Cavin
-rw-r--r-- | src/volume.c | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/src/volume.c b/src/volume.c index 6d8411165..2d3eb6d3e 100644 --- a/src/volume.c +++ b/src/volume.c @@ -33,6 +33,7 @@ #ifdef HAVE_ALSA #include <alsa/asoundlib.h> #endif +#include <alloca.h> #define VOLUME_MIXER_TYPE_SOFTWARE 0 #define VOLUME_MIXER_TYPE_OSS 1 @@ -77,6 +78,8 @@ static int volume_alsaSet = -1; #ifdef HAVE_OSS +#include <alloca.h> /* only alloca user in mpd atm, may change ... */ + static void closeOssMixer(void) { while (close(volume_ossFd) && errno == EINTR) ; @@ -92,12 +95,9 @@ static int prepOssMixer(const char *device) return -1; } - param = getConfigParam(CONF_MIXER_CONTROL); - - if (param) { + if ((param = getConfigParam(CONF_MIXER_CONTROL))) { const char *labels[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS; - char *duplicated; - int i, j; + int i; int devmask = 0; if (ioctl(volume_ossFd, SOUND_MIXER_READ_DEVMASK, &devmask) < 0) { @@ -107,16 +107,16 @@ static int prepOssMixer(const char *device) } for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) { - duplicated = xstrdup(labels[i]); + ssize_t len = strlen(labels[i]); + char *duplicated = alloca(len + 1); + /* eliminate spaces at the end */ - j = strlen(duplicated) - 1; - while (j >= 0 && duplicated[j] == ' ') - duplicated[j--] = '\0'; - if (strcasecmp(duplicated, param->value) == 0) { - free(duplicated); + memcpy(duplicated, labels[i], len + 1); + len -= 2; + while (len >= 0 && duplicated[len] == ' ') + duplicated[len--] = '\0'; + if (strcasecmp(duplicated, param->value) == 0) break; - } - free(duplicated); } if (i >= SOUND_MIXER_NRDEVICES) { |