summaryrefslogtreecommitdiff
path: root/include/alist.h
diff options
context:
space:
mode:
authorSimon Glass <[email protected]>2024-10-19 09:21:44 -0600
committerSimon Glass <[email protected]>2024-11-02 11:13:59 -0600
commit2ce146a3de7beeaa89ef4f8677fe71a38546156b (patch)
tree88ca8fa5cfeb5a4509fc7154526af3bc1e0ea93f /include/alist.h
parent6668d860f78035969db301f2c43266094d455191 (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 'include/alist.h')
-rw-r--r--include/alist.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/include/alist.h b/include/alist.h
index 2c78ede201e..97523af37a6 100644
--- a/include/alist.h
+++ b/include/alist.h
@@ -72,6 +72,21 @@ static inline bool alist_has(struct alist *lst, uint index)
}
/**
+ * alist_calc_index() - Calculate the index of an item in the list
+ *
+ * The returned element number will be -1 if the list is empty or the pointer
+ * pointers to before the list starts.
+ *
+ * If the pointer points to after the last item, the calculated element-number
+ * will be returned, even though it is greater than lst->count
+ *
+ * @lst: alist to check
+ * @ptr: pointer to check
+ * Return: element number of the pointer
+ */
+int alist_calc_index(const struct alist *lst, const void *ptr);
+
+/**
* alist_err() - Check if the alist is still valid
*
* @lst: List to check
@@ -190,6 +205,25 @@ bool alist_expand_by(struct alist *lst, uint inc_by);
#define alist_add(_lst, _obj) \
((typeof(_obj) *)alist_add_ptr(_lst, &(_obj)))
+/** get next entry as a constant */
+#define alist_next(_lst, _objp) \
+ ((const typeof(_objp))alist_next_ptrd(_lst, _objp))
+
+/** get next entry, which can be written to */
+#define alist_nextw(_lst, _objp) \
+ ((typeof(_objp))alist_next_ptrd(_lst, _objp))
+
+/**
+ * alist_next_ptrd() - Get a pointer to the next list element
+ *
+ * This returns NULL if the requested element is beyond lst->count
+ *
+ * @lst: List to check
+ * @ptr: Pointer to current element (must be valid)
+ * Return: Pointer to next element, or NULL if @ptr is the last
+ */
+const void *alist_next_ptrd(const struct alist *lst, const void *ptr);
+
/**
* alist_init() - Set up a new object list
*