[PATCH v2] list: use list_count_nodes() to count list entries

Sughosh Ganu sughosh.ganu at linaro.org
Wed Aug 28 18:54:22 CEST 2024


Use the API function list_count_nodes() to count the number of list
entries.

Signed-off-by: Sughosh Ganu <sughosh.ganu at linaro.org>
Reviewed-by: Simon Glass <sjg at chromium.org>
---
Changes since V1: Rebased on top of next

 boot/scene.c              | 8 +-------
 drivers/mtd/ubi/fastmap.c | 8 +++-----
 fs/yaffs2/yaffs_guts.c    | 5 ++---
 lib/fwu_updates/fwu_mtd.c | 5 +----
 test/boot/expo.c          | 4 +---
 5 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/boot/scene.c b/boot/scene.c
index ac976aa26b..270c9c6723 100644
--- a/boot/scene.c
+++ b/boot/scene.c
@@ -79,13 +79,7 @@ int scene_title_set(struct scene *scn, uint id)
 
 int scene_obj_count(struct scene *scn)
 {
-	struct scene_obj *obj;
-	int count = 0;
-
-	list_for_each_entry(obj, &scn->obj_head, sibling)
-		count++;
-
-	return count;
+	return list_count_nodes(&scn->obj_head);
 }
 
 void *scene_obj_find(const struct scene *scn, uint id, enum scene_obj_t type)
diff --git a/drivers/mtd/ubi/fastmap.c b/drivers/mtd/ubi/fastmap.c
index 21750e1817..9c6b15b8cb 100644
--- a/drivers/mtd/ubi/fastmap.c
+++ b/drivers/mtd/ubi/fastmap.c
@@ -581,13 +581,11 @@ static int count_fastmap_pebs(struct ubi_attach_info *ai)
 	struct ubi_ainf_peb *aeb;
 	struct ubi_ainf_volume *av;
 	struct rb_node *rb1, *rb2;
-	int n = 0;
+	int n;
 
-	list_for_each_entry(aeb, &ai->erase, u.list)
-		n++;
+	n = list_count_nodes(&ai->erase);
 
-	list_for_each_entry(aeb, &ai->free, u.list)
-		n++;
+	n += list_count_nodes(&ai->free);
 
 	 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb)
 		ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb)
diff --git a/fs/yaffs2/yaffs_guts.c b/fs/yaffs2/yaffs_guts.c
index e89d02513c..c20f2f8298 100644
--- a/fs/yaffs2/yaffs_guts.c
+++ b/fs/yaffs2/yaffs_guts.c
@@ -4452,13 +4452,12 @@ loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
 int yaffs_get_obj_link_count(struct yaffs_obj *obj)
 {
 	int count = 0;
-	struct list_head *i;
 
 	if (!obj->unlinked)
 		count++;	/* the object itself */
 
-	list_for_each(i, &obj->hard_links)
-	    count++;		/* add the hard links; */
+	/* add the hard links; */
+	count += list_count_nodes(&obj->hard_links);
 
 	return count;
 }
diff --git a/lib/fwu_updates/fwu_mtd.c b/lib/fwu_updates/fwu_mtd.c
index ccaba3f311..c14203b9dd 100644
--- a/lib/fwu_updates/fwu_mtd.c
+++ b/lib/fwu_updates/fwu_mtd.c
@@ -60,10 +60,7 @@ int fwu_mtd_get_alt_num(efi_guid_t *image_id, u8 *alt_num,
 	if (ret)
 		return -ENOENT;
 
-	nalt = 0;
-	list_for_each_entry(dfu, &dfu_list, list)
-		nalt++;
-
+	nalt = list_count_nodes(&dfu_list);
 	if (!nalt) {
 		log_warning("No entities in dfu_alt_info\n");
 		dfu_free_entities();
diff --git a/test/boot/expo.c b/test/boot/expo.c
index 4b24f504e2..9b4aa803eb 100644
--- a/test/boot/expo.c
+++ b/test/boot/expo.c
@@ -702,9 +702,7 @@ static int expo_test_build(struct unit_test_state *uts)
 	txt = scene_obj_find(scn, item->label_id, SCENEOBJT_NONE);
 	ut_asserteq_str("2 GHz", expo_get_str(exp, txt->str_id));
 
-	count = 0;
-	list_for_each_entry(item, &menu->item_head, sibling)
-		count++;
+	count = list_count_nodes(&menu->item_head);
 	ut_asserteq(3, count);
 
 	expo_destroy(exp);
-- 
2.34.1



More information about the U-Boot mailing list