Module Name:    src
Committed By:   rillig
Date:           Sat Jan 30 20:53:29 UTC 2021

Modified Files:
        src/usr.bin/make: buf.c buf.h cond.c dir.c for.c main.c parse.c var.c

Log Message:
make(1): split Buf_Destroy into Buf_Done and Buf_DoneData

In all cases except one, the boolean argument to Buf_Destroy was
constant.  Removing that argument by splitting the function into two
separate functions makes the intention clearer on the call site.  It
also removes the possibility for using the return value of Buf_Done,
which would have made no sense.

The function Buf_Done now pairs with Buf_Init, just as in HashTable and
Lst.

Even though Buf_Done is essentially a no-op, it is kept as a function,
both for symmetry with Buf_Init and for clearing the Buffer members
after use (this will be done only in CLEANUP mode, in a follow-up
commit).


To generate a diff of this commit:
cvs rdiff -u -r1.47 -r1.48 src/usr.bin/make/buf.c
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/make/buf.h
cvs rdiff -u -r1.253 -r1.254 src/usr.bin/make/cond.c
cvs rdiff -u -r1.264 -r1.265 src/usr.bin/make/dir.c
cvs rdiff -u -r1.138 -r1.139 src/usr.bin/make/for.c
cvs rdiff -u -r1.517 -r1.518 src/usr.bin/make/main.c
cvs rdiff -u -r1.533 -r1.534 src/usr.bin/make/parse.c
cvs rdiff -u -r1.783 -r1.784 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/buf.c
diff -u src/usr.bin/make/buf.c:1.47 src/usr.bin/make/buf.c:1.48
--- src/usr.bin/make/buf.c:1.47	Wed Dec 30 10:03:16 2020
+++ src/usr.bin/make/buf.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.c,v 1.47 2020/12/30 10:03:16 rillig Exp $	*/
+/*	$NetBSD: buf.c,v 1.48 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -75,7 +75,7 @@
 #include "make.h"
 
 /*	"@(#)buf.c	8.1 (Berkeley) 6/6/93"	*/
-MAKE_RCSID("$NetBSD: buf.c,v 1.47 2020/12/30 10:03:16 rillig Exp $");
+MAKE_RCSID("$NetBSD: buf.c,v 1.48 2021/01/30 20:53:29 rillig Exp $");
 
 /* Make space in the buffer for adding at least 16 more bytes. */
 void
@@ -175,18 +175,27 @@ Buf_Init(Buffer *buf)
 }
 
 /*
- * Reset the buffer.
- * If freeData is TRUE, the data from the buffer is freed as well.
- * Otherwise it is kept and returned.
+ * Free the data from the buffer.
+ * The buffer is left in an indeterminate state.
+ */
+void
+Buf_Done(Buffer *buf)
+{
+	free(buf->data);
+
+	buf->cap = 0;
+	buf->len = 0;
+	buf->data = NULL;
+}
+
+/*
+ * Return the data from the buffer.
+ * The buffer is left in an indeterminate state.
  */
 char *
-Buf_Destroy(Buffer *buf, Boolean freeData)
+Buf_DoneData(Buffer *buf)
 {
 	char *data = buf->data;
-	if (freeData) {
-		free(data);
-		data = NULL;
-	}
 
 	buf->cap = 0;
 	buf->len = 0;
@@ -206,16 +215,16 @@ Buf_Destroy(Buffer *buf, Boolean freeDat
  * a new buffer will be allocated and the old one freed.
  */
 char *
-Buf_DestroyCompact(Buffer *buf)
+Buf_DoneDataCompact(Buffer *buf)
 {
 #if BUF_COMPACT_LIMIT > 0
 	if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) {
 		/* We trust realloc to be smart */
 		char *data = bmake_realloc(buf->data, buf->len + 1);
 		data[buf->len] = '\0';	/* XXX: unnecessary */
-		Buf_Destroy(buf, FALSE);
+		Buf_DoneData(buf);
 		return data;
 	}
 #endif
-	return Buf_Destroy(buf, FALSE);
+	return Buf_DoneData(buf);
 }

Index: src/usr.bin/make/buf.h
diff -u src/usr.bin/make/buf.h:1.38 src/usr.bin/make/buf.h:1.39
--- src/usr.bin/make/buf.h:1.38	Mon Dec 28 15:42:53 2020
+++ src/usr.bin/make/buf.h	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.h,v 1.38 2020/12/28 15:42:53 rillig Exp $	*/
+/*	$NetBSD: buf.h,v 1.39 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -126,7 +126,8 @@ char *Buf_GetAll(Buffer *, size_t *);
 void Buf_Empty(Buffer *);
 void Buf_Init(Buffer *);
 void Buf_InitSize(Buffer *, size_t);
-char *Buf_Destroy(Buffer *, Boolean);
-char *Buf_DestroyCompact(Buffer *);
+void Buf_Done(Buffer *);
+char *Buf_DoneData(Buffer *);
+char *Buf_DoneDataCompact(Buffer *);
 
 #endif /* MAKE_BUF_H */

Index: src/usr.bin/make/cond.c
diff -u src/usr.bin/make/cond.c:1.253 src/usr.bin/make/cond.c:1.254
--- src/usr.bin/make/cond.c:1.253	Fri Jan 22 00:12:01 2021
+++ src/usr.bin/make/cond.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.253 2021/01/22 00:12:01 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.254 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -95,7 +95,7 @@
 #include "dir.h"
 
 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: cond.c,v 1.253 2021/01/22 00:12:01 rillig Exp $");
+MAKE_RCSID("$NetBSD: cond.c,v 1.254 2021/01/30 20:53:29 rillig Exp $");
 
 /*
  * The parsing of conditional expressions is based on this grammar:
@@ -282,8 +282,8 @@ ParseFuncArg(CondParser *par, const char
 		p++;
 	}
 
-	*out_arg = Buf_GetAll(&argBuf, &argLen);
-	Buf_Destroy(&argBuf, FALSE);
+	argLen = argBuf.len;
+	*out_arg = Buf_DoneData(&argBuf);
 
 	cpp_skip_hspace(&p);
 
@@ -539,9 +539,9 @@ CondParser_String(CondParser *par, Boole
 		}
 	}
 got_str:
-	str = FStr_InitOwn(Buf_GetAll(&buf, NULL));
+	str = FStr_InitOwn(buf.data);
 cleanup:
-	Buf_Destroy(&buf, FALSE);
+	Buf_DoneData(&buf);
 	*out_str = str;
 }
 

Index: src/usr.bin/make/dir.c
diff -u src/usr.bin/make/dir.c:1.264 src/usr.bin/make/dir.c:1.265
--- src/usr.bin/make/dir.c:1.264	Sun Jan 24 20:11:55 2021
+++ src/usr.bin/make/dir.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.264 2021/01/24 20:11:55 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.265 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -138,7 +138,7 @@
 #include "job.h"
 
 /*	"@(#)dir.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: dir.c,v 1.264 2021/01/24 20:11:55 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.265 2021/01/30 20:53:29 rillig Exp $");
 
 /*
  * A search path is a list of CachedDir structures. A CachedDir has in it the
@@ -1645,7 +1645,7 @@ SearchPath_ToFlags(SearchPath *path, con
 		}
 	}
 
-	return Buf_Destroy(&buf, FALSE);
+	return Buf_DoneData(&buf);
 }
 
 /* Free the search path and all directories mentioned in it. */

Index: src/usr.bin/make/for.c
diff -u src/usr.bin/make/for.c:1.138 src/usr.bin/make/for.c:1.139
--- src/usr.bin/make/for.c:1.138	Mon Jan 25 19:39:34 2021
+++ src/usr.bin/make/for.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: for.c,v 1.138 2021/01/25 19:39:34 rillig Exp $	*/
+/*	$NetBSD: for.c,v 1.139 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1992, The Regents of the University of California.
@@ -58,7 +58,7 @@
 #include "make.h"
 
 /*	"@(#)for.c	8.1 (Berkeley) 6/6/93"	*/
-MAKE_RCSID("$NetBSD: for.c,v 1.138 2021/01/25 19:39:34 rillig Exp $");
+MAKE_RCSID("$NetBSD: for.c,v 1.139 2021/01/30 20:53:29 rillig Exp $");
 
 
 /* One of the variables to the left of the "in" in a .for loop. */
@@ -103,7 +103,7 @@ ForLoop_New(void)
 static void
 ForLoop_Free(ForLoop *f)
 {
-	Buf_Destroy(&f->body, TRUE);
+	Buf_Done(&f->body);
 
 	while (f->vars.len > 0) {
 		ForVar *var = Vector_Pop(&f->vars);
@@ -112,7 +112,7 @@ ForLoop_Free(ForLoop *f)
 	Vector_Done(&f->vars);
 
 	Words_Free(f->items);
-	Buf_Destroy(&f->curBody, TRUE);
+	Buf_Done(&f->curBody);
 
 	free(f);
 }

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.517 src/usr.bin/make/main.c:1.518
--- src/usr.bin/make/main.c:1.517	Sun Jan 24 20:11:55 2021
+++ src/usr.bin/make/main.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.517 2021/01/24 20:11:55 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.518 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -110,7 +110,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.517 2021/01/24 20:11:55 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.518 2021/01/30 20:53:29 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1841,7 +1841,7 @@ Cmd_Exec(const char *cmd, const char **e
 			JobReapChild(pid, status, FALSE);
 
 		res_len = Buf_Len(&buf);
-		res = Buf_Destroy(&buf, FALSE);
+		res = Buf_DoneData(&buf);
 
 		if (savederr != 0)
 			*errfmt = "Couldn't read shell's output for \"%s\"";
@@ -2027,7 +2027,7 @@ execDie(const char *af, const char *av)
 
 	write_all(STDERR_FILENO, Buf_GetAll(&buf, NULL), Buf_Len(&buf));
 
-	Buf_Destroy(&buf, TRUE);
+	Buf_Done(&buf);
 	_exit(1);
 }
 

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.533 src/usr.bin/make/parse.c:1.534
--- src/usr.bin/make/parse.c:1.533	Wed Jan 27 00:02:38 2021
+++ src/usr.bin/make/parse.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.533 2021/01/27 00:02:38 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.534 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -109,7 +109,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.533 2021/01/27 00:02:38 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.534 2021/01/30 20:53:29 rillig Exp $");
 
 /* types and constants */
 
@@ -455,7 +455,7 @@ loadfile(const char *path, int fd)
 	{
 		struct loadedfile *lf = loadedfile_create(path,
 		    buf.data, buf.len);
-		Buf_Destroy(&buf, FALSE);
+		Buf_DoneData(&buf);
 		return lf;
 	}
 }

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.783 src/usr.bin/make/var.c:1.784
--- src/usr.bin/make/var.c:1.783	Sat Jan 30 15:48:42 2021
+++ src/usr.bin/make/var.c	Sat Jan 30 20:53:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.783 2021/01/30 15:48:42 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.784 2021/01/30 20:53:29 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.783 2021/01/30 15:48:42 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.784 2021/01/30 20:53:29 rillig Exp $");
 
 typedef enum VarFlags {
 	VAR_NONE	= 0,
@@ -456,7 +456,10 @@ VarFreeEnv(Var *v, Boolean freeValue)
 		return FALSE;
 
 	FStr_Done(&v->name);
-	Buf_Destroy(&v->val, freeValue);
+	if (freeValue)
+		Buf_Done(&v->val);
+	else
+		Buf_DoneData(&v->val);
 	free(v);
 	return TRUE;
 }
@@ -499,7 +502,7 @@ Var_DeleteVar(const char *varname, GNode
 		var_exportedVars = VAR_EXPORTED_NONE;
 	assert(v->name.freeIt == NULL);
 	HashTable_DeleteEntry(&ctxt->vars, he);
-	Buf_Destroy(&v->val, TRUE);
+	Buf_Done(&v->val);
 	free(v);
 }
 
@@ -1214,9 +1217,9 @@ SepBuf_AddStr(SepBuf *buf, const char *s
 }
 
 static char *
-SepBuf_Destroy(SepBuf *buf, Boolean free_buf)
+SepBuf_DoneData(SepBuf *buf)
 {
-	return Buf_Destroy(&buf->buf, free_buf);
+	return Buf_DoneData(&buf->buf);
 }
 
 
@@ -1686,7 +1689,7 @@ VarSelectWords(char sep, Boolean oneBigW
 
 	Words_Free(words);
 
-	return SepBuf_Destroy(&buf, FALSE);
+	return SepBuf_DoneData(&buf);
 }
 
 
@@ -1731,7 +1734,7 @@ ModifyWords(const char *str,
 	if (oneBigWord) {
 		SepBuf_Init(&result, sep);
 		modifyWord(str, &result, modifyWord_args);
-		return SepBuf_Destroy(&result, FALSE);
+		return SepBuf_DoneData(&result);
 	}
 
 	SepBuf_Init(&result, sep);
@@ -1749,7 +1752,7 @@ ModifyWords(const char *str,
 
 	Words_Free(words);
 
-	return SepBuf_Destroy(&result, FALSE);
+	return SepBuf_DoneData(&result);
 }
 
 
@@ -1771,7 +1774,7 @@ Words_JoinFree(Words words)
 
 	Words_Free(words);
 
-	return Buf_Destroy(&buf, FALSE);
+	return Buf_DoneData(&buf);
 }
 
 /* Remove adjacent duplicate words. */
@@ -1818,7 +1821,7 @@ VarQuote(const char *str, Boolean quoteD
 			Buf_AddStr(&buf, "\\$");
 	}
 
-	return Buf_Destroy(&buf, FALSE);
+	return Buf_DoneData(&buf);
 }
 
 /*
@@ -2164,7 +2167,7 @@ ParseModifierPartSubst(
 	if (out_length != NULL)
 		*out_length = Buf_Len(&buf);
 
-	*out_part = Buf_Destroy(&buf, FALSE);
+	*out_part = Buf_DoneData(&buf);
 	DEBUG1(VAR, "Modifier part: \"%s\"\n", *out_part);
 	return VPR_OK;
 }
@@ -2365,10 +2368,10 @@ ApplyModifier_Defined(const char **pp, c
 	ApplyModifiersState_Define(st);
 
 	if (eflags & VARE_WANTRES) {
-		st->newVal = FStr_InitOwn(Buf_Destroy(&buf, FALSE));
+		st->newVal = FStr_InitOwn(Buf_DoneData(&buf));
 	} else {
 		st->newVal = FStr_InitRefer(val);
-		Buf_Destroy(&buf, TRUE);
+		Buf_Done(&buf);
 	}
 	return AMR_OK;
 }
@@ -2563,7 +2566,7 @@ ApplyModifier_Range(const char **pp, con
 		Buf_AddInt(&buf, 1 + (int)i);
 	}
 
-	st->newVal = FStr_InitOwn(Buf_Destroy(&buf, FALSE));
+	st->newVal = FStr_InitOwn(Buf_DoneData(&buf));
 	return AMR_OK;
 }
 
@@ -2988,7 +2991,7 @@ ApplyModifier_Words(const char **pp, con
 			/* 3 digits + '\0' is usually enough */
 			Buf_InitSize(&buf, 4);
 			Buf_AddInt(&buf, (int)ac);
-			st->newVal = FStr_InitOwn(Buf_Destroy(&buf, FALSE));
+			st->newVal = FStr_InitOwn(Buf_DoneData(&buf));
 		}
 		goto ok;
 	}
@@ -3803,7 +3806,7 @@ ParseVarname(const char **pp, char start
 	}
 	*pp = p;
 	*out_varname_len = Buf_Len(&buf);
-	return Buf_Destroy(&buf, FALSE);
+	return Buf_DoneData(&buf);
 }
 
 static VarParseResult
@@ -4070,7 +4073,7 @@ ParseVarnameLong(
 static void
 FreeEnvVar(void **out_val_freeIt, Var *v, const char *value)
 {
-	char *varValue = Buf_Destroy(&v->val, FALSE);
+	char *varValue = Buf_DoneData(&v->val);
 	if (value == varValue)
 		*out_val_freeIt = varValue;
 	else
@@ -4241,7 +4244,7 @@ Var_Parse(const char **pp, GNode *ctxt, 
 			}
 		}
 		if (value.str != Buf_GetAll(&v->val, NULL))
-			Buf_Destroy(&v->val, TRUE);
+			Buf_Done(&v->val);
 		FStr_Done(&v->name);
 		free(v);
 	}
@@ -4365,7 +4368,7 @@ Var_Subst(const char *str, GNode *ctxt, 
 			VarSubstPlain(&p, &res);
 	}
 
-	*out_res = Buf_DestroyCompact(&res);
+	*out_res = Buf_DoneDataCompact(&res);
 	return VPR_OK;
 }
 

Reply via email to