From 9b6e504c19175bf4d1ebfb151ad97ee02e6de2f1 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 30 Sep 2022 17:23:05 +0200 Subject: [PATCH] clean up some more array iterations --- spa/plugins/audioconvert/channelmix-ops.h | 7 ++-- spa/plugins/bluez5/quirks.c | 7 ++-- spa/plugins/support/cpu.c | 13 +++---- spa/plugins/v4l2/v4l2-utils.c | 25 ++++++------ src/examples/sdl.h | 19 ++++----- src/modules/module-avb/acmp.c | 9 ++--- src/modules/module-avb/aecp-aem.c | 9 ++--- src/modules/module-avb/aecp.c | 9 ++--- .../module-client-node/v0/client-node.c | 9 ++--- src/modules/module-protocol-pulse/format.c | 39 ++++++++----------- src/modules/module-protocol-pulse/manager.c | 9 ++--- src/modules/module-protocol-pulse/quirks.c | 7 ++-- src/tools/pw-cat.c | 31 +++++++-------- src/tools/pw-dump.c | 9 ++--- 14 files changed, 88 insertions(+), 114 deletions(-) diff --git a/spa/plugins/audioconvert/channelmix-ops.h b/spa/plugins/audioconvert/channelmix-ops.h index 911fc0f0c..5bc42551c 100644 --- a/spa/plugins/audioconvert/channelmix-ops.h +++ b/spa/plugins/audioconvert/channelmix-ops.h @@ -109,10 +109,9 @@ static const struct channelmix_upmix_info { static inline uint32_t channelmix_upmix_from_label(const char *label) { - uint32_t i; - for (i = 0; i < SPA_N_ELEMENTS(channelmix_upmix_info); i++) { - if (spa_streq(channelmix_upmix_info[i].label, label)) - return channelmix_upmix_info[i].upmix; + SPA_FOR_EACH_ELEMENT_VAR(channelmix_upmix_info, i) { + if (spa_streq(i->label, label)) + return i->upmix; } return CHANNELMIX_UPMIX_NONE; } diff --git a/spa/plugins/bluez5/quirks.c b/spa/plugins/bluez5/quirks.c index ba47414c8..8a7f92643 100644 --- a/spa/plugins/bluez5/quirks.c +++ b/spa/plugins/bluez5/quirks.c @@ -88,10 +88,9 @@ static enum spa_bt_feature parse_feature(const char *str) { "faststream", SPA_BT_FEATURE_FASTSTREAM }, { "a2dp-duplex", SPA_BT_FEATURE_A2DP_DUPLEX }, }; - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(feature_keys); ++i) { - if (spa_streq(str, feature_keys[i].key)) - return feature_keys[i].value; + SPA_FOR_EACH_ELEMENT_VAR(feature_keys, f) { + if (spa_streq(str, f->key)) + return f->value; } return 0; } diff --git a/spa/plugins/support/cpu.c b/spa/plugins/support/cpu.c index 67440af16..3e6bb85dd 100644 --- a/spa/plugins/support/cpu.c +++ b/spa/plugins/support/cpu.c @@ -171,19 +171,18 @@ impl_cpu_get_vm_type(void *object) /* https://wiki.freebsd.org/bhyve */ { "BHYVE", SPA_CPU_VM_BHYVE }, }; - uint32_t i, j; - for (i = 0; i < SPA_N_ELEMENTS(dmi_vendors); i++) { + SPA_FOR_EACH_ELEMENT_VAR(dmi_vendors, dv) { char buffer[256], *s; - if ((s = read_file(dmi_vendors[i], buffer, sizeof(buffer))) == NULL) + if ((s = read_file(*dv, buffer, sizeof(buffer))) == NULL) continue; - for (j = 0; j < SPA_N_ELEMENTS(dmi_vendor_table); j++) { - if (spa_strstartswith(s, dmi_vendor_table[j].vendor)) { + SPA_FOR_EACH_ELEMENT_VAR(dmi_vendor_table, t) { + if (spa_strstartswith(s, t->vendor)) { spa_log_debug(impl->log, "Virtualization %s found in DMI (%s)", - s, dmi_vendors[i]); - impl->vm_type = dmi_vendor_table[j].id; + s, *dv); + impl->vm_type = t->id; goto done; } } diff --git a/spa/plugins/v4l2/v4l2-utils.c b/spa/plugins/v4l2/v4l2-utils.c index 14e967f73..77636ea25 100644 --- a/spa/plugins/v4l2/v4l2-utils.c +++ b/spa/plugins/v4l2/v4l2-utils.c @@ -351,11 +351,9 @@ static const struct format_info format_info[] = { static const struct format_info *fourcc_to_format_info(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; } @@ -363,11 +361,9 @@ static const struct format_info *fourcc_to_format_info(uint32_t fourcc) #if 0 static const struct format_info *video_format_to_format_info(uint32_t format) { - int i; - - for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) { - if (format_info[i].format == format) - return &format_info[i]; + SPA_FOR_EACH_ELEMENT_VAR(format_info, i) { + if (i->format == format) + return i; } return NULL; } @@ -381,10 +377,11 @@ static const struct format_info *find_format_info_by_media_type(uint32_t type, size_t i; for (i = startidx; 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]; + const struct format_info *fi = &format_info[i]; + if ((fi->media_type == type) && + (fi->media_subtype == subtype) && + (format == 0 || fi->format == format)) + return fi; } return NULL; } diff --git a/src/examples/sdl.h b/src/examples/sdl.h index cb7b87a6f..74ea74ab6 100644 --- a/src/examples/sdl.h +++ b/src/examples/sdl.h @@ -130,25 +130,22 @@ static struct { static inline uint32_t sdl_format_to_id(Uint32 format) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(sdl_video_formats); i++) { - if (sdl_video_formats[i].format == format) - return sdl_video_formats[i].id; + SPA_FOR_EACH_ELEMENT_VAR(sdl_video_formats, f) { + if (f->format == format) + return f->id; } return SPA_VIDEO_FORMAT_UNKNOWN; } static inline Uint32 id_to_sdl_format(uint32_t id) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(sdl_video_formats); i++) { - if (sdl_video_formats[i].id == id) - return sdl_video_formats[i].format; + SPA_FOR_EACH_ELEMENT_VAR(sdl_video_formats, f) { + if (f->id == id) + return f->format; } return SDL_PIXELFORMAT_UNKNOWN; } - static inline struct spa_pod *sdl_build_formats(SDL_RendererInfo *info, struct spa_pod_builder *b) { uint32_t i, c; @@ -178,8 +175,8 @@ static inline struct spa_pod *sdl_build_formats(SDL_RendererInfo *info, struct s spa_pod_builder_id(b, id); } /* then all the other ones SDL can convert from/to */ - for (i = 0; i < SPA_N_ELEMENTS(sdl_video_formats); i++) { - uint32_t id = sdl_video_formats[i].id; + SPA_FOR_EACH_ELEMENT_VAR(sdl_video_formats, f) { + uint32_t id = f->id; if (id != SPA_VIDEO_FORMAT_UNKNOWN) spa_pod_builder_id(b, id); } diff --git a/src/modules/module-avb/acmp.c b/src/modules/module-avb/acmp.c index 18ea1ba92..a7a409a27 100644 --- a/src/modules/module-avb/acmp.c +++ b/src/modules/module-avb/acmp.c @@ -345,11 +345,10 @@ static const struct msg_info msg_info[] = { static inline const struct msg_info *find_msg_info(uint16_t type, const char *name) { - uint32_t i; - for (i = 0; i < SPA_N_ELEMENTS(msg_info); i++) { - if ((name == NULL && type == msg_info[i].type) || - (name != NULL && spa_streq(name, msg_info[i].name))) - return &msg_info[i]; + SPA_FOR_EACH_ELEMENT_VAR(msg_info, i) { + if ((name == NULL && type == i->type) || + (name != NULL && spa_streq(name, i->name))) + return i; } return NULL; } diff --git a/src/modules/module-avb/aecp-aem.c b/src/modules/module-avb/aecp-aem.c index 22a2ac2dd..d1913301b 100644 --- a/src/modules/module-avb/aecp-aem.c +++ b/src/modules/module-avb/aecp-aem.c @@ -250,11 +250,10 @@ static const struct cmd_info cmd_info[] = { static inline const struct cmd_info *find_cmd_info(uint16_t type, const char *name) { - uint32_t i; - for (i = 0; i < SPA_N_ELEMENTS(cmd_info); i++) { - if ((name == NULL && type == cmd_info[i].type) || - (name != NULL && spa_streq(name, cmd_info[i].name))) - return &cmd_info[i]; + SPA_FOR_EACH_ELEMENT_VAR(cmd_info, i) { + if ((name == NULL && type == i->type) || + (name != NULL && spa_streq(name, i->name))) + return i; } return NULL; } diff --git a/src/modules/module-avb/aecp.c b/src/modules/module-avb/aecp.c index 3c25b0ea3..d581f818b 100644 --- a/src/modules/module-avb/aecp.c +++ b/src/modules/module-avb/aecp.c @@ -67,11 +67,10 @@ static const struct msg_info msg_info[] = { static inline const struct msg_info *find_msg_info(uint16_t type, const char *name) { - uint32_t i; - for (i = 0; i < SPA_N_ELEMENTS(msg_info); i++) { - if ((name == NULL && type == msg_info[i].type) || - (name != NULL && spa_streq(name, msg_info[i].name))) - return &msg_info[i]; + SPA_FOR_EACH_ELEMENT_VAR(msg_info, i) { + if ((name == NULL && type == i->type) || + (name != NULL && spa_streq(name, i->name))) + return i; } return NULL; } diff --git a/src/modules/module-client-node/v0/client-node.c b/src/modules/module-client-node/v0/client-node.c index aeb5931c6..747a4ad4a 100644 --- a/src/modules/module-client-node/v0/client-node.c +++ b/src/modules/module-client-node/v0/client-node.c @@ -1333,13 +1333,12 @@ static void convert_properties(struct pw_properties *properties) { "pipewire.target.node", PW_KEY_NODE_TARGET, } }; - uint32_t i; const char *str; - for(i = 0; i < SPA_N_ELEMENTS(props); i++) { - if ((str = pw_properties_get(properties, props[i].from)) != NULL) { - pw_properties_set(properties, props[i].to, str); - pw_properties_set(properties, props[i].from, NULL); + SPA_FOR_EACH_ELEMENT_VAR(props, p) { + if ((str = pw_properties_get(properties, p->from)) != NULL) { + pw_properties_set(properties, p->to, str); + pw_properties_set(properties, p->from, NULL); } } } diff --git a/src/modules/module-protocol-pulse/format.c b/src/modules/module-protocol-pulse/format.c index fa5a4d4d2..2de01054c 100644 --- a/src/modules/module-protocol-pulse/format.c +++ b/src/modules/module-protocol-pulse/format.c @@ -159,32 +159,28 @@ uint32_t format_name2id(const char *name) uint32_t format_paname2id(const char *name, size_t size) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) { - if (audio_formats[i].name != NULL && - strncmp(name, audio_formats[i].name, size) == 0) - return audio_formats[i].id; + SPA_FOR_EACH_ELEMENT_VAR(audio_formats, f) { + if (f->name != NULL && + strncmp(name, f->name, size) == 0) + return f->id; } return SPA_AUDIO_FORMAT_UNKNOWN; } enum sample_format format_id2pa(uint32_t id) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) { - if (id == audio_formats[i].id) - return audio_formats[i].pa; + SPA_FOR_EACH_ELEMENT_VAR(audio_formats, f) { + if (id == f->id) + return f->pa; } return SAMPLE_INVALID; } const char *format_id2paname(uint32_t id) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(audio_formats); i++) { - if (id == audio_formats[i].id && - audio_formats[i].name != NULL) - return audio_formats[i].name; + SPA_FOR_EACH_ELEMENT_VAR(audio_formats, f) { + if (id == f->id && f->name != NULL) + return f->name; } return "invalid"; } @@ -266,21 +262,18 @@ enum channel_position channel_id2pa(uint32_t id, uint32_t *aux) const char *channel_id2paname(uint32_t id, uint32_t *aux) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(audio_channels); i++) { - if (id == audio_channels[i].channel && - audio_channels[i].name != NULL) - return audio_channels[i].name; + SPA_FOR_EACH_ELEMENT_VAR(audio_channels, i) { + if (id == i->channel && i->name != NULL) + return i->name; } return audio_channels[CHANNEL_POSITION_AUX0 + ((*aux)++ & 31)].name; } uint32_t channel_paname2id(const char *name, size_t size) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(audio_channels); i++) { - if (strncmp(name, audio_channels[i].name, size) == 0) - return audio_channels[i].channel; + SPA_FOR_EACH_ELEMENT_VAR(audio_channels, i) { + if (strncmp(name, i->name, size) == 0) + return i->channel; } return SPA_AUDIO_CHANNEL_UNKNOWN; } diff --git a/src/modules/module-protocol-pulse/manager.c b/src/modules/module-protocol-pulse/manager.c index 1b08740d4..86048a0bb 100644 --- a/src/modules/module-protocol-pulse/manager.c +++ b/src/modules/module-protocol-pulse/manager.c @@ -553,11 +553,10 @@ static const struct object_info *objects[] = static const struct object_info *find_info(const char *type, uint32_t version) { - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(objects); i++) { - if (spa_streq(objects[i]->type, type) && - objects[i]->version <= version) - return objects[i]; + SPA_FOR_EACH_ELEMENT_VAR(objects, i) { + if (spa_streq((*i)->type, type) && + (*i)->version <= version) + return *i; } return NULL; } diff --git a/src/modules/module-protocol-pulse/quirks.c b/src/modules/module-protocol-pulse/quirks.c index e00eafe91..eb2243839 100644 --- a/src/modules/module-protocol-pulse/quirks.c +++ b/src/modules/module-protocol-pulse/quirks.c @@ -38,10 +38,9 @@ static uint64_t parse_quirks(const char *str) { "force-s16-info", QUIRK_FORCE_S16_FORMAT }, { "remove-capture-dont-move", QUIRK_REMOVE_CAPTURE_DONT_MOVE }, }; - size_t i; - for (i = 0; i < SPA_N_ELEMENTS(quirk_keys); ++i) { - if (spa_streq(str, quirk_keys[i].key)) - return quirk_keys[i].value; + SPA_FOR_EACH_ELEMENT_VAR(quirk_keys, i) { + if (spa_streq(str, i->key)) + return i->value; } return 0; } diff --git a/src/tools/pw-cat.c b/src/tools/pw-cat.c index ea502d647..ab43bd12e 100644 --- a/src/tools/pw-cat.c +++ b/src/tools/pw-cat.c @@ -174,20 +174,18 @@ static const struct format_info { static const struct format_info *format_info_by_name(const char *str) { - uint32_t i; - for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) - if (spa_streq(str, format_info[i].name)) - return &format_info[i]; + SPA_FOR_EACH_ELEMENT_VAR(format_info, i) + if (spa_streq(str, i->name)) + return i; return NULL; } static const struct format_info *format_info_by_sf_format(int format) { - uint32_t i; int sub_type = (format & SF_FORMAT_SUBMASK); - for (i = 0; i < SPA_N_ELEMENTS(format_info); i++) - if (format_info[i].sf_format == sub_type) - return &format_info[i]; + SPA_FOR_EACH_ELEMENT_VAR(format_info, i) + if (i->sf_format == sub_type) + return i; return NULL; } @@ -431,10 +429,10 @@ static int parse_channelmap(const char *channel_map, struct channelmap *map) int i, nch; char **ch; - for (i = 0; i < (int) SPA_N_ELEMENTS(maps); i++) { - if (spa_streq(maps[i].name, channel_map)) { - map->n_channels = maps[i].channels; - spa_memcpy(map->channels, &maps[i].values, + SPA_FOR_EACH_ELEMENT_VAR(maps, m) { + if (spa_streq(m->name, channel_map)) { + map->n_channels = m->channels; + spa_memcpy(map->channels, &m->values, map->n_channels * sizeof(unsigned int)); return 0; } @@ -1589,17 +1587,16 @@ int main(int argc, char *argv[]) case TYPE_DSD: { struct spa_audio_info_dsd info; - size_t i; spa_zero(info); info.channels = data.dsf.info.channels; info.rate = data.dsf.info.rate / 8; - for (i = 0; i < SPA_N_ELEMENTS(dsd_layouts); i++) { - if (dsd_layouts[i].type != data.dsf.info.channel_type) + SPA_FOR_EACH_ELEMENT_VAR(dsd_layouts, i) { + if (i->type != data.dsf.info.channel_type) continue; - info.channels = dsd_layouts[i].info.n_channels; - memcpy(info.position, dsd_layouts[i].info.position, + info.channels = i->info.n_channels; + memcpy(info.position, i->info.position, info.channels * sizeof(uint32_t)); } params[0] = spa_format_audio_dsd_build(&b, SPA_PARAM_EnumFormat, &info); diff --git a/src/tools/pw-dump.c b/src/tools/pw-dump.c index 8b8cc76f1..127704d8d 100644 --- a/src/tools/pw-dump.c +++ b/src/tools/pw-dump.c @@ -1241,11 +1241,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; }