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): condense the list library into a singl...



details:   https://anonhg.NetBSD.org/src/rev/e5361bcd20dc
branches:  trunk
changeset: 936349:e5361bcd20dc
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jul 26 07:15:26 2020 +0000

description:
make(1): condense the list library into a single file

The list library is only used in make(1). Having it spread out over 28
files made it look more complex than it really is. In fact, it's just a
versatile generic data type like in hash.c.

Having all the list functions in a single file reduces the code size,
both by omitting the many RCS Ids and by inlining commonly used code.

diffstat:

 usr.bin/make/Makefile                 |     9 +-
 usr.bin/make/lst.c                    |  1214 +++++++++++++++++++++++++++++++++
 usr.bin/make/lst.lib/Makefile         |    10 -
 usr.bin/make/lst.lib/lstAppend.c      |   121 ---
 usr.bin/make/lst.lib/lstAtEnd.c       |    79 --
 usr.bin/make/lst.lib/lstAtFront.c     |    76 --
 usr.bin/make/lst.lib/lstClose.c       |    85 --
 usr.bin/make/lst.lib/lstConcat.c      |   184 -----
 usr.bin/make/lst.lib/lstDatum.c       |    76 --
 usr.bin/make/lst.lib/lstDeQueue.c     |    86 --
 usr.bin/make/lst.lib/lstDestroy.c     |   101 --
 usr.bin/make/lst.lib/lstDupl.c        |   107 --
 usr.bin/make/lst.lib/lstEnQueue.c     |    77 --
 usr.bin/make/lst.lib/lstFind.c        |    73 -
 usr.bin/make/lst.lib/lstFindFrom.c    |    89 --
 usr.bin/make/lst.lib/lstFirst.c       |    76 --
 usr.bin/make/lst.lib/lstForEach.c     |    75 --
 usr.bin/make/lst.lib/lstForEachFrom.c |   124 ---
 usr.bin/make/lst.lib/lstInit.c        |    85 --
 usr.bin/make/lst.lib/lstInsert.c      |   121 ---
 usr.bin/make/lst.lib/lstInt.h         |   105 --
 usr.bin/make/lst.lib/lstIsAtEnd.c     |    86 --
 usr.bin/make/lst.lib/lstIsEmpty.c     |    74 --
 usr.bin/make/lst.lib/lstLast.c        |    76 --
 usr.bin/make/lst.lib/lstMember.c      |    77 --
 usr.bin/make/lst.lib/lstNext.c        |   119 ---
 usr.bin/make/lst.lib/lstOpen.c        |    86 --
 usr.bin/make/lst.lib/lstPrev.c        |    78 --
 usr.bin/make/lst.lib/lstRemove.c      |   134 ---
 usr.bin/make/lst.lib/lstReplace.c     |    77 --
 usr.bin/make/lst.lib/lstSucc.c        |    78 --
 31 files changed, 1216 insertions(+), 2642 deletions(-)

diffs (truncated from 3990 to 300 lines):

diff -r 10184cd446e8 -r e5361bcd20dc usr.bin/make/Makefile
--- a/usr.bin/make/Makefile     Sun Jul 26 07:13:51 2020 +0000
+++ b/usr.bin/make/Makefile     Sun Jul 26 07:15:26 2020 +0000
@@ -1,15 +1,10 @@
-#      $NetBSD: Makefile,v 1.73 2020/07/25 21:00:48 rillig Exp $
+#      $NetBSD: Makefile,v 1.74 2020/07/26 07:15:26 rillig Exp $
 #      @(#)Makefile    5.2 (Berkeley) 12/28/90
 
 PROG=  make
-SRCS=  arch.c buf.c compat.c cond.c dir.c for.c hash.c job.c main.c
+SRCS=  arch.c buf.c compat.c cond.c dir.c for.c hash.c job.c lst.c main.c
 SRCS+= make.c make_malloc.c metachar.c parse.c
 SRCS+= str.c strlist.c suff.c targ.c trace.c var.c util.c
-SRCS+= lstAppend.c lstAtEnd.c lstAtFront.c lstClose.c lstConcat.c
-SRCS+= lstDatum.c lstDeQueue.c lstDestroy.c lstDupl.c lstEnQueue.c
-SRCS+= lstFind.c lstFindFrom.c lstFirst.c lstForEach.c lstForEachFrom.c
-SRCS+= lstInit.c lstInsert.c lstIsAtEnd.c lstIsEmpty.c lstLast.c lstMember.c
-SRCS+= lstNext.c lstOpen.c lstPrev.c lstRemove.c lstReplace.c lstSucc.c
 
 USE_COVERAGE?= no              # works only with gcc; clang9 fails to link
 .if ${USE_COVERAGE} == "yes"
diff -r 10184cd446e8 -r e5361bcd20dc usr.bin/make/lst.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/make/lst.c        Sun Jul 26 07:15:26 2020 +0000
@@ -0,0 +1,1214 @@
+/* $NetBSD: lst.c,v 1.1 2020/07/26 07:15:26 rillig Exp $ */
+
+/*
+ * Copyright (c) 1988, 1989, 1990, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Adam de Boor.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "lst.h"
+#include "make_malloc.h"
+
+#ifndef MAKE_NATIVE
+static char rcsid[] = "$NetBSD: lst.c,v 1.1 2020/07/26 07:15:26 rillig Exp $";
+#else
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: lst.c,v 1.1 2020/07/26 07:15:26 rillig Exp $");
+#endif /* not lint */
+#endif
+
+typedef struct ListNode {
+       struct ListNode *prevPtr;   /* previous element in list */
+       struct ListNode *nextPtr;   /* next in list */
+       unsigned int    useCount:8, /* Count of functions using the node.
+                                    * node may not be deleted until count
+                                    * goes to 0 */
+                       flags:8;    /* Node status flags */
+       void            *datum;     /* datum associated with this element */
+} *ListNode;
+/*
+ * Flags required for synchronization
+ */
+#define LN_DELETED     0x0001      /* List node should be removed when done */
+
+typedef enum {
+    Head, Middle, Tail, Unknown
+} Where;
+
+typedef struct List {
+       ListNode        firstPtr; /* first node in list */
+       ListNode        lastPtr;  /* last node in list */
+       Boolean         isCirc;   /* true if the list should be considered
+                                  * circular */
+/*
+ * fields for sequential access
+ */
+       Where           atEnd;    /* Where in the list the last access was */
+       Boolean         isOpen;   /* true if list has been Lst_Open'ed */
+       ListNode        curPtr;   /* current node, if open. NULL if
+                                  * *just* opened */
+       ListNode        prevPtr;  /* Previous node, if open. Used by
+                                  * Lst_Remove */
+} *List;
+
+/*
+ * PAlloc (var, ptype) --
+ *     Allocate a pointer-typedef structure 'ptype' into the variable 'var'
+ */
+#define        PAlloc(var,ptype)       var = (ptype) bmake_malloc(sizeof *(var))
+
+/*
+ * LstValid (l) --
+ *     Return TRUE if the list l is valid
+ */
+#define LstValid(l)    ((Lst)(l) != NULL)
+
+/*
+ * LstNodeValid (ln, l) --
+ *     Return TRUE if the LstNode ln is valid with respect to l
+ */
+#define LstNodeValid(ln, l)    ((ln) != NULL)
+
+/*
+ * LstIsEmpty (l) --
+ *     TRUE if the list l is empty.
+ */
+#define LstIsEmpty(l)  (((List)(l))->firstPtr == NULL)
+
+/*     $NetBSD: lst.c,v 1.1 2020/07/26 07:15:26 rillig Exp $   */
+
+/*-
+ *-----------------------------------------------------------------------
+ * Lst_Init --
+ *     Create and initialize a new list.
+ *
+ * Input:
+ *     circ            TRUE if the list should be made circular
+ *
+ * Results:
+ *     The created list.
+ *
+ * Side Effects:
+ *     A list is created, what else?
+ *
+ *-----------------------------------------------------------------------
+ */
+Lst
+Lst_Init(Boolean circ)
+{
+    List       nList;
+
+    PAlloc (nList, List);
+
+    nList->firstPtr = NULL;
+    nList->lastPtr = NULL;
+    nList->isOpen = FALSE;
+    nList->isCirc = circ;
+    nList->atEnd = Unknown;
+
+    return nList;
+}
+
+/*-
+ *-----------------------------------------------------------------------
+ * Lst_Duplicate --
+ *     Duplicate an entire list. If a function to copy a void *is
+ *     given, the individual client elements will be duplicated as well.
+ *
+ * Input:
+ *     l               the list to duplicate
+ *     copyProc        A function to duplicate each void *
+ *
+ * Results:
+ *     The new Lst structure or NULL if failure.
+ *
+ * Side Effects:
+ *     A new list is created.
+ *-----------------------------------------------------------------------
+ */
+Lst
+Lst_Duplicate(Lst l, DuplicateProc *copyProc)
+{
+    Lst        nl;
+    ListNode   ln;
+    List       list = l;
+
+    if (!LstValid (l)) {
+       return NULL;
+    }
+
+    nl = Lst_Init(list->isCirc);
+    if (nl == NULL) {
+       return NULL;
+    }
+
+    ln = list->firstPtr;
+    while (ln != NULL) {
+       if (copyProc != NULL) {
+           if (Lst_AtEnd(nl, copyProc(ln->datum)) == FAILURE) {
+               return NULL;
+           }
+       } else if (Lst_AtEnd(nl, ln->datum) == FAILURE) {
+           return NULL;
+       }
+
+       if (list->isCirc && ln == list->lastPtr) {
+           ln = NULL;
+       } else {
+           ln = ln->nextPtr;
+       }
+    }
+
+    return nl;
+}
+
+/*-
+ *-----------------------------------------------------------------------
+ * Lst_Destroy --
+ *     Destroy a list and free all its resources. If the freeProc is
+ *     given, it is called with the datum from each node in turn before
+ *     the node is freed.
+ *
+ * Results:
+ *     None.
+ *
+ * Side Effects:
+ *     The given list is freed in its entirety.
+ *
+ *-----------------------------------------------------------------------
+ */
+void
+Lst_Destroy(Lst list, FreeProc *freeProc)
+{
+    ListNode   ln;
+    ListNode   tln = NULL;
+
+    if (list == NULL)
+       return;
+
+    /* To ease scanning */
+    if (list->lastPtr != NULL)
+       list->lastPtr->nextPtr = NULL;
+    else {
+       free(list);
+       return;
+    }
+
+    if (freeProc) {
+       for (ln = list->firstPtr; ln != NULL; ln = tln) {
+            tln = ln->nextPtr;
+            freeProc(ln->datum);
+            free(ln);
+       }
+    } else {
+       for (ln = list->firstPtr; ln != NULL; ln = tln) {
+            tln = ln->nextPtr;
+            free(ln);
+       }
+    }
+
+    free(list);
+}
+
+/*
+ * Functions to modify a list
+ */
+
+/*-
+ *-----------------------------------------------------------------------
+ * Lst_InsertBefore --
+ *     Insert a new node with the given piece of data before the given
+ *     node in the given list.
+ *
+ * Input:
+ *     l               list to manipulate
+ *     ln              node before which to insert d
+ *     d               datum to be inserted
+ *
+ * Results:
+ *     SUCCESS or FAILURE.
+ *
+ * Side Effects:
+ *     the firstPtr field will be changed if ln is the first node in the
+ *     list.
+ *
+ *-----------------------------------------------------------------------
+ */
+ReturnStatus
+Lst_InsertBefore(Lst l, LstNode ln, void *d)
+{
+    ListNode   nLNode; /* new lnode for d */
+    ListNode   lNode = ln;
+    List       list = l;
+
+
+    /*
+     * check validity of arguments
+     */
+    if (LstValid (l) && (LstIsEmpty (l) && ln == NULL))



Home | Main Index | Thread Index | Old Index