]> mj.ucw.cz Git - libucw.git/commitdiff
CLists, SLists: Extended interface for merging of link lists.
authorPavel Charvat <pchar@ucw.cz>
Sun, 17 Dec 2017 16:51:11 +0000 (17:51 +0100)
committerPavel Charvat <pchar@ucw.cz>
Sun, 17 Dec 2017 16:51:11 +0000 (17:51 +0100)
ucw/clists.h
ucw/mainloop.c
ucw/slists.h

index d327479fdd8924f2807b0c94bb26d75e6ac84376..809c480dea4303820edce351ad0dfc518931a3fb 100644 (file)
@@ -2,6 +2,7 @@
  *     UCW Library -- Circular Linked Lists
  *
  *     (c) 2003--2010 Martin Mares <mj@ucw.cz>
+ *     (c) 2017 Pavel Charvat <pchar@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -189,6 +190,24 @@ static inline void clist_insert_list_after(clist *what, cnode *after)
     }
 }
 
+/**
+ * Merge two lists by inserting the list @what in front of all other nodes in a different list @l.
+ * The first list is then cleared.
+ **/
+static inline void clist_add_list_head(clist *l, clist *what)
+{
+  clist_insert_list_after(what, &l->head);
+}
+
+/**
+ * Merge two lists by inserting the list @what after all other nodes in a different list @l.
+ * The first list is then cleared.
+ **/
+static inline void clist_add_list_tail(clist *l, clist *what)
+{
+  clist_insert_list_after(what, l->head.prev);
+}
+
 /**
  * Move all items from a source list to a destination list. The source list
  * becomes empty, the original contents of the destination list are destroyed.
index 920d0f0b646c595ce2ec947ad35720054b00c719..dcc0f749836e07ce7b041e6ddda9379011c7d76b 100644 (file)
@@ -183,9 +183,9 @@ main_destroy(struct main_context *m)
   main_prepare_delete(m);
 
   // Close all files
-  clist_insert_list_after(&m->file_active_list, m->file_list.head.prev);
+  clist_add_list_tail(&m->file_list, &m->file_active_list);
 #ifdef CONFIG_UCW_EPOLL
-  clist_insert_list_after(&m->file_recalc_list, m->file_list.head.prev);
+  clist_add_list_tail(&m->file_list, &m->file_recalc_list);
 #endif
   CLIST_FOR_EACH(struct main_file *, f, m->file_list)
     close(f->fd);
index 1325c12376878974f74d682e599d396b6f792654..a9eb996e9caea0de0d27e99ab63819a87999e423 100644 (file)
@@ -2,6 +2,7 @@
  *     UCW Library -- Single-Linked Lists
  *
  *     (c) 2005 Martin Mares <mj@ucw.cz>
+ *     (c) 2017 Pavel Charvat <pchar@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -173,6 +174,51 @@ static inline void slist_remove_tail(slist *l)
   slist_remove(l, l->last);
 }
 
+/**
+ * Merge two lists by inserting the list @what in front of all other nodes in a different list @l.
+ * The first list is then cleared.
+ **/
+static inline void slist_add_list_head(slist *l, slist *what)
+{
+  if (!slist_empty(what))
+    {
+      if (!slist_empty(l))
+       what->last->next = l->head.next;
+      else
+       l->last = what->last;
+      l->head.next = what->head.next;
+      slist_init(what);
+    }
+}
+
+/**
+ * Merge two lists by inserting the list @what after all other nodes in a different list @l.
+ * The first list is then cleared.
+ **/
+static inline void slist_add_list_tail(slist *l, slist *what)
+{
+  if (!slist_empty(what))
+    {
+      if (!slist_empty(l))
+       l->last->next = what->head.next;
+      else
+       l->head.next = what->head.next;
+      l->last = what->last;
+      slist_init(what);
+    }
+}
+
+/**
+ * Move all items from a source list to a destination list. The source list
+ * becomes empty, the original contents of the destination list are destroyed.
+ **/
+static inline void slist_move(slist *to, slist *from)
+{
+  to->head.next = from->head.next;
+  to->last = from->last;
+  slist_init(from);
+}
+
 /**
  * Compute the number of nodes in @l. Beware linear time complexity.
  **/