Instead of open-coding the linked list iterations, let's define some macros to wrap them. This will come in handy when we associate the parameters with the bobject instead of the device as we will only have to adjust the macro definitions.
Signed-off-by: Ahmad Fatoum <a.fat...@barebox.org> --- commands/devinfo.c | 2 +- commands/varinfo.c | 2 +- common/complete.c | 2 +- common/globalvar.c | 16 ++++++++-------- include/device.h | 5 +++++ lib/parameter.c | 2 +- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/commands/devinfo.c b/commands/devinfo.c index 3c791e4464ac..cff4dbb92e31 100644 --- a/commands/devinfo.c +++ b/commands/devinfo.c @@ -88,7 +88,7 @@ static int do_devinfo(int argc, char *argv[]) printf("Parent: %s\n", dev_name(dev->parent)); first = true; - list_for_each_entry(param, &dev->parameters, list) { + dev_for_each_param(dev, param) { if (first) { printf("Parameters:\n"); first = false; diff --git a/commands/varinfo.c b/commands/varinfo.c index 5965c60159ca..eff7bf423cce 100644 --- a/commands/varinfo.c +++ b/commands/varinfo.c @@ -56,7 +56,7 @@ static int do_varinfo(int argc, char *argv[]) if (!dev) return -ENODEV; - list_for_each_entry(param, &dev->parameters, list) { + dev_for_each_param(dev, param) { if (prefix && !strstarts(param->name, prefix)) continue; diff --git a/common/complete.c b/common/complete.c index 5327944ca93b..a01ce9b47163 100644 --- a/common/complete.c +++ b/common/complete.c @@ -179,7 +179,7 @@ static int device_param_complete(struct device *dev, const char *devname, { struct param_d *param; - list_for_each_entry(param, &dev->parameters, list) { + dev_for_each_param(dev, param) { if (!strstarts_escaped(param->name, instr)) continue; diff --git a/common/globalvar.c b/common/globalvar.c index 1ef98f44bc67..cca1ac9b39f5 100644 --- a/common/globalvar.c +++ b/common/globalvar.c @@ -35,7 +35,7 @@ void globalvar_remove(const char *name) { struct param_d *p, *tmp; - list_for_each_entry_safe(p, tmp, &global_device.parameters, list) { + dev_for_each_param_safe(&global_device, p, tmp) { if (fnmatch(name, p->name, 0)) continue; @@ -276,7 +276,7 @@ int nvvar_remove(const char *name) if (!IS_ENABLED(CONFIG_NVVAR)) return -ENOSYS; - list_for_each_entry_safe(p, tmp, &nv_device.parameters, list) { + dev_for_each_param_safe(&nv_device, p, tmp) { if (fnmatch(name, p->name, 0)) continue; @@ -382,7 +382,7 @@ static void device_param_print(struct device *dev) { struct param_d *param; - list_for_each_entry(param, &dev->parameters, list) { + dev_for_each_param(dev, param) { const char *p = dev_get_param(dev, param->name); const char *nv = NULL; @@ -420,7 +420,7 @@ char *globalvar_get_match(const char *match, const char *separator) char *val = NULL; struct param_d *param; - list_for_each_entry(param, &global_device.parameters, list) { + dev_for_each_param(&global_device, param) { if (!strncmp(match, param->name, strlen(match))) { const char *p = dev_get_param(&global_device, param->name); if (val) { @@ -444,7 +444,7 @@ void globalvar_set_match(const char *match, const char *val) { struct param_d *param; - list_for_each_entry(param, &global_device.parameters, list) { + dev_for_each_param(&global_device, param) { if (!strncmp(match, param->name, strlen(match))) dev_set_param(&global_device, param->name, val); } @@ -723,7 +723,7 @@ int nvvar_save(void) envfs_load(env, TMPDIR, 0); unlink_recursive(TMPDIR "/nv", NULL); - list_for_each_entry(param, &nv_device.parameters, list) { + dev_for_each_param(&nv_device, param) { ret = __nv_save(TMPDIR "/nv", param->name, dev_get_param(&nv_device, param->name)); if (ret) { @@ -760,7 +760,7 @@ static int nv_global_param_complete(struct device *dev, len = strlen(instr); - list_for_each_entry(param, &dev->parameters, list) { + dev_for_each_param(dev, param) { if (strncmp(instr, param->name, len)) continue; @@ -790,7 +790,7 @@ int nv_complete(struct string_list *sl, char *instr) if (dev == &global_device || dev == &nv_device) continue; - list_for_each_entry(param, &dev->parameters, list) { + dev_for_each_param(dev, param) { str = basprintf("dev.%s.%s=", dev_name(dev), param->name); if (strncmp(instr, str, len)) free(str); diff --git a/include/device.h b/include/device.h index 8b65693d9672..1158ec59b5e8 100644 --- a/include/device.h +++ b/include/device.h @@ -162,6 +162,11 @@ extern struct list_head class_list; #define class_for_each(class) list_for_each_entry((class), &class_list, list) +#define dev_for_each_param(dev, param) \ + list_for_each_entry((param), &(dev)->parameters, list) +#define dev_for_each_param_safe(dev, param, tmp) \ + list_for_each_entry_safe((param), (tmp), &(dev)->parameters, list) + struct device_alias { struct device *dev; struct list_head list; diff --git a/lib/parameter.c b/lib/parameter.c index 584876bbc24b..c550de050db0 100644 --- a/lib/parameter.c +++ b/lib/parameter.c @@ -1026,7 +1026,7 @@ void dev_remove_parameters(struct device *dev) { struct param_d *p, *n; - list_for_each_entry_safe(p, n, &dev->parameters, list) + dev_for_each_param_safe(dev, p, n) param_remove(p); } -- 2.39.5