spa: add macro to simplify array iterations some more

uint32_t i;
	for (i = 0; i < SPA_N_ELEMENTS(some_array); i++)
		.. stuff with some_array[i].foo ...

   becomes:

	SPA_FOR_EACH_ELEMENT_VAR(some_array, p)
		.. stuff with p->foo ..
This commit is contained in:
Wim Taymans 2022-09-30 16:21:28 +02:00
parent 365ebcda9b
commit d22feab92a
21 changed files with 113 additions and 150 deletions

View file

@ -867,10 +867,9 @@ static const struct chmap_info chmap_info[] = {
static enum snd_pcm_chmap_position channel_to_chmap(enum spa_audio_channel channel)
{
uint32_t i;
for (i = 0; i < SPA_N_ELEMENTS(chmap_info); i++)
if (chmap_info[i].channel == channel)
return chmap_info[i].pos;
SPA_FOR_EACH_ELEMENT_VAR(chmap_info, info)
if (info->channel == channel)
return info->pos;
return SND_CHMAP_UNKNOWN;
}

View file

@ -913,23 +913,19 @@ static const struct format_info format_info[] = {
static const struct format_info *format_info_from_media_type(uint32_t type,
uint32_t subtype, uint32_t format)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) {
if ((format_info[i].media_type == type) &&
(format_info[i].media_subtype == subtype) &&
(format == 0 || format_info[i].format == format))
return &format_info[i];
}
SPA_FOR_EACH_ELEMENT_VAR(format_info, i)
if ((i->media_type == type) &&
(i->media_subtype == subtype) &&
(format == 0 || i->format == format))
return i;
return NULL;
}
static const struct format_info *format_info_from_fourcc(uint32_t fourcc)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) {
if (format_info[i].fourcc == fourcc)
return &format_info[i];
}
SPA_FOR_EACH_ELEMENT_VAR(format_info, i)
if (i->fourcc == fourcc)
return i;
return NULL;
}

View file

@ -141,6 +141,9 @@ struct spa_fraction {
#define SPA_FOR_EACH_ELEMENT(arr, ptr) \
for ((ptr) = arr; (void*)(ptr) < SPA_PTROFF(arr, sizeof(arr), void); (ptr)++)
#define SPA_FOR_EACH_ELEMENT_VAR(arr, var) \
for (__typeof__((arr)[0])* (var) = arr; (void*)(var) < SPA_PTROFF(arr, sizeof(arr), void); (var)++)
#define SPA_ABS(a) \
({ \
__typeof__(a) _a = (a); \

View file

@ -216,7 +216,7 @@ static int emit_info(struct impl *this, bool full)
{
int err = 0;
struct spa_dict_item *items;
uint32_t i, n_items;
uint32_t n_items;
const struct acp_dict_item *it;
struct acp_card *card = this->card;
char path[128];
@ -241,10 +241,10 @@ static int emit_info(struct impl *this, bool full)
#undef ADD_ITEM
if (this->info.change_mask & SPA_DEVICE_CHANGE_MASK_PARAMS) {
for (i = 0; i < SPA_N_ELEMENTS(this->params); i++) {
if (this->params[i].user > 0) {
this->params[i].flags ^= SPA_PARAM_INFO_SERIAL;
this->params[i].user = 0;
SPA_FOR_EACH_ELEMENT_VAR(this->params, p) {
if (p->user > 0) {
p->flags ^= SPA_PARAM_INFO_SERIAL;
p->user = 0;
}
}
}

View file

@ -638,12 +638,10 @@ static const struct format_info format_info[] = {
static snd_pcm_format_t spa_format_to_alsa(uint32_t format, bool *planar)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) {
*planar = format_info[i].spa_pformat == format;
if (format_info[i].spa_format == format || *planar)
return format_info[i].format;
SPA_FOR_EACH_ELEMENT_VAR(format_info, i) {
*planar = i->spa_pformat == format;
if (i->spa_format == format || *planar)
return i->format;
}
return SND_PCM_FORMAT_UNKNOWN;
}
@ -969,7 +967,7 @@ static int enum_pcm_formats(struct state *state, uint32_t index, uint32_t *next,
struct spa_pod **result, struct spa_pod_builder *b)
{
int res, err;
size_t i, j;
size_t j;
snd_pcm_t *hndl;
snd_pcm_hw_params_t *params;
struct spa_pod_frame f[2];
@ -1020,8 +1018,10 @@ static int enum_pcm_formats(struct state *state, uint32_t index, uint32_t *next,
spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_None, 0);
choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[1]);
for (i = 1, j = 0; i < SPA_N_ELEMENTS(format_info); i++) {
const struct format_info *fi = &format_info[i];
j = 0;
SPA_FOR_EACH_ELEMENT_VAR(format_info, fi) {
if (fi->format == SND_PCM_FORMAT_UNKNOWN)
continue;
if (snd_pcm_format_mask_test(fmask, fi->format)) {
if ((snd_pcm_access_mask_test(amask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED) ||

View file

@ -241,16 +241,15 @@ static void set_volume(struct impl *this);
static void emit_node_info(struct impl *this, bool full)
{
uint64_t old = full ? this->info.change_mask : 0;
uint32_t i;
if (full)
this->info.change_mask = this->info_all;
if (this->info.change_mask) {
if (this->info.change_mask & SPA_NODE_CHANGE_MASK_PARAMS) {
for (i = 0; i < SPA_N_ELEMENTS(this->params); i++) {
if (this->params[i].user > 0) {
this->params[i].flags ^= SPA_PARAM_INFO_SERIAL;
this->params[i].user = 0;
SPA_FOR_EACH_ELEMENT_VAR(this->params, p) {
if (p->user > 0) {
p->flags ^= SPA_PARAM_INFO_SERIAL;
p->user = 0;
}
}
}
@ -262,7 +261,6 @@ static void emit_node_info(struct impl *this, bool full)
static void emit_port_info(struct impl *this, struct port *port, bool full)
{
uint64_t old = full ? port->info.change_mask : 0;
uint32_t i;
if (full)
port->info.change_mask = port->info_all;
@ -282,10 +280,10 @@ static void emit_port_info(struct impl *this, struct port *port, bool full)
port->info.props = &SPA_DICT_INIT(items, n_items);
if (port->info.change_mask & SPA_PORT_CHANGE_MASK_PARAMS) {
for (i = 0; i < SPA_N_ELEMENTS(port->params); i++) {
if (port->params[i].user > 0) {
port->params[i].flags ^= SPA_PARAM_INFO_SERIAL;
port->params[i].user = 0;
SPA_FOR_EACH_ELEMENT_VAR(port->params, p) {
if (p->user > 0) {
p->flags ^= SPA_PARAM_INFO_SERIAL;
p->user = 0;
}
}
}
@ -432,7 +430,6 @@ static int impl_node_enum_params(void *object, int seq,
{
struct props *p = &this->props;
struct spa_pod_frame f[2];
uint32_t i;
switch (result.index) {
case 0:
@ -596,9 +593,9 @@ static int impl_node_enum_params(void *object, int seq,
spa_pod_builder_prop(&b, SPA_PROP_INFO_labels, 0);
spa_pod_builder_push_struct(&b, &f[1]);
for (i = 0; i < SPA_N_ELEMENTS(channelmix_upmix_info); i++) {
spa_pod_builder_string(&b, channelmix_upmix_info[i].label);
spa_pod_builder_string(&b, channelmix_upmix_info[i].description);
SPA_FOR_EACH_ELEMENT_VAR(channelmix_upmix_info, i) {
spa_pod_builder_string(&b, i->label);
spa_pod_builder_string(&b, i->description);
}
spa_pod_builder_pop(&b, &f[1]);
param = spa_pod_builder_pop(&b, &f[0]);
@ -646,9 +643,9 @@ static int impl_node_enum_params(void *object, int seq,
0);
spa_pod_builder_prop(&b, SPA_PROP_INFO_labels, 0);
spa_pod_builder_push_struct(&b, &f[1]);
for (i = 0; i < SPA_N_ELEMENTS(dither_method_info); i++) {
spa_pod_builder_string(&b, dither_method_info[i].label);
spa_pod_builder_string(&b, dither_method_info[i].description);
SPA_FOR_EACH_ELEMENT_VAR(dither_method_info, i) {
spa_pod_builder_string(&b, i->label);
spa_pod_builder_string(&b, i->description);
}
spa_pod_builder_pop(&b, &f[1]);
param = spa_pod_builder_pop(&b, &f[0]);

View file

@ -105,21 +105,17 @@ static void run_test1(const char *name, const char *impl, bool in_packed, bool o
static void run_testc(const char *name, const char *impl, bool in_packed, bool out_packed, convert_func_t func,
int channel_count)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(sample_sizes); i++) {
SPA_FOR_EACH_ELEMENT_VAR(sample_sizes, s) {
run_test1(name, impl, in_packed, out_packed, func, channel_count,
(sample_sizes[i] + (channel_count -1)) / channel_count);
(*s + (channel_count -1)) / channel_count);
}
}
static void run_test(const char *name, const char *impl, bool in_packed, bool out_packed, convert_func_t func)
{
size_t i, j;
for (i = 0; i < SPA_N_ELEMENTS(sample_sizes); i++) {
for (j = 0; j < SPA_N_ELEMENTS(channel_counts); j++) {
run_test1(name, impl, in_packed, out_packed, func, channel_counts[j],
(sample_sizes[i] + (channel_counts[j] -1)) / channel_counts[j]);
SPA_FOR_EACH_ELEMENT_VAR(sample_sizes, s) {
SPA_FOR_EACH_ELEMENT_VAR(channel_counts, c) {
run_test1(name, impl, in_packed, out_packed, func, *c, (*s + (*c -1)) / *c);
}
}
}

View file

@ -108,19 +108,18 @@ static const struct channelmix_info {
static const struct channelmix_info *find_channelmix_info(uint32_t src_chan, uint64_t src_mask,
uint32_t dst_chan, uint64_t dst_mask, uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(channelmix_table); i++) {
if (!MATCH_CPU_FLAGS(channelmix_table[i].cpu_flags, cpu_flags))
SPA_FOR_EACH_ELEMENT_VAR(channelmix_table, info) {
if (!MATCH_CPU_FLAGS(info->cpu_flags, cpu_flags))
continue;
if (src_chan == dst_chan && src_mask == dst_mask)
return &channelmix_table[i];
return info;
if (MATCH_CHAN(channelmix_table[i].src_chan, src_chan) &&
MATCH_CHAN(channelmix_table[i].dst_chan, dst_chan) &&
MATCH_MASK(channelmix_table[i].src_mask, src_mask) &&
MATCH_MASK(channelmix_table[i].dst_mask, dst_mask))
return &channelmix_table[i];
if (MATCH_CHAN(info->src_chan, src_chan) &&
MATCH_CHAN(info->dst_chan, dst_chan) &&
MATCH_MASK(info->src_mask, src_mask) &&
MATCH_MASK(info->dst_mask, dst_mask))
return info;
}
return NULL;
}

View file

@ -359,14 +359,13 @@ static struct conv_info conv_table[] =
static const struct conv_info *find_conv_info(uint32_t src_fmt, uint32_t dst_fmt,
uint32_t n_channels, uint32_t cpu_flags, uint32_t conv_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(conv_table); i++) {
if (conv_table[i].src_fmt == src_fmt &&
conv_table[i].dst_fmt == dst_fmt &&
MATCH_CHAN(conv_table[i].n_channels, n_channels) &&
MATCH_CPU_FLAGS(conv_table[i].cpu_flags, cpu_flags) &&
MATCH_DITHER(conv_table[i].conv_flags, conv_flags))
return &conv_table[i];
SPA_FOR_EACH_ELEMENT_VAR(conv_table, c) {
if (c->src_fmt == src_fmt &&
c->dst_fmt == dst_fmt &&
MATCH_CHAN(c->n_channels, n_channels) &&
MATCH_CPU_FLAGS(c->cpu_flags, cpu_flags) &&
MATCH_DITHER(c->conv_flags, conv_flags))
return c;
}
return NULL;
}
@ -403,11 +402,10 @@ static struct noise_info noise_table[] =
static const struct noise_info *find_noise_info(uint32_t method,
uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(noise_table); i++) {
if (noise_table[i].method == method &&
MATCH_CPU_FLAGS(noise_table[i].cpu_flags, cpu_flags))
return &noise_table[i];
SPA_FOR_EACH_ELEMENT_VAR(noise_table, t) {
if (t->method == method &&
MATCH_CPU_FLAGS(t->cpu_flags, cpu_flags))
return t;
}
return NULL;
}
@ -471,17 +469,14 @@ static const struct dither_info {
static const struct dither_info *find_dither_info(uint32_t method, uint32_t rate)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(dither_info); i++) {
const struct dither_info *di = &dither_info[i];
SPA_FOR_EACH_ELEMENT_VAR(dither_info, di) {
if (di->method != method)
continue;
/* don't use shaped for too low rates, it moves the noise to
* audible ranges */
if (di->ns != NULL && rate < di->rate * 3 / 4)
return find_dither_info(DITHER_METHOD_TRIANGULAR_HF, rate);
return &dither_info[i];
return di;
}
return NULL;
}

View file

@ -271,10 +271,9 @@ static const struct dither_method_info {
static inline uint32_t dither_method_from_label(const char *label)
{
uint32_t i;
for (i = 0; i < SPA_N_ELEMENTS(dither_method_info); i++) {
if (spa_streq(dither_method_info[i].label, label))
return dither_method_info[i].method;
SPA_FOR_EACH_ELEMENT_VAR(dither_method_info, i) {
if (spa_streq(i->label, label))
return i->method;
}
return DITHER_METHOD_NONE;
}

View file

@ -59,11 +59,9 @@ static const struct peaks_info {
static const struct peaks_info *find_peaks_info(uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(peaks_table); i++) {
if (!MATCH_CPU_FLAGS(peaks_table[i].cpu_flags, cpu_flags))
continue;
return &peaks_table[i];
SPA_FOR_EACH_ELEMENT_VAR(peaks_table, t) {
if (MATCH_CPU_FLAGS(t->cpu_flags, cpu_flags))
return t;
}
return NULL;
}

View file

@ -125,11 +125,10 @@ static struct resample_info resample_table[] =
#define MATCH_CPU_FLAGS(a,b) ((a) == 0 || ((a) & (b)) == a)
static const struct resample_info *find_resample_info(uint32_t format, uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(resample_table); i++) {
if (resample_table[i].format == format &&
MATCH_CPU_FLAGS(resample_table[i].cpu_flags, cpu_flags))
return &resample_table[i];
SPA_FOR_EACH_ELEMENT_VAR(resample_table, t) {
if (t->format == format &&
MATCH_CPU_FLAGS(t->cpu_flags, cpu_flags))
return t;
}
return NULL;
}

View file

@ -56,11 +56,9 @@ static const struct volume_info {
static const struct volume_info *find_volume_info(uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(volume_table); i++) {
if (!MATCH_CPU_FLAGS(volume_table[i].cpu_flags, cpu_flags))
continue;
return &volume_table[i];
SPA_FOR_EACH_ELEMENT_VAR(volume_table, t) {
if (MATCH_CPU_FLAGS(t->cpu_flags, cpu_flags))
return t;
}
return NULL;
}

View file

@ -98,13 +98,11 @@ static struct mix_info mix_table[] =
static const struct mix_info *find_mix_info(uint32_t fmt,
uint32_t n_channels, uint32_t cpu_flags)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(mix_table); i++) {
if (mix_table[i].fmt == fmt &&
MATCH_CHAN(mix_table[i].n_channels, n_channels) &&
MATCH_CPU_FLAGS(mix_table[i].cpu_flags, cpu_flags))
return &mix_table[i];
SPA_FOR_EACH_ELEMENT_VAR(mix_table, t) {
if (t->fmt == fmt &&
MATCH_CHAN(t->n_channels, n_channels) &&
MATCH_CPU_FLAGS(t->cpu_flags, cpu_flags))
return t;
}
return NULL;
}

View file

@ -206,11 +206,11 @@ static int codec_enum_config(const struct media_codec *codec, uint32_t flags,
spa_pod_builder_push_choice(b, &f[1], SPA_CHOICE_None, 0);
choice = (struct spa_pod_choice*)spa_pod_builder_frame(b, &f[1]);
i = 0;
for (size_t j = 0; j < SPA_N_ELEMENTS(aac_frequencies); j++) {
if (AAC_GET_FREQUENCY(conf) & aac_frequencies[j].config) {
SPA_FOR_EACH_ELEMENT_VAR(aac_frequencies, f) {
if (AAC_GET_FREQUENCY(conf) & f->config) {
if (i++ == 0)
spa_pod_builder_int(b, aac_frequencies[j].value);
spa_pod_builder_int(b, aac_frequencies[j].value);
spa_pod_builder_int(b, f->value);
spa_pod_builder_int(b, f->value);
}
}
if (i == 0)
@ -272,14 +272,15 @@ static int codec_validate_config(const struct media_codec *codec, uint32_t flags
if (!(conf.object_type & (AAC_OBJECT_TYPE_MPEG2_AAC_LC |
AAC_OBJECT_TYPE_MPEG4_AAC_LC)))
return -EINVAL;
for (j = 0; j < SPA_N_ELEMENTS(aac_frequencies); ++j) {
if (AAC_GET_FREQUENCY(conf) & aac_frequencies[j].config) {
info->info.raw.rate = aac_frequencies[j].value;
j = 0;
SPA_FOR_EACH_ELEMENT_VAR(aac_frequencies, f) {
if (AAC_GET_FREQUENCY(conf) & f->config) {
info->info.raw.rate = f->value;
j++;
break;
}
}
if (j == SPA_N_ELEMENTS(aac_frequencies))
if (j == 0)
return -EINVAL;
if (conf.channels & AAC_CHANNELS_2) {

View file

@ -37,12 +37,9 @@ static const struct extension extensions[] = {
const struct extension *extension_find(uint32_t index, const char *name)
{
const struct extension *ext;
SPA_FOR_EACH_ELEMENT(extensions, ext) {
SPA_FOR_EACH_ELEMENT_VAR(extensions, ext) {
if (index == ext->index || spa_streq(name, ext->name))
return ext;
}
return NULL;
}

View file

@ -380,8 +380,7 @@ static AvahiStringList *get_service_txt(const struct service *s)
txt = avahi_string_list_add_pair(txt, "channel_map", channel_map_snprint(cm, sizeof(cm), &s->cm));
txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[s->subtype]);
const struct mapping *m;
SPA_FOR_EACH_ELEMENT(mappings, m) {
SPA_FOR_EACH_ELEMENT_VAR(mappings, m) {
const char *value = pw_properties_get(s->props, m->pw_key);
if (value != NULL)
txt = avahi_string_list_add_pair(txt, m->txt_key, value);

View file

@ -262,14 +262,11 @@ static bool do_quit(struct data *data, const char *cmd, char *args, char **error
static bool do_help(struct data *data, const char *cmd, char *args, char **error)
{
size_t i;
printf("Available commands:\n");
for (i = 0; i < SPA_N_ELEMENTS(command_list); i++) {
SPA_FOR_EACH_ELEMENT_VAR(command_list, c) {
char cmd[256];
snprintf(cmd, sizeof(cmd), "%s | %s",
command_list[i].name, command_list[i].alias);
printf("\t%-20.20s\t%s\n", cmd, command_list[i].description);
snprintf(cmd, sizeof(cmd), "%s | %s", c->name, c->alias);
printf("\t%-20.20s\t%s\n", cmd, c->description);
}
return true;
}
@ -1312,11 +1309,10 @@ static const struct class *classes[] =
static const struct class *find_class(const char *type, uint32_t version)
{
size_t i;
for (i = 0; i < SPA_N_ELEMENTS(classes); i++) {
if (spa_streq(classes[i]->type, type) &&
classes[i]->version <= version)
return classes[i];
SPA_FOR_EACH_ELEMENT_VAR(classes, c) {
if (spa_streq((*c)->type, type) &&
(*c)->version <= version)
return *c;
}
return NULL;
}
@ -2110,7 +2106,6 @@ static bool parse(struct data *data, char *buf, char **error)
{
char *a[2];
int n;
size_t i;
char *p, *cmd, *args;
if ((p = strchr(buf, '#')))
@ -2128,10 +2123,10 @@ static bool parse(struct data *data, char *buf, char **error)
cmd = a[0];
args = n > 1 ? a[1] : "";
for (i = 0; i < SPA_N_ELEMENTS(command_list); i++) {
if (spa_streq(command_list[i].name, cmd) ||
spa_streq(command_list[i].alias, cmd)) {
return command_list[i].func(data, cmd, args, error);
SPA_FOR_EACH_ELEMENT_VAR(command_list, c) {
if (spa_streq(c->name, cmd) ||
spa_streq(c->alias, cmd)) {
return c->func(data, cmd, args, error);
}
}
*error = spa_aprintf("Command \"%s\" does not exist. Type 'help' for usage.", cmd);

View file

@ -358,16 +358,13 @@ pwtest_spa_plugin_new(void)
void
pwtest_spa_plugin_destroy(struct pwtest_spa_plugin *plugin)
{
void **dll;
struct spa_handle **hnd;
SPA_FOR_EACH_ELEMENT(plugin->handles, hnd) {
SPA_FOR_EACH_ELEMENT_VAR(plugin->handles, hnd) {
if (*hnd) {
spa_handle_clear(*hnd);
free(*hnd);
}
}
SPA_FOR_EACH_ELEMENT(plugin->dlls, dll) {
SPA_FOR_EACH_ELEMENT_VAR(plugin->dlls, dll) {
if (*dll)
dlclose(*dll);
}

View file

@ -184,9 +184,8 @@ PWTEST(utils_macros)
#define check_traversal(array_) \
{ \
__typeof__(array_[0]) *it; \
int count = 0; \
SPA_FOR_EACH_ELEMENT(array_, it) \
SPA_FOR_EACH_ELEMENT_VAR(array_, it) \
*it = count++; \
for (size_t i = 0; i < SPA_N_ELEMENTS(array_); i++) \
pwtest_int_eq(array_[i], i); \

View file

@ -172,9 +172,7 @@ static void test__pw_split_walk(void)
},
};
const struct test_case *tc;
SPA_FOR_EACH_ELEMENT(test_cases, tc) {
SPA_FOR_EACH_ELEMENT_VAR(test_cases, tc) {
const char *str = tc->input, *s;
const char *state = NULL;
size_t j = 0, len;