Module Name: src
Committed By: rillig
Date: Sat Nov 14 19:24:24 UTC 2020
Modified Files:
src/usr.bin/make: dir.c dir.h meta.c
Log Message:
make(1): remove redundant struct make_stat
In the cache for stat(2) and lstat(2), only one of the two timestamps
was ever used. To prevent a result from stat(2) leaking into the cache
for lstat(2), there have been two completely separate caches all the
time. Using different fields in the struct was therefore unnecessary.
By removing the redundant field, the internal struct in the cache is the
same as the external struct. This makes one of them redundant, thus
struct make_stat has been renamed to cached_stat, which better describes
its purpose, and the internal struct cache_st has been removed.
Just as before, the cache prevents any direct access to its internal
data. When passing it to the caller, it is copied.
Just as before, the field names of struct cached_stat cannot correspond
to those from struct stat, since the latter are often defined as macros.
Therefore they are prefixed with cst instead of st.
The redundancy had been added on 2020-06-05.
To generate a diff of this commit:
cvs rdiff -u -r1.207 -r1.208 src/usr.bin/make/dir.c
cvs rdiff -u -r1.33 -r1.34 src/usr.bin/make/dir.h
cvs rdiff -u -r1.142 -r1.143 src/usr.bin/make/meta.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.207 src/usr.bin/make/dir.c:1.208
--- src/usr.bin/make/dir.c:1.207 Sat Nov 14 11:51:58 2020
+++ src/usr.bin/make/dir.c Sat Nov 14 19:24:24 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: dir.c,v 1.207 2020/11/14 11:51:58 rillig Exp $ */
+/* $NetBSD: dir.c,v 1.208 2020/11/14 19:24:24 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.207 2020/11/14 11:51:58 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.208 2020/11/14 19:24:24 rillig Exp $");
#define DIR_DEBUG0(text) DEBUG0(DIR, text)
#define DIR_DEBUG1(fmt, arg1) DEBUG1(DIR, fmt, arg1)
@@ -300,31 +300,22 @@ static HashTable mtimes;
static HashTable lmtimes; /* same as mtimes but for lstat */
-/*
- * We use stat(2) a lot, cache the results.
- * mtime and mode are all we care about.
- */
-struct cache_st {
- time_t lmtime; /* lstat; XXX: is probably redundant */
- time_t mtime; /* stat */
- mode_t mode;
-};
-
typedef enum CachedStatsFlags {
CST_NONE = 0,
CST_LSTAT = 1 << 0, /* call lstat(2) instead of stat(2) */
CST_UPDATE = 1 << 1 /* ignore existing cached entry */
} CachedStatsFlags;
-/* Returns 0 and the result of stat(2) or lstat(2) in *mst, or -1 on error. */
+/* Returns 0 and the result of stat(2) or lstat(2) in *out_cst,
+ * or -1 on error. */
static int
-cached_stats(const char *pathname, struct make_stat *mst,
+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 cache_st *cst;
+ struct cached_stat *cst;
int rc;
if (pathname == NULL || pathname[0] == '\0')
@@ -335,31 +326,19 @@ cached_stats(const char *pathname, struc
if (entry != NULL && !(flags & CST_UPDATE)) {
cst = HashEntry_Get(entry);
- mst->mst_mode = cst->mode;
- mst->mst_mtime = (flags & CST_LSTAT) ? cst->lmtime : cst->mtime;
- /* XXX: Checking for mst_mtime != 0 is probably redundant since
- * nonexistent files are not cached. */
- if (mst->mst_mtime != 0) {
- DIR_DEBUG2("Using cached time %s for %s\n",
- Targ_FmtTime(mst->mst_mtime), pathname);
- return 0;
- }
- /* Continue with the normal lookup to see whether the file has been
- * created in the meantime. */
- }
-
- rc = (flags & CST_LSTAT)
- ? lstat(pathname, &sys_st)
- : stat(pathname, &sys_st);
+ *out_cst = *cst;
+ DIR_DEBUG2("Using cached time %s for %s\n",
+ Targ_FmtTime(cst->cst_mtime), pathname);
+ return 0;
+ }
+
+ rc = (flags & CST_LSTAT ? lstat : stat)(pathname, &sys_st);
if (rc == -1)
- return -1;
+ return -1; /* don't cache negative lookups */
if (sys_st.st_mtime == 0)
sys_st.st_mtime = 1; /* avoid confusion with missing file */
- mst->mst_mode = sys_st.st_mode;
- mst->mst_mtime = sys_st.st_mtime;
-
if (entry != NULL)
cst = entry->value;
else {
@@ -368,12 +347,11 @@ cached_stats(const char *pathname, struc
memset(cst, 0, sizeof *cst);
HashEntry_Set(entry, cst);
}
- if (flags & CST_LSTAT) {
- cst->lmtime = sys_st.st_mtime;
- } else {
- cst->mtime = sys_st.st_mtime;
- }
- cst->mode = sys_st.st_mode;
+
+ cst->cst_mtime = sys_st.st_mtime;
+ cst->cst_mode = sys_st.st_mode;
+
+ *out_cst = *cst;
DIR_DEBUG2(" Caching %s for %s\n",
Targ_FmtTime(sys_st.st_mtime), pathname);
@@ -381,15 +359,15 @@ cached_stats(const char *pathname, struc
}
int
-cached_stat(const char *pathname, struct make_stat *st)
+cached_stat(const char *pathname, struct cached_stat *cst)
{
- return cached_stats(pathname, st, CST_NONE);
+ return cached_stats(pathname, cst, CST_NONE);
}
int
-cached_lstat(const char *pathname, struct make_stat *st)
+cached_lstat(const char *pathname, struct cached_stat *cst)
{
- return cached_stats(pathname, st, CST_LSTAT);
+ return cached_stats(pathname, cst, CST_LSTAT);
}
/* Initialize the directories module. */
@@ -896,13 +874,13 @@ DirLookup(CachedDir *dir, const char *ba
static char *
DirLookupSubdir(CachedDir *dir, const char *name)
{
- struct make_stat mst;
+ struct cached_stat cst;
char *file = dir == dot ? bmake_strdup(name)
: str_concat3(dir->name, "/", name);
DIR_DEBUG1("checking %s ...\n", file);
- if (cached_stat(file, &mst) == 0) {
+ if (cached_stat(file, &cst) == 0) {
nearmisses++;
return file;
}
@@ -991,7 +969,7 @@ Dir_FindFile(const char *name, SearchPat
const char *base; /* Terminal name of file */
Boolean hasLastDot = FALSE; /* true if we should search dot last */
Boolean hasSlash; /* true if 'name' contains a / */
- struct make_stat mst; /* Buffer for stat, if necessary */
+ struct cached_stat cst; /* Buffer for stat, if necessary */
const char *trailing_dot = ".";
/*
@@ -1219,7 +1197,7 @@ Dir_FindFile(const char *name, SearchPat
DIR_DEBUG1(" Looking for \"%s\" ...\n", name);
bigmisses++;
- if (cached_stat(name, &mst) == 0) {
+ if (cached_stat(name, &cst) == 0) {
return bmake_strdup(name);
}
@@ -1242,7 +1220,7 @@ Dir_FindFile(const char *name, SearchPat
char *
Dir_FindHereOrAbove(const char *here, const char *search_path)
{
- struct make_stat mst;
+ struct cached_stat cst;
char *dirbase, *dirbase_end;
char *try, *try_end;
@@ -1255,12 +1233,12 @@ Dir_FindHereOrAbove(const char *here, co
/* try and stat(2) it ... */
try = str_concat3(dirbase, "/", search_path);
- if (cached_stat(try, &mst) != -1) {
+ if (cached_stat(try, &cst) != -1) {
/*
* success! if we found a file, chop off
* the filename so we return a directory.
*/
- if ((mst.mst_mode & S_IFMT) != S_IFDIR) {
+ if ((cst.cst_mode & S_IFMT) != S_IFDIR) {
try_end = try + strlen(try);
while (try_end > try && *try_end != '/')
try_end--;
@@ -1299,8 +1277,8 @@ Dir_FindHereOrAbove(const char *here, co
void
Dir_UpdateMTime(GNode *gn, Boolean recheck)
{
- char *fullName; /* the full pathname of name */
- struct make_stat mst; /* buffer for finding the mod time */
+ char *fullName;
+ struct cached_stat cst;
CachedStatsFlags flags;
if (gn->type & OP_ARCHV) {
@@ -1357,7 +1335,7 @@ Dir_UpdateMTime(GNode *gn, Boolean reche
fullName = bmake_strdup(gn->name);
flags = recheck ? CST_UPDATE : CST_NONE;
- if (cached_stats(fullName, &mst, flags) < 0) {
+ if (cached_stats(fullName, &cst, flags) < 0) {
if (gn->type & OP_MEMBER) {
if (fullName != gn->path)
free(fullName);
@@ -1365,13 +1343,13 @@ Dir_UpdateMTime(GNode *gn, Boolean reche
return;
}
- mst.mst_mtime = 0;
+ cst.cst_mtime = 0;
}
if (fullName != NULL && gn->path == NULL)
gn->path = fullName;
- gn->mtime = mst.mst_mtime;
+ gn->mtime = cst.cst_mtime;
}
/* Read the list of filenames in the directory and store the result
Index: src/usr.bin/make/dir.h
diff -u src/usr.bin/make/dir.h:1.33 src/usr.bin/make/dir.h:1.34
--- src/usr.bin/make/dir.h:1.33 Sun Nov 8 09:34:55 2020
+++ src/usr.bin/make/dir.h Sat Nov 14 19:24:24 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: dir.h,v 1.33 2020/11/08 09:34:55 rillig Exp $ */
+/* $NetBSD: dir.h,v 1.34 2020/11/14 19:24:24 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -112,12 +112,12 @@ void Dir_Destroy(void *);
SearchPath *Dir_CopyDirSearchPath(void);
/* Stripped-down variant of struct stat. */
-struct make_stat {
- time_t mst_mtime;
- mode_t mst_mode;
+struct cached_stat {
+ time_t cst_mtime;
+ mode_t cst_mode;
};
-int cached_lstat(const char *, struct make_stat *);
-int cached_stat(const char *, struct make_stat *);
+int cached_lstat(const char *, struct cached_stat *);
+int cached_stat(const char *, struct cached_stat *);
#endif /* MAKE_DIR_H */
Index: src/usr.bin/make/meta.c
diff -u src/usr.bin/make/meta.c:1.142 src/usr.bin/make/meta.c:1.143
--- src/usr.bin/make/meta.c:1.142 Sat Nov 14 17:39:59 2020
+++ src/usr.bin/make/meta.c Sat Nov 14 19:24:24 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: meta.c,v 1.142 2020/11/14 17:39:59 rillig Exp $ */
+/* $NetBSD: meta.c,v 1.143 2020/11/14 19:24:24 rillig Exp $ */
/*
* Implement 'meta' mode.
@@ -410,7 +410,7 @@ static Boolean
meta_needed(GNode *gn, const char *dname, const char *tname,
char *objdir, int verbose)
{
- struct make_stat mst;
+ struct cached_stat cst;
if (verbose)
verbose = DEBUG(META);
@@ -441,7 +441,7 @@ meta_needed(GNode *gn, const char *dname
}
/* The object directory may not exist. Check it.. */
- if (cached_stat(dname, &mst) != 0) {
+ if (cached_stat(dname, &cst) != 0) {
if (verbose)
debug_printf("Skipping meta for %s: no .OBJDIR\n", gn->name);
return FALSE;
@@ -1125,7 +1125,7 @@ meta_oodate(GNode *gn, Boolean oodate)
int pid;
int x;
StringListNode *cmdNode;
- struct make_stat mst;
+ struct cached_stat cst;
if (buf == NULL) {
bufsz = 8 * BUFSIZ;
@@ -1382,8 +1382,8 @@ meta_oodate(GNode *gn, Boolean oodate)
if ((strstr("tmp", p)))
break;
- if ((link_src != NULL && cached_lstat(p, &mst) < 0) ||
- (link_src == NULL && cached_stat(p, &mst) < 0)) {
+ if ((link_src != NULL && cached_lstat(p, &cst) < 0) ||
+ (link_src == NULL && cached_stat(p, &cst) < 0)) {
if (!meta_ignore(gn, p))
append_if_new(missingFiles, p);
}
@@ -1443,7 +1443,7 @@ meta_oodate(GNode *gn, Boolean oodate)
DEBUG3(META, "%s: %d: looking for: %s\n",
fname, lineno, *sdp);
#endif
- if (cached_stat(*sdp, &mst) == 0) {
+ if (cached_stat(*sdp, &cst) == 0) {
found = 1;
p = *sdp;
}
@@ -1453,12 +1453,12 @@ meta_oodate(GNode *gn, Boolean oodate)
DEBUG3(META, "%s: %d: found: %s\n",
fname, lineno, p);
#endif
- if (!S_ISDIR(mst.mst_mode) &&
- mst.mst_mtime > gn->mtime) {
+ if (!S_ISDIR(cst.cst_mode) &&
+ cst.cst_mtime > gn->mtime) {
DEBUG3(META, "%s: %d: file '%s' is newer than the target...\n",
fname, lineno, p);
oodate = TRUE;
- } else if (S_ISDIR(mst.mst_mode)) {
+ } else if (S_ISDIR(cst.cst_mode)) {
/* Update the latest directory. */
cached_realpath(p, latestdir);
}