]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add list_to_array
authorQuentin Young <qlyoung@cumulusnetworks.com>
Fri, 7 Dec 2018 22:06:42 +0000 (22:06 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Fri, 17 May 2019 00:27:08 +0000 (00:27 +0000)
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/linklist.c
lib/linklist.h

index 40c4b271698ce48d125f8e14abfeee20ebd8519c..43bc709325d6615e9b0b65a8ddd14fb894e28014 100644 (file)
@@ -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;
+}
index c30d8d314a3bb785d016bb1d3e529ca2f77aac68..c2b289596d86ed586ae7c8fbef26b756e97189c0 100644 (file)
@@ -239,6 +239,26 @@ extern struct list *list_dup(struct list *l);
 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.
  *