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.
This commit is contained in:
rillig 2020-11-14 21:29:44 +00:00
parent a7b28dcb09
commit 64455685f0
5 changed files with 30 additions and 33 deletions

View File

@ -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, const char *member, Boolean addToCache)
#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)

View File

@ -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 char *name)
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, struct cached_stat *out_cst,
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, struct cached_stat *out_cst,
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, struct cached_stat *out_cst,
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;

View File

@ -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, const char *key, Boolean *out_isNew)
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)

View File

@ -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 *, const char *);
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 *);

View File

@ -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 *val, GNode *ctxt)
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 *val, GNode *ctxt)
* 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);