diff options
author | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-12-07 23:06:42 +0100 |
---|---|---|
committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2019-05-17 02:27:08 +0200 |
commit | 4440e3cdf7860a036836216ffa80a6ef82096901 (patch) | |
tree | 21c4a47c20c677c2756348664b0e3efef711c7e3 | |
parent | vrrpd: add unit when showing time values (diff) | |
download | frr-4440e3cdf7860a036836216ffa80a6ef82096901.tar.xz frr-4440e3cdf7860a036836216ffa80a6ef82096901.zip |
lib: add list_to_array
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
-rw-r--r-- | lib/linklist.c | 15 | ||||
-rw-r--r-- | lib/linklist.h | 20 |
2 files changed, 35 insertions, 0 deletions
diff --git a/lib/linklist.c b/lib/linklist.c index 40c4b2716..43bc70932 100644 --- a/lib/linklist.c +++ b/lib/linklist.c @@ -334,3 +334,18 @@ struct listnode *listnode_add_force(struct list **list, void *val) *list = list_new(); return listnode_add(*list, val); } + +void **list_to_array(struct list *list, void **arr, size_t arrlen) +{ + struct listnode *ln; + void *vp; + size_t idx = 0; + + for (ALL_LIST_ELEMENTS_RO(list, ln, vp)) { + arr[idx++] = vp; + if (idx == arrlen) + break; + } + + return arr; +} diff --git a/lib/linklist.h b/lib/linklist.h index c30d8d314..c2b289596 100644 --- a/lib/linklist.h +++ b/lib/linklist.h @@ -240,6 +240,26 @@ extern void list_sort(struct list *list, int (*cmp)(const void **, const void **)); /* + * Convert a list to an array of void pointers. + * + * Starts from the list head and ends either on the last node of the list or + * when the provided array cannot store any more elements. + * + * list + * list to convert + * + * arr + * Pre-allocated array of void * + * + * arrlen + * Number of elements in arr + * + * Returns: + * arr + */ +void **list_to_array(struct list *list, void **arr, size_t arrlen); + +/* * Delete a list and NULL its pointer. * * If non-null, list->del is called with each data element. |