Module Name:    src
Committed By:   rillig
Date:           Sat Aug 22 22:41:42 UTC 2020

Modified Files:
        src/usr.bin/make: lst.c lst.h make.c parse.c suff.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/make/lst.c
cvs rdiff -u -r1.36 -r1.37 src/usr.bin/make/lst.h
cvs rdiff -u -r1.119 -r1.120 src/usr.bin/make/make.c
cvs rdiff -u -r1.260 -r1.261 src/usr.bin/make/parse.c
cvs rdiff -u -r1.110 -r1.111 src/usr.bin/make/suff.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/lst.c
diff -u src/usr.bin/make/lst.c:1.33 src/usr.bin/make/lst.c:1.34
--- src/usr.bin/make/lst.c:1.33	Sat Aug 22 22:00:50 2020
+++ src/usr.bin/make/lst.c	Sat Aug 22 22:41:42 2020
@@ -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 @@ Lst_ForEachFrom(Lst list, LstNode node,
     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 */
+    assert(LstIsValid(list1));
+    assert(LstIsValid(list2));
 
-    if (!LstIsValid(list1) || !LstIsValid(list2)) {
-	return FAILURE;
-    }
-
-    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;
-	}
-	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;
+    if (list2->first != NULL) {
+	list2->first->prev = list1->last;
+	if (list1->last != NULL) {
+	    list1->last->next = list2->first;
+	} else {
+	    list1->first = list2->first;
 	}
-
-	/*
-	 * 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. */

Index: src/usr.bin/make/lst.h
diff -u src/usr.bin/make/lst.h:1.36 src/usr.bin/make/lst.h:1.37
--- src/usr.bin/make/lst.h:1.36	Sat Aug 22 22:00:50 2020
+++ src/usr.bin/make/lst.h	Sat Aug 22 22:41:42 2020
@@ -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	struct ListNode	*LstNode;
 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_AppendS(Lst, void *);
 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

Index: src/usr.bin/make/make.c
diff -u src/usr.bin/make/make.c:1.119 src/usr.bin/make/make.c:1.120
--- src/usr.bin/make/make.c:1.119	Sat Aug 22 20:03:41 2020
+++ src/usr.bin/make/make.c	Sat Aug 22 22:41:42 2020
@@ -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 @@ Make_ExpandUse(Lst targs)
 	    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 @@ Make_ProcessWait(Lst targs)
 	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);

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.260 src/usr.bin/make/parse.c:1.261
--- src/usr.bin/make/parse.c:1.260	Sat Aug 22 21:42:38 2020
+++ src/usr.bin/make/parse.c	Sat Aug 22 22:41:42 2020
@@ -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 @@ Parse_MainName(void)
 	/*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);

Index: src/usr.bin/make/suff.c
diff -u src/usr.bin/make/suff.c:1.110 src/usr.bin/make/suff.c:1.111
--- src/usr.bin/make/suff.c:1.110	Sat Aug 22 22:00:50 2020
+++ src/usr.bin/make/suff.c	Sat Aug 22 22:41:42 2020
@@ -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 @@ sfnd_return:
     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);
 }
 
 

Reply via email to