Module Name: src
Committed By: rillig
Date: Sat Nov 14 21:29:44 UTC 2020
Modified Files:
src/usr.bin/make: arch.c dir.c hash.c hash.h var.c
Log Message:
make(1): replace a few HashTable_CreateEntry with HashTable_Set
Instead of HashTable_CreateEntry and HashEntry_Set, several places just
need the HashEntry for storing a value in it. This makes the calling
code simpler to understand.
These parts of the code are already hard enough to understand since they
are about memory management and aliasing. Having a too detailed API for
the HashTable only distracts from these topics.
To generate a diff of this commit:
cvs rdiff -u -r1.176 -r1.177 src/usr.bin/make/arch.c
cvs rdiff -u -r1.209 -r1.210 src/usr.bin/make/dir.c
cvs rdiff -u -r1.56 -r1.57 src/usr.bin/make/hash.c
cvs rdiff -u -r1.32 -r1.33 src/usr.bin/make/hash.h
cvs rdiff -u -r1.684 -r1.685 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/arch.c
diff -u src/usr.bin/make/arch.c:1.176 src/usr.bin/make/arch.c:1.177
--- src/usr.bin/make/arch.c:1.176 Sat Nov 14 06:10:28 2020
+++ src/usr.bin/make/arch.c Sat Nov 14 21:29:44 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: arch.c,v 1.176 2020/11/14 06:10:28 rillig Exp $ */
+/* $NetBSD: arch.c,v 1.177 2020/11/14 21:29:44 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@@ -125,7 +125,7 @@
#include "config.h"
/* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
-MAKE_RCSID("$NetBSD: arch.c,v 1.176 2020/11/14 06:10:28 rillig Exp $");
+MAKE_RCSID("$NetBSD: arch.c,v 1.177 2020/11/14 21:29:44 rillig Exp $");
typedef struct List ArchList;
typedef struct ListNode ArchListNode;
@@ -557,10 +557,9 @@ ArchStatMember(const char *archive, cons
#endif
{
- HashEntry *he;
- he = HashTable_CreateEntry(&ar->members, memName, NULL);
- HashEntry_Set(he, bmake_malloc(sizeof arh));
- memcpy(HashEntry_Get(he), &arh, sizeof arh);
+ struct ar_hdr *cached_hdr = bmake_malloc(sizeof *cached_hdr);
+ memcpy(cached_hdr, &arh, sizeof arh);
+ HashTable_Set(&ar->members, memName, cached_hdr);
}
if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
Index: src/usr.bin/make/dir.c
diff -u src/usr.bin/make/dir.c:1.209 src/usr.bin/make/dir.c:1.210
--- src/usr.bin/make/dir.c:1.209 Sat Nov 14 19:36:31 2020
+++ src/usr.bin/make/dir.c Sat Nov 14 21:29:44 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: dir.c,v 1.209 2020/11/14 19:36:31 rillig Exp $ */
+/* $NetBSD: dir.c,v 1.210 2020/11/14 21:29:44 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -134,7 +134,7 @@
#include "job.h"
/* "@(#)dir.c 8.2 (Berkeley) 1/2/94" */
-MAKE_RCSID("$NetBSD: dir.c,v 1.209 2020/11/14 19:36:31 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.210 2020/11/14 21:29:44 rillig Exp $");
#define DIR_DEBUG0(text) DEBUG0(DIR, text)
#define DIR_DEBUG1(fmt, arg1) DEBUG1(DIR, fmt, arg1)
@@ -253,12 +253,10 @@ OpenDirs_Find(OpenDirs *odirs, const cha
static void
OpenDirs_Add(OpenDirs *odirs, CachedDir *cdir)
{
- HashEntry *he = HashTable_FindEntry(&odirs->table, cdir->name);
- if (he != NULL)
+ if (HashTable_FindEntry(&odirs->table, cdir->name) != NULL)
return;
- he = HashTable_CreateEntry(&odirs->table, cdir->name, NULL);
Lst_Append(odirs->list, cdir);
- HashEntry_Set(he, odirs->list->last);
+ HashTable_Set(&odirs->table, cdir->name, odirs->list->last);
}
static void
@@ -313,7 +311,6 @@ cached_stats(const char *pathname, struc
CachedStatsFlags flags)
{
HashTable *tbl = flags & CST_LSTAT ? &lmtimes : &mtimes;
- HashEntry *entry;
struct stat sys_st;
struct cached_stat *cst;
int rc;
@@ -321,11 +318,8 @@ cached_stats(const char *pathname, struc
if (pathname == NULL || pathname[0] == '\0')
return -1; /* This can happen in meta mode. */
- entry = HashTable_FindEntry(tbl, pathname);
-
- if (entry != NULL && !(flags & CST_UPDATE)) {
- cst = HashEntry_Get(entry);
-
+ cst = HashTable_FindValue(tbl, pathname);
+ if (cst != NULL && !(flags & CST_UPDATE)) {
*out_cst = *cst;
DIR_DEBUG2("Using cached time %s for %s\n",
Targ_FmtTime(cst->cst_mtime), pathname);
@@ -339,13 +333,9 @@ cached_stats(const char *pathname, struc
if (sys_st.st_mtime == 0)
sys_st.st_mtime = 1; /* avoid confusion with missing file */
- if (entry != NULL)
- cst = entry->value;
- else {
- entry = HashTable_CreateEntry(tbl, pathname, NULL);
+ if (cst == NULL) {
cst = bmake_malloc(sizeof *cst);
- memset(cst, 0, sizeof *cst);
- HashEntry_Set(entry, cst);
+ HashTable_Set(tbl, pathname, cst);
}
cst->cst_mtime = sys_st.st_mtime;
Index: src/usr.bin/make/hash.c
diff -u src/usr.bin/make/hash.c:1.56 src/usr.bin/make/hash.c:1.57
--- src/usr.bin/make/hash.c:1.56 Thu Nov 5 17:27:16 2020
+++ src/usr.bin/make/hash.c Sat Nov 14 21:29:44 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: hash.c,v 1.56 2020/11/05 17:27:16 rillig Exp $ */
+/* $NetBSD: hash.c,v 1.57 2020/11/14 21:29:44 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -74,7 +74,7 @@
#include "make.h"
/* "@(#)hash.c 8.1 (Berkeley) 6/6/93" */
-MAKE_RCSID("$NetBSD: hash.c,v 1.56 2020/11/05 17:27:16 rillig Exp $");
+MAKE_RCSID("$NetBSD: hash.c,v 1.57 2020/11/14 21:29:44 rillig Exp $");
/*
* The ratio of # entries to # buckets at which we rebuild the table to
@@ -253,6 +253,14 @@ HashTable_CreateEntry(HashTable *t, cons
return he;
}
+HashEntry *
+HashTable_Set(HashTable *t, const char *key, void *value)
+{
+ HashEntry *he = HashTable_CreateEntry(t, key, NULL);
+ HashEntry_Set(he, value);
+ return he;
+}
+
/* Delete the entry from the table and free the associated memory. */
void
HashTable_DeleteEntry(HashTable *t, HashEntry *he)
Index: src/usr.bin/make/hash.h
diff -u src/usr.bin/make/hash.h:1.32 src/usr.bin/make/hash.h:1.33
--- src/usr.bin/make/hash.h:1.32 Tue Nov 10 00:32:12 2020
+++ src/usr.bin/make/hash.h Sat Nov 14 21:29:44 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: hash.h,v 1.32 2020/11/10 00:32:12 rillig Exp $ */
+/* $NetBSD: hash.h,v 1.33 2020/11/14 21:29:44 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -122,6 +122,7 @@ void *HashTable_FindValue(HashTable *, c
unsigned int Hash_Hash(const char *);
void *HashTable_FindValueHash(HashTable *, const char *, unsigned int);
HashEntry *HashTable_CreateEntry(HashTable *, const char *, Boolean *);
+HashEntry *HashTable_Set(HashTable *, const char *, void *);
void HashTable_DeleteEntry(HashTable *, HashEntry *);
void HashTable_DebugStats(HashTable *, const char *);
Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.684 src/usr.bin/make/var.c:1.685
--- src/usr.bin/make/var.c:1.684 Tue Nov 10 00:32:12 2020
+++ src/usr.bin/make/var.c Sat Nov 14 21:29:44 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: var.c,v 1.684 2020/11/10 00:32:12 rillig Exp $ */
+/* $NetBSD: var.c,v 1.685 2020/11/14 21:29:44 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@@ -130,7 +130,7 @@
#include "metachar.h"
/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.684 2020/11/10 00:32:12 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.685 2020/11/14 21:29:44 rillig Exp $");
#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -953,8 +953,6 @@ Var_Append(const char *name, const char
ctxt->name, name, Buf_GetAll(&v->val, NULL));
if (v->flags & VAR_FROM_ENV) {
- HashEntry *h;
-
/*
* If the original variable came from the environment, we
* have to install it in the global context (we could place
@@ -962,8 +960,9 @@ Var_Append(const char *name, const char
* export other variables...)
*/
v->flags &= ~(unsigned)VAR_FROM_ENV;
- h = HashTable_CreateEntry(&ctxt->context, name, NULL);
- HashEntry_Set(h, v);
+ /* This is the only place where a variable is created whose
+ * v->name is not the same as ctxt->context->key. */
+ HashTable_Set(&ctxt->context, name, v);
}
}
free(name_freeIt);