From feeb70ef8928a92c392ad3ef6d032f3df8b125cb Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 27 Dec 2017 20:24:40 +0100 Subject: shared: split helper functions out of c_list_sort() Just to make it clearer what happens. The compiler can (and possibly will) inline these static functions just fine. --- shared/nm-utils/c-list-util.c | 62 ++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/shared/nm-utils/c-list-util.c b/shared/nm-utils/c-list-util.c index 070323c69d..509f4e12ab 100644 --- a/shared/nm-utils/c-list-util.c +++ b/shared/nm-utils/c-list-util.c @@ -58,39 +58,35 @@ c_list_relink (CList *lst) /*****************************************************************************/ static CList * -_c_list_sort (CList *ls, - CListSortCmp cmp, - const void *user_data) +_c_list_srt_split (CList *ls) { - CList *ls1, *ls2; - CList head; + CList *ls2; - if (!ls->next) - return ls; - - /* split list in two halfs @ls1 and @ls2. */ - ls1 = ls; ls2 = ls; ls = ls->next; - while (ls) { + if (!ls) + return NULL; + do { ls = ls->next; if (!ls) break; ls = ls->next; ls2 = ls2->next; - } - ls = ls2; - ls2 = ls->next; - ls->next = NULL; - - /* recurse */ - ls1 = _c_list_sort (ls1, cmp, user_data); - if (!ls2) - return ls1; + } while (ls); + ls = ls2->next; + ls2->next = NULL; + return ls; +} - ls2 = _c_list_sort (ls2, cmp, user_data); +static CList * +_c_list_srt_merge (CList *ls1, + CList *ls2, + CListSortCmp cmp, + const void *user_data) +{ + CList *ls; + CList head; - /* merge */ ls = &head; for (;;) { /* while invoking the @cmp function, the list @@ -115,6 +111,28 @@ _c_list_sort (CList *ls, return head.next; } +static CList * +_c_list_sort (CList *ls, + CListSortCmp cmp, + const void *user_data) +{ + CList *ls1, *ls2; + + if (!ls->next) + return ls; + ls1 = ls; + + ls2 = _c_list_srt_split (ls1); + + ls1 = _c_list_sort (ls1, cmp, user_data); + if (!ls2) + return ls1; + + ls2 = _c_list_sort (ls2, cmp, user_data); + + return _c_list_srt_merge (ls1, ls2, cmp, user_data); +} + /** * c_list_sort_headless: * @lst: the list. -- cgit v1.2.1