diff options
| author | Simon Glass <[email protected]> | 2024-10-19 09:21:44 -0600 |
|---|---|---|
| committer | Tom Rini <[email protected]> | 2024-11-03 21:27:12 -0600 |
| commit | 1d49f78c362981435a88d887c777ccc445f5a4e7 (patch) | |
| tree | ccf9cde5ff3f79eb3d3b5104ddd361d5de809147 /lib/alist.c | |
| parent | eb6e87a7ab9f93596983b57eef08509beeedf3fb (diff) | |
alist: Add a way to get the next element
Add a new function which returns the next element after the one
provided, if it exists in the list.
Signed-off-by: Simon Glass <[email protected]>
Diffstat (limited to 'lib/alist.c')
| -rw-r--r-- | lib/alist.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/alist.c b/lib/alist.c index b7928cad520..7730fe0d473 100644 --- a/lib/alist.c +++ b/lib/alist.c @@ -106,6 +106,27 @@ const void *alist_get_ptr(const struct alist *lst, uint index) return lst->data + index * lst->obj_size; } +int alist_calc_index(const struct alist *lst, const void *ptr) +{ + uint index; + + if (!lst->count || ptr < lst->data) + return -1; + + index = (ptr - lst->data) / lst->obj_size; + + return index; +} + +const void *alist_next_ptrd(const struct alist *lst, const void *ptr) +{ + int index = alist_calc_index(lst, ptr); + + assert(index != -1); + + return alist_get_ptr(lst, index + 1); +} + void *alist_ensure_ptr(struct alist *lst, uint index) { uint minsize = index + 1; |
