Module Name: src Committed By: rillig Date: Sun Oct 18 10:44:25 UTC 2020
Modified Files: src/usr.bin/make: dir.c hash.c hash.h main.c var.c Log Message: make(1): make API for iterating over hash tables simpler To generate a diff of this commit: cvs rdiff -u -r1.163 -r1.164 src/usr.bin/make/dir.c cvs rdiff -u -r1.44 -r1.45 src/usr.bin/make/hash.c cvs rdiff -u -r1.26 -r1.27 src/usr.bin/make/hash.h cvs rdiff -u -r1.373 -r1.374 src/usr.bin/make/main.c cvs rdiff -u -r1.574 -r1.575 src/usr.bin/make/var.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/dir.c diff -u src/usr.bin/make/dir.c:1.163 src/usr.bin/make/dir.c:1.164 --- src/usr.bin/make/dir.c:1.163 Sat Oct 17 21:32:30 2020 +++ src/usr.bin/make/dir.c Sun Oct 18 10:44:25 2020 @@ -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 @@ Dir_HasWildcards(const char *name) 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 Index: src/usr.bin/make/hash.c diff -u src/usr.bin/make/hash.c:1.44 src/usr.bin/make/hash.c:1.45 --- src/usr.bin/make/hash.c:1.44 Mon Oct 5 20:21:30 2020 +++ src/usr.bin/make/hash.c Sun Oct 18 10:44:25 2020 @@ -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 @@ Hash_DeleteEntry(Hash_Table *t, Hash_Ent 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 @@ Hash_EnumNext(Hash_Search *searchPtr) * 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", Index: src/usr.bin/make/hash.h diff -u src/usr.bin/make/hash.h:1.26 src/usr.bin/make/hash.h:1.27 --- src/usr.bin/make/hash.h:1.26 Mon Oct 5 20:21:30 2020 +++ src/usr.bin/make/hash.h Sun Oct 18 10:44:25 2020 @@ -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 @@ typedef struct Hash_Table { 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 @@ Hash_Entry *Hash_FindEntry(Hash_Table *, 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 */ Index: src/usr.bin/make/main.c diff -u src/usr.bin/make/main.c:1.373 src/usr.bin/make/main.c:1.374 --- src/usr.bin/make/main.c:1.373 Sun Oct 18 08:01:23 2020 +++ src/usr.bin/make/main.c Sun Oct 18 10:44:25 2020 @@ -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 @@ purge_cached_realpaths(void) { 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); Index: src/usr.bin/make/var.c diff -u src/usr.bin/make/var.c:1.574 src/usr.bin/make/var.c:1.575 --- src/usr.bin/make/var.c:1.574 Sun Oct 18 08:58:29 2020 +++ src/usr.bin/make/var.c Sun Oct 18 10:44:25 2020 @@ -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 @@ Var_Export1(const char *name, VarExportF 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 @@ Var_ExportVars(void) 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 @@ void 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);