From 0f16688138d40affba369a6e2f394b3a5d4c3bb4 Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Mon, 10 Aug 2020 23:21:59 -0300 Subject: [PATCH] lib: introduce new list_dup() API This new function will be used by the upcoming TI-LFA code. Signed-off-by: Renato Westphal --- lib/linklist.c | 17 +++++++++++++++++ lib/linklist.h | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/linklist.c b/lib/linklist.c index 2936c5b502..84dc6e1419 100644 --- a/lib/linklist.c +++ b/lib/linklist.c @@ -150,6 +150,23 @@ bool listnode_add_sort_nodup(struct list *list, void *val) return true; } +struct list *list_dup(struct list *list) +{ + struct list *dup; + struct listnode *node; + void *data; + + assert(list); + + dup = list_new(); + dup->cmp = list->cmp; + dup->del = list->del; + for (ALL_LIST_ELEMENTS_RO(list, node, data)) + listnode_add(dup, data); + + return dup; +} + void listnode_add_sort(struct list *list, void *val) { struct listnode *n; diff --git a/lib/linklist.h b/lib/linklist.h index 94a1a1604a..d8820c924d 100644 --- a/lib/linklist.h +++ b/lib/linklist.h @@ -327,6 +327,19 @@ extern void list_filter_out_nodes(struct list *list, bool (*cond)(void *data)); */ extern bool listnode_add_sort_nodup(struct list *list, void *val); + +/* + * Duplicate the specified list, creating a shallow copy of each of its + * elements. + * + * list + * list to duplicate + * + * Returns: + * the duplicated list + */ +extern struct list *list_dup(struct list *list); + /* List iteration macro. * Usage: for (ALL_LIST_ELEMENTS (...) { ... } * It is safe to delete the listnode using this macro. -- 2.39.5