From 0e2be09d6c4c78414104dd876dfd503203e90fc3 Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Sun, 17 Dec 2017 17:51:11 +0100 Subject: [PATCH] CLists, SLists: Extended interface for merging of link lists. --- ucw/clists.h | 19 +++++++++++++++++++ ucw/mainloop.c | 4 ++-- ucw/slists.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/ucw/clists.h b/ucw/clists.h index d327479f..809c480d 100644 --- a/ucw/clists.h +++ b/ucw/clists.h @@ -2,6 +2,7 @@ * UCW Library -- Circular Linked Lists * * (c) 2003--2010 Martin Mares + * (c) 2017 Pavel Charvat * * 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. diff --git a/ucw/mainloop.c b/ucw/mainloop.c index 920d0f0b..dcc0f749 100644 --- a/ucw/mainloop.c +++ b/ucw/mainloop.c @@ -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); diff --git a/ucw/slists.h b/ucw/slists.h index 1325c123..a9eb996e 100644 --- a/ucw/slists.h +++ b/ucw/slists.h @@ -2,6 +2,7 @@ * UCW Library -- Single-Linked Lists * * (c) 2005 Martin Mares + * (c) 2017 Pavel Charvat * * 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. **/ -- 2.39.5