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): reduce memory allocation in suffix han...



details:   https://anonhg.NetBSD.org/src/rev/0c6c5570e253
branches:  trunk
changeset: 946489:0c6c5570e253
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Nov 29 00:54:43 2020 +0000

description:
make(1): reduce memory allocation in suffix handling

The function Lst_MoveAll previously freed the source list.  This
function was only used in a few places, and none of them really needed
the allocation.

diffstat:

 usr.bin/make/lst.c  |   7 ++---
 usr.bin/make/suff.c |  60 +++++++++++++++++++++++-----------------------------
 2 files changed, 30 insertions(+), 37 deletions(-)

diffs (238 lines):

diff -r 73abe699a725 -r 0c6c5570e253 usr.bin/make/lst.c
--- a/usr.bin/make/lst.c        Sun Nov 29 00:42:01 2020 +0000
+++ b/usr.bin/make/lst.c        Sun Nov 29 00:54:43 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lst.c,v 1.96 2020/11/28 19:26:10 rillig Exp $ */
+/* $NetBSD: lst.c,v 1.97 2020/11/29 00:54:43 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -34,7 +34,7 @@
 
 #include "make.h"
 
-MAKE_RCSID("$NetBSD: lst.c,v 1.96 2020/11/28 19:26:10 rillig Exp $");
+MAKE_RCSID("$NetBSD: lst.c,v 1.97 2020/11/29 00:54:43 rillig Exp $");
 
 static ListNode *
 LstNodeNew(ListNode *prev, ListNode *next, void *datum)
@@ -207,7 +207,7 @@
 }
 
 /* Move all nodes from src to the end of dst.
- * The source list is destroyed and freed. */
+ * The source list becomes empty but is not freed. */
 void
 Lst_MoveAll(List *dst, List *src)
 {
@@ -220,7 +220,6 @@
 
                dst->last = src->last;
        }
-       free(src);
 }
 
 /* Copy the element data from src to the start of dst. */
diff -r 73abe699a725 -r 0c6c5570e253 usr.bin/make/suff.c
--- a/usr.bin/make/suff.c       Sun Nov 29 00:42:01 2020 +0000
+++ b/usr.bin/make/suff.c       Sun Nov 29 00:54:43 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: suff.c,v 1.315 2020/11/28 22:56:01 rillig Exp $        */
+/*     $NetBSD: suff.c,v 1.316 2020/11/29 00:54:43 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -114,7 +114,7 @@
 #include "dir.h"
 
 /*     "@(#)suff.c     8.4 (Berkeley) 3/21/94" */
-MAKE_RCSID("$NetBSD: suff.c,v 1.315 2020/11/28 22:56:01 rillig Exp $");
+MAKE_RCSID("$NetBSD: suff.c,v 1.316 2020/11/29 00:54:43 rillig Exp $");
 
 #define SUFF_DEBUG0(text) DEBUG0(SUFF, text)
 #define SUFF_DEBUG1(fmt, arg1) DEBUG1(SUFF, fmt, arg1)
@@ -126,7 +126,7 @@
 typedef List CandidateList;
 typedef ListNode CandidateListNode;
 
-static SuffixList *sufflist;   /* List of suffixes */
+static SuffixList sufflist = LST_INIT; /* List of suffixes */
 #ifdef CLEANUP
 static SuffixList *suffClean;  /* List of suffixes to be cleaned */
 #endif
@@ -311,7 +311,7 @@
 {
     SuffixListNode *ln;
 
-    for (ln = sufflist->first; ln != NULL; ln = ln->next) {
+    for (ln = sufflist.first; ln != NULL; ln = ln->next) {
        Suffix *suff = ln->datum;
        if (suff->nameLen == nameLen && memcmp(suff->name, name, nameLen) == 0)
            return suff;
@@ -387,7 +387,7 @@
     SuffixList_Unref(list, suff);
     if (suff->refCount == 0) {
        /* XXX: can lead to suff->refCount == -1 */
-       SuffixList_Unref(sufflist, suff);
+       SuffixList_Unref(&sufflist, suff);
        DEBUG1(SUFF, "Removing suffix \"%s\"\n", suff->name);
        SuffFree(suff);
     }
@@ -458,10 +458,10 @@
 Suff_ClearSuffixes(void)
 {
 #ifdef CLEANUP
-    Lst_MoveAll(suffClean, sufflist);
+    Lst_MoveAll(suffClean, &sufflist);
 #endif
     DEBUG0(SUFF, "Clearing all suffixes\n");
-    sufflist = Lst_New();
+    Lst_Init(&sufflist);
     sNum = 0;
     if (nullSuff != NULL)
        SuffFree(nullSuff);
@@ -489,7 +489,7 @@
      * we can find two that meet these criteria, we've successfully
      * parsed the string.
      */
-    for (ln = sufflist->first; ln != NULL; ln = ln->next) {
+    for (ln = sufflist.first; ln != NULL; ln = ln->next) {
        Suffix *src = ln->datum;
 
        if (StrTrimPrefix(src->name, str) == NULL)
@@ -796,7 +796,7 @@
        return;
 
     suff = Suffix_New(name);
-    Lst_Append(sufflist, suff);
+    Lst_Append(&sufflist, suff);
     DEBUG1(SUFF, "Adding suffix \"%s\"\n", suff->name);
 
     UpdateTargets(inout_main, suff);
@@ -838,7 +838,7 @@
     SearchPath *inIncludes = SearchPath_New(); /* Cumulative .INCLUDES path */
     SearchPath *inLibs = SearchPath_New();     /* Cumulative .LIBS path */
 
-    for (ln = sufflist->first; ln != NULL; ln = ln->next) {
+    for (ln = sufflist.first; ln != NULL; ln = ln->next) {
        Suffix *suff = ln->datum;
        if (!Lst_IsEmpty(suff->searchPath)) {
 #ifdef INCLUDES
@@ -1408,7 +1408,7 @@
        char *name = gn->name;
        size_t nameLen = strlen(gn->name);
        SuffixListNode *ln;
-       for (ln = sufflist->first; ln != NULL; ln = ln->next)
+       for (ln = sufflist.first; ln != NULL; ln = ln->next)
            if (Suffix_IsSuffix(ln->datum, nameLen, name + nameLen))
                break;
 
@@ -1664,7 +1664,7 @@
     Candidate *targ;
     char *pref;
 
-    for (ln = sufflist->first; ln != NULL; ln = ln->next) {
+    for (ln = sufflist.first; ln != NULL; ln = ln->next) {
        Suffix *suff = ln->datum;
        if (!Suffix_IsSuffix(suff, nameLen, name + nameLen))
            continue;
@@ -1781,10 +1781,11 @@
 static void
 FindDepsRegular(GNode *gn, CandidateSearcher *cs)
 {
-    CandidateList *srcs;       /* List of sources at which to look */
-    CandidateList *targs;      /* List of targets to which things can be
-                                * transformed. They all have the same file,
-                                * but different suff and prefix fields */
+    /* List of sources at which to look */
+    CandidateList srcs = LST_INIT;
+    /* List of targets to which things can be transformed.
+     * They all have the same file, but different suff and prefix fields. */
+    CandidateList targs = LST_INIT;
     Candidate *bottom;         /* Start of found transformation path */
     Candidate *src;
     Candidate *targ;
@@ -1797,12 +1798,6 @@
 #endif
 
     /*
-     * Begin at the beginning...
-     */
-    srcs = Lst_New();
-    targs = Lst_New();
-
-    /*
      * We're caught in a catch-22 here. On the one hand, we want to use any
      * transformation implied by the target's sources, but we can't examine
      * the sources until we've expanded any variables/wildcards they may hold,
@@ -1825,24 +1820,24 @@
 
     if (!(gn->type & OP_PHONY)) {
 
-       FindDepsRegularKnown(name, nameLen, gn, srcs, targs);
+       FindDepsRegularKnown(name, nameLen, gn, &srcs, &targs);
 
        /* Handle target of unknown suffix... */
-       FindDepsRegularUnknown(gn, name, srcs, targs);
+       FindDepsRegularUnknown(gn, name, &srcs, &targs);
 
        /*
         * Using the list of possible sources built up from the target
         * suffix(es), try and find an existing file/target that matches.
         */
-       bottom = FindThem(srcs, cs);
+       bottom = FindThem(&srcs, cs);
 
        if (bottom == NULL) {
            /*
             * No known transformations -- use the first suffix found
             * for setting the local variables.
             */
-           if (targs->first != NULL)
-               targ = targs->first->datum;
+           if (targs.first != NULL)
+               targ = targs.first->datum;
            else
                targ = NULL;
        } else {
@@ -1962,11 +1957,11 @@
     if (bottom != NULL)
        CandidateSearcher_AddIfNew(cs, bottom);
 
-    while (RemoveCandidate(srcs) || RemoveCandidate(targs))
+    while (RemoveCandidate(&srcs) || RemoveCandidate(&targs))
        continue;
 
-    CandidateSearcher_MoveAll(cs, srcs);
-    CandidateSearcher_MoveAll(cs, targs);
+    CandidateSearcher_MoveAll(cs, &srcs);
+    CandidateSearcher_MoveAll(cs, &targs);
 }
 
 static void
@@ -2061,7 +2056,6 @@
 {
 #ifdef CLEANUP
     suffClean = Lst_New();
-    sufflist = Lst_New();
 #endif
     transforms = Lst_New();
 
@@ -2079,7 +2073,7 @@
 Suff_End(void)
 {
 #ifdef CLEANUP
-    Lst_Destroy(sufflist, SuffFree);
+    Lst_DoneCall(&sufflist, SuffFree);
     Lst_Destroy(suffClean, SuffFree);
     if (nullSuff != NULL)
        SuffFree(nullSuff);
@@ -2140,7 +2134,7 @@
     debug_printf("#*** Suffixes:\n");
     {
        SuffixListNode *ln;
-       for (ln = sufflist->first; ln != NULL; ln = ln->next)
+       for (ln = sufflist.first; ln != NULL; ln = ln->next)
            Suffix_Print(ln->datum);
     }
 



Home | Main Index | Thread Index | Old Index