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 API for iterating over hash table...



details:   https://anonhg.NetBSD.org/src/rev/1d5f300c943a
branches:  trunk
changeset: 1015226:1d5f300c943a
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Oct 18 10:44:25 2020 +0000

description:
make(1): make API for iterating over hash tables simpler

diffstat:

 usr.bin/make/dir.c  |  12 ++++------
 usr.bin/make/hash.c |  58 ++++++++++++++--------------------------------------
 usr.bin/make/hash.h |  18 +++++++---------
 usr.bin/make/main.c |  13 ++++++-----
 usr.bin/make/var.c  |  30 +++++++++++++-------------
 5 files changed, 51 insertions(+), 80 deletions(-)

diffs (288 lines):

diff -r 6c5e7ef53f50 -r 1d5f300c943a usr.bin/make/dir.c
--- a/usr.bin/make/dir.c        Sun Oct 18 10:17:09 2020 +0000
+++ b/usr.bin/make/dir.c        Sun Oct 18 10:44:25 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dir.c,v 1.163 2020/10/17 21:32:30 rillig Exp $ */
+/*     $NetBSD: dir.c,v 1.164 2020/10/18 10:44:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -135,7 +135,7 @@
 #include "job.h"
 
 /*     "@(#)dir.c      8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: dir.c,v 1.163 2020/10/17 21:32:30 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.164 2020/10/18 10:44:25 rillig Exp $");
 
 #define DIR_DEBUG0(text) DEBUG0(DIR, text)
 #define DIR_DEBUG1(fmt, arg1) DEBUG1(DIR, fmt, arg1)
@@ -608,16 +608,14 @@
 static void
 DirMatchFiles(const char *pattern, CachedDir *dir, StringList *expansions)
 {
-    Hash_Search search;                /* Index into the directory's table */
+    HashIter hi;
     Hash_Entry *entry;         /* Current entry in the table */
     Boolean isDot;             /* TRUE if the directory being searched is . */
 
     isDot = (dir->name[0] == '.' && dir->name[1] == '\0');
 
-    for (entry = Hash_EnumFirst(&dir->files, &search);
-        entry != NULL;
-        entry = Hash_EnumNext(&search))
-    {
+    HashIter_Init(&hi, &dir->files);
+    while ((entry = HashIter_Next(&hi)) != NULL) {
        /*
         * See if the file matches the given pattern. Note we follow the UNIX
         * convention that dot files will only be found if the pattern
diff -r 6c5e7ef53f50 -r 1d5f300c943a usr.bin/make/hash.c
--- a/usr.bin/make/hash.c       Sun Oct 18 10:17:09 2020 +0000
+++ b/usr.bin/make/hash.c       Sun Oct 18 10:44:25 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash.c,v 1.44 2020/10/05 20:21:30 rillig Exp $ */
+/*     $NetBSD: hash.c,v 1.45 2020/10/18 10:44:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -79,7 +79,7 @@
 #include "make.h"
 
 /*     "@(#)hash.c     8.1 (Berkeley) 6/6/93"  */
-MAKE_RCSID("$NetBSD: hash.c,v 1.44 2020/10/05 20:21:30 rillig Exp $");
+MAKE_RCSID("$NetBSD: hash.c,v 1.45 2020/10/18 10:44:25 rillig Exp $");
 
 /*
  * The ratio of # entries to # buckets at which we rebuild the table to
@@ -283,43 +283,29 @@
        abort();
 }
 
-/* Sets things up for enumerating all entries in the hash table.
- *
- * Input:
- *     t               Table to be searched.
- *     searchPtr       Area in which to keep state about search.
- *
- * Results:
- *     The return value is the address of the first entry in
- *     the hash table, or NULL if the table is empty.
- */
-Hash_Entry *
-Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
+/* Set things up for iterating over all entries in the hash table. */
+void
+HashIter_Init(HashIter *hi, Hash_Table *t)
 {
-       searchPtr->table = t;
-       searchPtr->nextBucket = 0;
-       searchPtr->entry = NULL;
-       return Hash_EnumNext(searchPtr);
+       hi->table = t;
+       hi->nextBucket = 0;
+       hi->entry = NULL;
 }
 
-/* Returns the next entry in the hash table, or NULL if the end of the table
- * is reached.
- *
- * Input:
- *     searchPtr       Area used to keep state about search.
- */
+/* Return the next entry in the hash table, or NULL if the end of the table
+ * is reached. */
 Hash_Entry *
-Hash_EnumNext(Hash_Search *searchPtr)
+HashIter_Next(HashIter *hi)
 {
        Hash_Entry *e;
-       Hash_Table *t = searchPtr->table;
+       Hash_Table *t = hi->table;
 
        /*
         * The entry field points to the most recently returned
         * entry, or is NULL if we are starting up.  If not NULL, we have
         * to start at the next one in the chain.
         */
-       e = searchPtr->entry;
+       e = hi->entry;
        if (e != NULL)
                e = e->next;
        /*
@@ -327,27 +313,15 @@
         * find the next nonempty chain.
         */
        while (e == NULL) {
-               if (searchPtr->nextBucket >= t->bucketsSize)
+               if (hi->nextBucket >= t->bucketsSize)
                        return NULL;
-               e = t->buckets[searchPtr->nextBucket++];
+               e = t->buckets[hi->nextBucket++];
        }
-       searchPtr->entry = e;
+       hi->entry = e;
        return e;
 }
 
 void
-Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
-{
-       Hash_Search search;
-       Hash_Entry *e;
-
-       for (e = Hash_EnumFirst(t, &search);
-            e != NULL;
-            e = Hash_EnumNext(&search))
-               action(Hash_GetValue(e), data);
-}
-
-void
 Hash_DebugStats(Hash_Table *t, const char *name)
 {
        DEBUG4(HASH, "Hash_Table %s: size=%u numEntries=%u maxchain=%u\n",
diff -r 6c5e7ef53f50 -r 1d5f300c943a usr.bin/make/hash.h
--- a/usr.bin/make/hash.h       Sun Oct 18 10:17:09 2020 +0000
+++ b/usr.bin/make/hash.h       Sun Oct 18 10:44:25 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hash.h,v 1.26 2020/10/05 20:21:30 rillig Exp $ */
+/*     $NetBSD: hash.h,v 1.27 2020/10/18 10:44:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -96,15 +96,12 @@
     unsigned int maxchain;     /* max length of chain detected */
 } Hash_Table;
 
-/*
- * The following structure is used by the searching routines
- * to record where we are in the search.
- */
-typedef struct Hash_Search {
+/* State of an iteration over all entries in a table. */
+typedef struct HashIter {
     Hash_Table *table;         /* Table being searched. */
     unsigned int nextBucket;   /* Next bucket to check (after current). */
     Hash_Entry *entry;         /* Next entry to check in current bucket. */
-} Hash_Search;
+} HashIter;
 
 static inline MAKE_ATTR_UNUSED void *
 Hash_GetValue(Hash_Entry *h)
@@ -124,9 +121,10 @@
 void *Hash_FindValue(Hash_Table *, const char *);
 Hash_Entry *Hash_CreateEntry(Hash_Table *, const char *, Boolean *);
 void Hash_DeleteEntry(Hash_Table *, Hash_Entry *);
-Hash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *);
-Hash_Entry *Hash_EnumNext(Hash_Search *);
-void Hash_ForEach(Hash_Table *, void (*)(void *, void *), void *);
+
+void HashIter_Init(HashIter *, Hash_Table *);
+Hash_Entry *HashIter_Next(HashIter *);
+
 void Hash_DebugStats(Hash_Table *, const char *);
 
 #endif /* MAKE_HASH_H */
diff -r 6c5e7ef53f50 -r 1d5f300c943a usr.bin/make/main.c
--- a/usr.bin/make/main.c       Sun Oct 18 10:17:09 2020 +0000
+++ b/usr.bin/make/main.c       Sun Oct 18 10:44:25 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.373 2020/10/18 08:01:23 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.374 2020/10/18 10:44:25 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -118,7 +118,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.373 2020/10/18 08:01:23 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.374 2020/10/18 10:44:25 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -1958,11 +1958,12 @@
 {
     GNode *cache = get_cached_realpaths();
     Hash_Entry *he, *nhe;
-    Hash_Search hs;
+    HashIter hi;
 
-    he = Hash_EnumFirst(&cache->context, &hs);
-    while (he) {
-       nhe = Hash_EnumNext(&hs);
+    HashIter_Init(&hi, &cache->context);
+    he = HashIter_Next(&hi);
+    while (he != NULL) {
+       nhe = HashIter_Next(&hi);
        if (he->name[0] != '/') {
            if (DEBUG(DIR))
                fprintf(stderr, "cached_realpath: purging %s\n", he->name);
diff -r 6c5e7ef53f50 -r 1d5f300c943a usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Oct 18 10:17:09 2020 +0000
+++ b/usr.bin/make/var.c        Sun Oct 18 10:44:25 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.574 2020/10/18 08:58:29 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.575 2020/10/18 10:44:25 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -121,7 +121,7 @@
 #include    "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.574 2020/10/18 08:58:29 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.575 2020/10/18 10:44:25 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -551,13 +551,6 @@
     return TRUE;
 }
 
-static void
-Var_ExportVars_callback(void *entry, void *unused MAKE_ATTR_UNUSED)
-{
-    Var *var = entry;
-    Var_Export1(var->name, 0);
-}
-
 /*
  * This gets called from our children.
  */
@@ -580,8 +573,15 @@
        return;
 
     if (var_exportedVars == VAR_EXPORTED_ALL) {
-       /* Ouch! This is crazy... */
-       Hash_ForEach(&VAR_GLOBAL->context, Var_ExportVars_callback, NULL);
+        HashIter hi;
+        Hash_Entry *he;
+
+       /* Ouch! Exporting all variables at once is crazy... */
+       HashIter_Init(&hi, &VAR_GLOBAL->context);
+       while ((he = HashIter_Next(&hi)) != NULL) {
+           Var *var = Hash_GetValue(he);
+           Var_Export1(var->name, 0);
+       }
        return;
     }
 
@@ -3857,14 +3857,14 @@
 Var_Dump(GNode *ctxt)
 {
     Vector varnames;
-    Hash_Search iter;
+    HashIter hi;
     Hash_Entry *he;
     size_t i;
 
     Vector_Init(&varnames);
-    for (he = Hash_EnumFirst(&ctxt->context, &iter);
-        he != NULL;
-        he = Hash_EnumNext(&iter))
+
+    HashIter_Init(&hi, &ctxt->context);
+    while ((he = HashIter_Next(&hi)) != NULL)
        Vector_Push(&varnames, he->name);
 
     qsort(varnames.items, varnames.len, sizeof varnames.items[0], str_cmp_asc);



Home | Main Index | Thread Index | Old Index