Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): make moving and copying lists simpler



details:   https://anonhg.NetBSD.org/src/rev/3451e18c95ff
branches:  trunk
changeset: 937648:3451e18c95ff
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Aug 22 22:41:42 2020 +0000

description:
make(1): make moving and copying lists simpler

Instead of the two-in-one Lst_Concat, having two separate functions is
easier to understand.  There is no need for a long API comment anymore
since the new functions have a single purpose that is accurately
described by their name.

The long comment inside Lst_Concat has been removed since it only
repeated the exact code, only in more words.

The comments in make.c about appending the cohorts had been wrong.  They
were not appended but prepended.  Once more, the function name expresses
everything that the comment said, making the comment redundant.  There
is no need to test whether the cohorts list is empty, doing nothing is
implied by the word All in Lst_PrependAllS.

diffstat:

 usr.bin/make/lst.c   |  97 +++++++++------------------------------------------
 usr.bin/make/lst.h   |   8 +---
 usr.bin/make/make.c  |  24 +++---------
 usr.bin/make/parse.c |   8 ++--
 usr.bin/make/suff.c  |  10 ++--
 5 files changed, 35 insertions(+), 112 deletions(-)

diffs (286 lines):

diff -r ee8ab95e40af -r 3451e18c95ff usr.bin/make/lst.c
--- a/usr.bin/make/lst.c        Sat Aug 22 22:00:50 2020 +0000
+++ b/usr.bin/make/lst.c        Sat Aug 22 22:41:42 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lst.c,v 1.33 2020/08/22 22:00:50 rillig Exp $ */
+/* $NetBSD: lst.c,v 1.34 2020/08/22 22:41:42 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -37,11 +37,11 @@
 #include "make.h"
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: lst.c,v 1.33 2020/08/22 22:00:50 rillig Exp $";
+static char rcsid[] = "$NetBSD: lst.c,v 1.34 2020/08/22 22:41:42 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: lst.c,v 1.33 2020/08/22 22:00:50 rillig Exp $");
+__RCSID("$NetBSD: lst.c,v 1.34 2020/08/22 22:41:42 rillig Exp $");
 #endif /* not lint */
 #endif
 
@@ -597,87 +597,24 @@
     return result;
 }
 
-/* Concatenate two lists. New nodes are created to hold the data elements,
- * if specified, but the data themselves are not copied. If the data
- * should be duplicated to avoid confusion with another list, the Lst_Duplicate
- * function should be called first. If LST_CONCLINK is specified, the second
- * list is destroyed since its pointers have been corrupted and the list is no
- * longer usable.
- *
- * Input:
- *     list1           The list to which list2 is to be appended
- *     list2           The list to append to list1
- *     flags           LST_CONCNEW if the list nodes should be duplicated
- *                     LST_CONCLINK if the list nodes should just be relinked
- */
-ReturnStatus
-Lst_Concat(Lst list1, Lst list2, int flags)
+/* Move all nodes from list2 to the end of list1.
+ * List2 is destroyed and freed. */
+void
+Lst_MoveAllS(Lst list1, Lst list2)
 {
-    LstNode node;      /* original node */
-    LstNode newNode;
-    LstNode last;      /* the last element in the list.
-                        * Keeps bookkeeping until the end */
-
-    if (!LstIsValid(list1) || !LstIsValid(list2)) {
-       return FAILURE;
-    }
+    assert(LstIsValid(list1));
+    assert(LstIsValid(list2));
 
-    if (flags == LST_CONCLINK) {
-       if (list2->first != NULL) {
-           /*
-            * So long as the second list isn't empty, we just link the
-            * first element of the second list to the last element of the
-            * first list. If the first list isn't empty, we then link the
-            * last element of the list to the first element of the second list
-            * The last element of the second list, if it exists, then becomes
-            * the last element of the first list.
-            */
-           list2->first->prev = list1->last;
-           if (list1->last != NULL) {
-               list1->last->next = list2->first;
-           } else {
-               list1->first = list2->first;
-           }
-           list1->last = list2->last;
+    if (list2->first != NULL) {
+       list2->first->prev = list1->last;
+       if (list1->last != NULL) {
+           list1->last->next = list2->first;
+       } else {
+           list1->first = list2->first;
        }
-       free(list2);
-    } else if (list2->first != NULL) {
-       /*
-        * We set the 'next' of the last element of list 2 to be nil to make
-        * the loop less difficult. The loop simply goes through the entire
-        * second list creating new LstNodes and filling in the 'next', and
-        * 'prev' to fit into list1 and its datum field from the
-        * datum field of the corresponding element in list2. The 'last' node
-        * follows the last of the new nodes along until the entire list2 has
-        * been appended. Only then does the bookkeeping catch up with the
-        * changes. During the first iteration of the loop, if 'last' is nil,
-        * the first list must have been empty so the newly-created node is
-        * made the first node of the list.
-        */
-       list2->last->next = NULL;
-       for (last = list1->last, node = list2->first;
-            node != NULL;
-            node = node->next)
-       {
-           newNode = LstNodeNew(node->datum);
-           if (last != NULL) {
-               last->next = newNode;
-           } else {
-               list1->first = newNode;
-           }
-           newNode->prev = last;
-           last = newNode;
-       }
-
-       /*
-        * Finish bookkeeping. The last new element becomes the last element
-        * of list one.
-        */
-       list1->last = last;
-       last->next = NULL;
+       list1->last = list2->last;
     }
-
-    return SUCCESS;
+    free(list2);
 }
 
 /* Copy the element data from src to the start of dst. */
diff -r ee8ab95e40af -r 3451e18c95ff usr.bin/make/lst.h
--- a/usr.bin/make/lst.h        Sat Aug 22 22:00:50 2020 +0000
+++ b/usr.bin/make/lst.h        Sat Aug 22 22:41:42 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lst.h,v 1.36 2020/08/22 22:00:50 rillig Exp $  */
+/*     $NetBSD: lst.h,v 1.37 2020/08/22 22:41:42 rillig Exp $  */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -95,9 +95,6 @@
 typedef void           *DuplicateProc(void *);
 typedef void           FreeProc(void *);
 
-#define LST_CONCNEW    0   /* create new LstNode's when using Lst_Concat */
-#define LST_CONCLINK   1   /* relink LstNode's when using Lst_Concat */
-
 /*
  * Creation/destruction functions
  */
@@ -124,10 +121,9 @@
 void           Lst_RemoveS(Lst, LstNode);
 /* Replace a node with a new value */
 void           Lst_ReplaceS(LstNode, void *);
-/* Concatenate two lists */
-ReturnStatus   Lst_Concat(Lst, Lst, int);
 void           Lst_PrependAllS(Lst, Lst);
 void           Lst_AppendAllS(Lst, Lst);
+void           Lst_MoveAllS(Lst, Lst);
 
 /*
  * Node-specific functions
diff -r ee8ab95e40af -r 3451e18c95ff usr.bin/make/make.c
--- a/usr.bin/make/make.c       Sat Aug 22 22:00:50 2020 +0000
+++ b/usr.bin/make/make.c       Sat Aug 22 22:41:42 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.c,v 1.119 2020/08/22 20:03:41 rillig Exp $        */
+/*     $NetBSD: make.c,v 1.120 2020/08/22 22:41:42 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: make.c,v 1.119 2020/08/22 20:03:41 rillig Exp $";
+static char rcsid[] = "$NetBSD: make.c,v 1.120 2020/08/22 22:41:42 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)make.c     8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: make.c,v 1.119 2020/08/22 20:03:41 rillig Exp $");
+__RCSID("$NetBSD: make.c,v 1.120 2020/08/22 22:41:42 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1279,13 +1279,8 @@
            fprintf(debug_file, "Make_ExpandUse: examine %s%s\n",
                    gn->name, gn->cohort_num);
 
-       if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
-           /* Append all the 'cohorts' to the list of things to examine */
-           Lst new;
-           new = Lst_Duplicate(gn->cohorts, NULL);
-           Lst_Concat(new, examine, LST_CONCLINK);
-           examine = new;
-       }
+       if (gn->type & OP_DOUBLEDEP)
+           Lst_PrependAllS(examine, gn->cohorts);
 
        /*
         * Apply any .USE rules before looking for implicit dependencies
@@ -1414,13 +1409,8 @@
        if (DEBUG(MAKE))
            fprintf(debug_file, "Make_ProcessWait: examine %s\n", pgn->name);
 
-       if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts)) {
-           /* Append all the 'cohorts' to the list of things to examine */
-           Lst new;
-           new = Lst_Duplicate(pgn->cohorts, NULL);
-           Lst_Concat(new, examine, LST_CONCLINK);
-           examine = new;
-       }
+       if (pgn->type & OP_DOUBLEDEP)
+           Lst_PrependAllS(examine, pgn->cohorts);
 
        owln = Lst_First(pgn->children);
        Lst_OpenS(pgn->children);
diff -r ee8ab95e40af -r 3451e18c95ff usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Sat Aug 22 22:00:50 2020 +0000
+++ b/usr.bin/make/parse.c      Sat Aug 22 22:41:42 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.260 2020/08/22 21:42:38 rillig Exp $       */
+/*     $NetBSD: parse.c,v 1.261 2020/08/22 22:41:42 rillig Exp $       */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: parse.c,v 1.260 2020/08/22 21:42:38 rillig Exp $";
+static char rcsid[] = "$NetBSD: parse.c,v 1.261 2020/08/22 22:41:42 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)parse.c    8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: parse.c,v 1.260 2020/08/22 21:42:38 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.261 2020/08/22 22:41:42 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -3332,7 +3332,7 @@
        /*NOTREACHED*/
     } else if (mainNode->type & OP_DOUBLEDEP) {
        Lst_AppendS(mainList, mainNode);
-       Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
+       Lst_AppendAllS(mainList, mainNode->cohorts);
     }
     else
        Lst_AppendS(mainList, mainNode);
diff -r ee8ab95e40af -r 3451e18c95ff usr.bin/make/suff.c
--- a/usr.bin/make/suff.c       Sat Aug 22 22:00:50 2020 +0000
+++ b/usr.bin/make/suff.c       Sat Aug 22 22:41:42 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: suff.c,v 1.110 2020/08/22 22:00:50 rillig Exp $        */
+/*     $NetBSD: suff.c,v 1.111 2020/08/22 22:41:42 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: suff.c,v 1.110 2020/08/22 22:00:50 rillig Exp $";
+static char rcsid[] = "$NetBSD: suff.c,v 1.111 2020/08/22 22:41:42 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)suff.c     8.4 (Berkeley) 3/21/94";
 #else
-__RCSID("$NetBSD: suff.c,v 1.110 2020/08/22 22:00:50 rillig Exp $");
+__RCSID("$NetBSD: suff.c,v 1.111 2020/08/22 22:41:42 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -2392,8 +2392,8 @@
     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
        continue;
 
-    Lst_Concat(slst, srcs, LST_CONCLINK);
-    Lst_Concat(slst, targs, LST_CONCLINK);
+    Lst_MoveAllS(slst, srcs);
+    Lst_MoveAllS(slst, targs);
 }
 
 



Home | Main Index | Thread Index | Old Index