Skip to content

Commit

Permalink
drgn.helpers.linux.list: add list_count_nodes()
Browse files Browse the repository at this point in the history
I've needed this many times, but there wasn't a corresponding function
in the kernel so I could never decide what to name it. Linux kernel
commit 4d70c74659d9 ("i915: Move list_count() to list.h as
list_count_nodes() for broader use") (in v6.3-rc1) fixed that problem
for me.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Jun 30, 2023
1 parent 7cb3e99 commit c69e5b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions drgn/helpers/linux/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"hlist_empty",
"hlist_for_each",
"hlist_for_each_entry",
"list_count_nodes",
"list_empty",
"list_first_entry",
"list_first_entry_or_null",
Expand Down Expand Up @@ -57,6 +58,15 @@ def list_is_singular(head: Object) -> bool:
return next != head and next == head.prev


def list_count_nodes(head: Object) -> int:
"""
Return the number of nodes in a list.
:param head: ``struct list_head *``
"""
return sum(1 for _ in list_for_each(head))


def list_first_entry(head: Object, type: Union[str, Type], member: str) -> Object:
"""
Return the first entry in a list.
Expand Down
6 changes: 6 additions & 0 deletions tests/linux_kernel/helpers/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
hlist_empty,
hlist_for_each,
hlist_for_each_entry,
list_count_nodes,
list_empty,
list_first_entry,
list_first_entry_or_null,
Expand Down Expand Up @@ -56,6 +57,11 @@ def test_list_is_singular(self):
self.assertFalse(list_is_singular(self.full))
self.assertTrue(list_is_singular(self.singular))

def test_list_count_nodes(self):
self.assertEqual(list_count_nodes(self.empty), 0)
self.assertEqual(list_count_nodes(self.full), self.num_entries)
self.assertEqual(list_count_nodes(self.singular), 1)

def test_list_first_entry(self):
self.assertEqual(
list_first_entry(self.full, "struct drgn_test_list_entry", "node"),
Expand Down

0 comments on commit c69e5b1

Please sign in to comment.