From 349d13961d50281a65aed432a22a8c4d970e6ce8 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Wed, 28 May 2025 12:39:10 +0200 Subject: [PATCH] include: list.h: add duplicate checking add/append functions Simplify cases where callers need to check whether an entry already is in a list before adding / appending. Signed-off-by: Enrico Weigelt, metux IT consult --- include/list.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/list.h b/include/list.h index 20beb92fd..53fe84fd0 100644 --- a/include/list.h +++ b/include/list.h @@ -251,6 +251,44 @@ xorg_list_present(struct xorg_list *entry, struct xorg_list *head) return 0; } +/** + * @brief prepend a new element to the end of the list if not existing yet + * + * Same as xorg_list_add(), but protecting against duplicate insertion. + * + * @param entry The new element to append to the list. + * @param head The existing list. + * @return zero if element already in list, otherwise non-zero + */ +static inline int +xorg_list_add_ndup(struct xorg_list *entry, struct xorg_list *head) +{ + if (xorg_list_present(entry, head)) + return 0; + + xorg_list_add(entry, head); + return 1; +} + +/** + * @brief append a new element to the end of the list if not existing yet + * + * Same as xorg_list_append(), but protecting against duplicate insertion. + * + * @param entry The new element to append to the list. + * @param head The existing list. + * @return zero if element already in list, otherwise non-zero + */ +static inline int +xorg_list_append_ndup(struct xorg_list *entry, struct xorg_list *head) +{ + if (xorg_list_present(entry, head)) + return 0; + + xorg_list_append(entry, head); + return 1; +} + /** * Returns a pointer to the container of this list element. *